Magento:无法使用自定义属性过滤器获取产品集合


Magento: Cant get product collection use custom attribute filter?

我的magento目录中有一组标记为样品的产品。我需要撤回这部分藏品。该属性是名为candies的属性集的一部分。

当我去加载集合时,我添加一个过滤器来缩小属性集,然后为我创建的sampler属性添加另一个过滤器。无论我用过滤器做什么,我总是能得到所有的糖果,而不仅仅是那些sampler属性设置为"yes"或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'));
?>