MediaGiantDesign
MediaGiantDesign

Reputation: 628

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.

Upvotes: 1

Views: 6384

Answers (3)

webwiseguys
webwiseguys

Reputation: 331

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";
        }
    }

Upvotes: 0

Niloct
Niloct

Reputation: 10015

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

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

Upvotes: 2

alex
alex

Reputation: 57

<?php
    $products = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect('*')
        ->addFieldToFilter('attribute_set_id', 9)
        ->addAttributeToFilter('sampler', array('eq' => '1'));
?>

Upvotes: -1

Related Questions