简体   繁体   English

Magento 1-按名称获取产品

[英]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. 我目前正在检查post参数是否具有特定的值,并使用pid初始化一个变量,然后在后端手动查找并输入脚本。

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. 我可以开发一种将所有产品模型加载到for循环中并检查名称的方法,但是这将占用过多的资源,并且加载时间极长,并且服务器不满意。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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