简体   繁体   中英

Magento 1 - get product by name

I currently check if a post param has a specific value and init a variable with the pid which I manually look up in the backend and enter in the script.

if (strpos($this->gleit, "Alu Gleitabschluss - Design") !== false) { $this->gleit_pid  = 2322; }
else if (strpos($this->gleit, "Kunststoff-Gleitabschluss") !== false) { $this->gleit_pid  = 2954; }
else if (strpos($this->gleit, "Alu Gleitabschluss") !== false) { $this->gleit_pid  = 2950; }
else if (strpos($this->gleit, "Alu-Endstück Putz") !== false) { $this->gleit_pid  = 2936; }
else if (strpos($this->gleit, "Alu-Endstück Klinker") !== false) { $this->gleit_pid  = 3227; }

Thats not a good solution, if there are new products, then I have to add more code.

Is there a way to load the product by it's name? I could develope one which loads all product models in a for loop and check the name, but this would take too much ressources and it would load extremly long and the server would not be happy.

eg

public function findProductByName($name, $start, $end)
{
    for($pid = $start; $pid < $end; $pid++ ) {
        $tmpModel = Mage::getModel('catalog/product')->load($pid);
        $tmpName = $tmpModel->getName();

        $a = get_object_vars($tmpModel);
        $b = get_class_methods($tmpModel);

        if ($tmpName == $name) {
            return tmpModel;
        }
    }
}

How to do it right.

You can do using the following:

$_product= Mage::getModel('catalog/product')->loadByAttribute('name','Product Name');

echo  $_product->getId();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM