Magento标记多种产品的集合

时间:2017-05-21 08:03:58

标签: php magento tags

我的PHP知识有限,我正在使用Magento 1.9.3。

我需要显示多个产品的标签集合,我不明白为什么这段代码不起作用:

//List of my products
$displayProduct = $this->getCollection();
ob_start();
foreach ($displayProduct as $_product) { 
    echo ($_product->getId().','); 
}
$output = substr(ob_get_clean(), 0, -1);
echo $output;

// Tags list
$model = Mage::getModel('tag/tag'); 
$TaGCollection = $model->getResourceCollection() 
    ->addPopularity() 
    ->addProductFilter(array($output))
    ->setFlag('relation', true) 
    ->addStoreFilter(Mage::app()
    ->getStore()->getId()) 
    ->limit(30)
    ->setActiveFilter()
    ->load();

第一个集合正确显示此产品ID列表:

548,549,650,675,676,686,761,534,535,533,766,767,768,772,778,783,786,790,794,814,818

如果我将此列表作为数组值粘贴到第二个集合中,则可以正常工作。但是当我在第二个集合中插入变量$output时,它不起作用。

我错过了什么?

3 个答案:

答案 0 :(得分:0)

   //List of my products
   $displayProduct = $this->getCollection();
   $output = array();  /declaration as an arraY
   foreach ($displayProduct as $_product) { 
       array_push($output,$_product);  //PUSH Product Ids in $output 
   }



    // Tags list
    $model = Mage::getModel('tag/tag'); 
    $TaGCollection = $model->getResourceCollection() 
           ->addPopularity() 
           ->addProductFilter($output)
           ->setFlag('relation', true) 
           ->addStoreFilter(Mage::app()
           ->getStore()->getId()) 
           ->limit(30)
           ->setActiveFilter()
           ->load();

答案 1 :(得分:0)

//获取产品集合

$displayProduct = $this->getCollection();
$output = array();  //declaration as an array
foreach ($displayProduct as $_product) { 
array_push($output,$_product->getId());  //PUSH Product Ids in $output 
}

//按产品ID获取标签,即基于$ output变量

$model = Mage::getModel('tag/tag'); 
$TagCollection = $model->getResourceCollection() 
->addPopularity() 
->addProductFilter($output)
->setFlag('relation', true) 
->addStoreFilter(Mage::app()
->getStore()->getId()) 
->limit(30)
->setActiveFilter()
->load();

然后循环遍历$ TagCollection以逐个获取标签。

答案 2 :(得分:0)

嗯,这个解决方案确实有效:

//List of my products
$displayProduct = $this->getCollection();
ob_start();
foreach ($displayProduct as $_product) { 
    echo ($_product->getId().', '); 
}
$output = substr(ob_get_clean(), 0, -2);
$productIds = explode(', ', trim($output));

// Tags list
$model = Mage::getModel('tag/tag'); 
$TaGCollection = $model->getResourceCollection() 
    ->addPopularity() 
    ->addFieldToFilter('product_id', array('in' => $productIds))
    ->setFlag('relation', true) 
    ->addStoreFilter(Mage::app()
    ->getStore()->getId()) 
    ->limit(30)
    ->setActiveFilter()
    ->load();

感谢您的回答!! :)