簡體   English   中英

Magento:無法使用自定義屬性過濾器來獲取產品集合?

[英]Magento: Cant get product collection use custom attribute filter?

我的magento目錄中有一些產品被標記為采樣器。 我需要撤回該特定集合。 該屬性是稱為糖果的屬性集的一部分。

當我去加載集合時,我添加了一個過濾器以縮小屬性集,然后為我創建的采樣器屬性添加另一個過濾器。 無論我對過濾器做什么,我總是會取回所有糖果,而不僅僅是采樣器屬性設置為“是”或1的糖果。

這是我的代碼,如何獲得所需的結果?

 $products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')  //select all fields available to product
    ->addFieldToFilter('attribute_set_id', 9) //filter the collection by the candies attribute set
    ->addAttributeToFilter('sampler','yes'); //filter the collection to only products that have the attribute sampler set to "yes" - this part doesnt work.

提前致謝。

請參閱: http : //gielberkers.com/magento-attributes-missing-using-flat-catalog/

在考慮了此處的評論之后,我將嘗試此設置。

您將遍歷整個集合。 加載每個產品的ID,然后使用-> getAttributeText('')。

    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('*')
    ->addAttributeToFilter('type_id', 'configurable')
    ;
    foreach ($collection as $_product) {
        $id =  $_product->getId();
        if($tncValue = $_product->load($id)->getAttributeText('terms_and_conditions')){
            echo $id . ' => ' . $tncValue . "\n";
        }
    }
<?php
    $products = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect('*')
        ->addFieldToFilter('attribute_set_id', 9)
        ->addAttributeToFilter('sampler', array('eq' => '1'));
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM