【问题标题】:magento get product using proudct idmagento 使用产品 ID 获取产品
【发布时间】:2014-05-13 10:39:02
【问题描述】:

我正在尝试获取 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;

【问题讨论】:

  • 您好,感谢您的回复,我试过了,但现在它只显示一个产品,我在 count($_product) 值中有 18 个产品。当我 print_r($_product) 时它显示所有 id。跨度>
  • 您犯了一个错误...您正在重新分配 $_product 数组,因此它只为您提供一种产品,因此请在 for 循环中将 $_product 更改为 $_productnew...检查我的代码跨度>

标签: magento attributes product


【解决方案1】:

试试这个

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

【讨论】:

  • @vishal:有时会发生 :)
【解决方案2】:

将 $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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多