简体   繁体   中英

Magento: Cant get product collection use custom attribute filter?

I have a collection of products in my magento catalog that are tagged as samplers. I need to pull back this specific collection. The attribute is part of an attribute set called candies.

When I go to load the collection, I add a filter to narrow by the attribute set, and then add another filter for the sampler attribute I created. No matter what I do with the filter, I always get back ALL the candies not just the ones with the attribute of sampler set to "yes" or 1.

Here is my code, how do I get the result I am looking for?

 $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.

Thanks in advance.

See this: http://gielberkers.com/magento-attributes-missing-using-flat-catalog/

After considering the comments here, I would try this setting.

You'll have iterate over the collection. Load the id of each product, then use ->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'));
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM