按自定义属性检索多个产品[magento]
我有一个产品的自定义属性,假设我有3-4个产品在该属性上具有相同的值。现在我想加载这3个产品并将它们存储在一个数组中。我是这样做的:按自定义属性检索多个产品[magento],magento,magento-1.7,Magento,Magento 1.7,我有一个产品的自定义属性,假设我有3-4个产品在该属性上具有相同的值。现在我想加载这3个产品并将它们存储在一个数组中。我是这样做的: $all_products = array(); $count = 0; $collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToSelect('ordernumber'); foreach($collection as $
$all_products = array();
$count = 0;
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('ordernumber');
foreach($collection as $product)
{
$all_products[count] = array ('sku' => $product->getSku(), 'qty' => $this->getRequest()->get('qty'), 'size' => $product->getSize());
$count++;
}
return $all_products;
注意:ordernumber
有一个值,我只是从另一个地方得到它
无论如何,当我运行它时,我没有值。调试时-调试器终止于$products[count]=…
我尝试通过自定义属性加载单个产品-Mage::…->loadByAttribute('ordernumber',$ordernumber)代码>并且它工作得很好
因此,我的猜测是,我没有正确地使用集合操作或数组,尽管我看到了一些示例,从中我得到了这个想法
我做错了什么?试着这样做:
$all_products = array();
$count = 0;
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('ordernumber');
//add a filter if needed - uncomment the next 2 lines and adjust the value or $ordernumber
//$ordernumber = '123';
//$addAttributeToFilter('ordernumber', $ordernumber)
foreach($collection as $product)
{
$all_products[$count] = array ('sku' => $product->getSku(), 'qty' => $this->getRequest()->get('qty'), 'size' => $product->getSize());
$count++;
}
return $all_products;