magento使用产品ID获取产品

时间:2014-05-13 10:39:03

标签: magento attributes product

我正在尝试获取magento的特性产品,我在每个变量中正确获取所有产品ID,但它不在循环内工作并显示重复的产品。

这是我的代码

$storeId = Mage::app()->getStore()->getId();   
$categoryId = $this->getRequest()->getParam('id', false);
$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('catalog_read');
$categoryProductTable = $resource->getTableName('catalog/category_product');
//$productEntityIntTable = $resource->getTableName('catalog/product_entity_int'); // doesn't work :(
$productEntityIntTable = (string)Mage::getConfig()->getTablePrefix() . 'catalog_product_entity_int';
$eavAttributeTable = $resource->getTableName('eav/attribute');

$select = $read->select()
    ->from(array('cp'=>$categoryProductTable))
    ->join(array('pei'=>$productEntityIntTable), 'pei.entity_id=cp.product_id', array())
    ->joinNatural(array('ea'=>$eavAttributeTable))
    ->where('pei.value=1')
    ->where('ea.attribute_code="featured"');            

$_product = $read->fetchAll($select);
$total_product= count($_product);

当我print_r($ _ product)时,它会显示多个产品ID,这些功能是正确的

但在for循环中,当我尝试使用id获取产品名称时,它会显示重复的产品名称,这里是代码

$obj = Mage::getModel('catalog/product');
for($cp=0;$cp <= $total_product; $cp++):
    $_product= $obj->load($_product[$cp]['product_id']);
    echo $_product->getName();
endfor;

2 个答案:

答案 0 :(得分:2)

试试这个

for($cp=0;$cp <= $total_product; $cp++):
 $obj = Mage::getModel('catalog/product');
 $_productnew= $obj->load($_product[$cp]['product_id']);
echo $_productnew->getName();
endfor;

答案 1 :(得分:0)

将$ obj放入for循环中,如下所示,

for($cp=0;$cp <= $total_product; $cp++):
 $obj = Mage::getModel('catalog/product');
 $_product= $obj->load($_product[$cp]['product_id']);
 echo $_product->getName();

endfor;