2013-02-12 98 views
0

我正在嘗試執行與ajax相關的magento搜索,但我無法正確地將多選類型產品屬性添加到產品集合中,例如: :將多列表屬性添加到magento中的產品收集過濾器

$productModel = Mage::getModel('catalog/product'); //getting product model 
$productCollection = $productModel->getCollection(); 
$productCollection->addAttributeToSelect(
         Mage::getSingleton('catalog/config') 
          ->getProductAttributes() 
        ); 
$productCollection->addAttributeToFilter(
      array(
        array('attribute'=>'my_attribute_id', 
          'finset' => Mage::getResourceModel('catalog/product') 
             ->getAttribute('my_attribute_id')   
             ->getSource() 
             ->getOptionId($searched)) 
        ); 

其中$被搜索是我保存關鍵字的字符串。現在,讓我們假設my_attribute_id是一個多選產品屬性,它有一個名稱爲「紅牛」的選項...如果我搜索確切的字符串「紅牛」後,它的工作原理,但我想工作,如果我搜索只有在「紅」或「公牛」之後。 即使搜索字符串不完整,是否有方法可以獲取屬性的選項ID?因爲這個問題是在這裏:

Mage::getResourceModel('catalog/product') 
         ->getAttribute('my_attribute_id') 
         ->getSource() 
         ->getOptionId($searched)) 

此代碼返回屬性選項的ID只有當我搜索它entirely.Probably模型並查詢像這樣

"select...where value='$searched'" 

有沒有一種辦法獲得屬性選項ID列表是即使選項的值是不完整?..所以做了這樣的查詢

"select...where value like '%$searched%'" 

還是有更好的方法來檢索產品收集後多選屬性部分值,除了我正在嘗試的解決方案? 非常感謝!

回答

0

請試試這個..

$collection = Mage::getModel('catalog/product')->getCollection(); 
    ->addAttributeToSelect('*') 
    ->addFieldToFilter(
     'my_attribute_id', 
     array(
      'like' => Mage::getResourceModel('catalog/product') 
         ->getAttribute('my_attribute_id') 
         ->getSource() 
         ->getOptionId($searched) 
     ) 
    ); 
+0

感謝您的答覆...我加入您的建議,但沒有工作.. 我忘了在原崗位提,我也加入以下代碼: $ productCollection-> addAttributeToSelect(Mage :: getSingleton('catalog/config') - > getProductAttributes());無論如何,我用您的建議替換了我的代碼,但仍然沒有任何結果。 – 2013-02-12 12:17:36

相關問題