2017-05-21 112 views
0

我的PHP知識是有限的,我正在與Magento 1.9.3。Magento標籤集合的多個產品

我需要顯示來自多個產品標籤收集和我不明白爲什麼這個代碼不工作:

//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(); 

第一款集合顯示器的產品清單IDS正確:

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

如果我將這個列表作爲數組值粘貼到第二個集合中,它就起作用。但是當我在第二個集合中插入變量$output時,它不起作用。

我錯過了什麼?

回答

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(); 
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,是基於$輸出變量

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

然後遍歷$ TagCollection到逐個獲取標籤。

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(); 

謝謝您的回答! :)