3

I have product attribute called 'category_3' with value 'kids' or null.i wanted only those products with an attribute 'category_3' value equals to null.my query is as follows:-

$products = Mage::getModel('catalog/category')->load(40)
            ->getProductCollection()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('status', 1)
            ->addAttributeToFilter('visibility', 4)
            ->setOrder('price', 'ASC');

i had tried following options and i am not getting the products with the category_3 value as null

// ->addAttributeToFilter('category_3',array('NULL' => true))
    //->addAttributeToFilter('category_3',array('eq'=>'NULL'))
    // ->addAttributeToFilter('category_3','')

1 Answer 1

4

Add your attribute to product collection using below code

<config>
    <frontend>
        <product>
            <collection>
                <attributes>
                    <category_3 />
                </attributes>
            </collection>
        </product>
    </frontend>
</config>

after adding attribute to collection using below code you can filter collection

$products = Mage::getModel('catalog/category')->load(40)
            ->getProductCollection()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('category_3',array('null' => true))
            ->addAttributeToFilter('status', 1)
            ->addAttributeToFilter('visibility', 4)
            ->setOrder('price', 'ASC');
6
  • still not working Mar 8, 2016 at 6:50
  • Debug product collection have attribute 'category_3' ? Mar 8, 2016 at 6:52
  • when i write ->addAttributeToFilter('category_3','kids'); i get proper result Mar 8, 2016 at 6:55
  • Have you save product after creating attribute? I think there is no null value for category_3. Save any product with null value of category_3. Mar 8, 2016 at 7:15
  • yes i had imported products after creation of the attributes and addinginto attribute set.in the database i can see the values as null also Mar 8, 2016 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.