[英]Magento tags collection for multiple products
My PHP knowledge is somewhat limited and I'm working with Magento 1.9.3. 我的PHP知识有限,我正在使用Magento 1.9.3。
I need to display a tags collection from multiple products and I don't understand why this code doesn't work: 我需要显示来自多个产品的标签集合,但我不明白为什么此代码不起作用:
//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();
The first collection displays this list of products ids correctly: 第一个集合正确显示此产品ID列表:
548,549,650,675,676,686,761,534,535,533,766,767,768,772,778,783,786,790,794,814,818
If I paste this list as array values in the second collection it works. 如果我将此列表作为数组值粘贴到第二个集合中,它将起作用。 But when I insert the variable $output
in the second collection it doesn't work. 但是,当我在第二个集合中插入变量$output
,它不起作用。
What did I miss? 我错过了什么?
//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();
//Get the collection of products //获取产品集合
$displayProduct = $this->getCollection();
$output = array(); //declaration as an array
foreach ($displayProduct as $_product) {
array_push($output,$_product->getId()); //PUSH Product Ids in $output
}
// Get Tags By Product ids, that is based on $output variable //根据产品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();
Then loop through the $TagCollection to get tags one by one. 然后遍历$ TagCollection来逐个获取标签。
Well, this solution really works: 嗯,这个解决方案确实有效:
//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();
Thanks for your answers!! 感谢您的回答! :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.