3

Now I'm trying to filter product collections with my configurable product attribute i.e size

$collection = Mage::getModel('catalog/product')
              ->getCollection()
              ->addAttributeToSelect('*')
              ->addAttributeToFilter('visibility','4')
              ->addAttributeToFilter('size','3');

Above code doesn't works. Apart from attribute size i can able to filter this collection with any attributes like name, sku, created_date etc.

Did anyone know where i'm doing wrong ?

3 Answers 3

3

Figured it out by myself.

Above code returns nothing because attribute (size) is assigned only to simple products (associated products) and all simple product's visibility is set to 1 i.e, not visible individually.

Step 1 : Remove visibility to 1, now i can get list of all simple products (associated products) which is assigned to size 3.

Step 2 : Join table catalog_product_super_link to get parent product associated with it.

Complete code is,

    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToFilter(
            array(
            array('attribute'=> 'color','like' => '12')
            )
        )
        // multiple filters can be achieved by adding another filterattribute
        ->addAttributeToFilter(
            array(
            array('attribute'=> 'size','like' => '3')
            )
        );

   $collection->getSelect()->joinLeft(array('link_table' => 'catalog_product_super_link'),'link_table.product_id = e.entity_id',
            array('product_id','parent_id')
        );

   $collection->getSelect()->group('link_table.parent_id');

   foreach($collection as $product) {
        $ids[] = $product->getParentId();
       }

    $collection->load();

    echo json_encode($ids);

Hope this may help to someone.

1
  • visibility = 4 was very tricky :) as the associated products have visibility = 1 Commented Aug 27, 2015 at 8:10
1

visibility = 4 made it tricky. Commented it out and size filter worked fine.

$collection = Mage::getModel('catalog/product')
              ->getCollection()
              ->addAttributeToSelect('*')
              //->addAttributeToFilter('visibility', '4')
              ->addAttributeToFilter('size', array('3', '4')); // 3, 4 are attribute's option_id
1

Try this way:

$filter = array(
    'attribute' => 'size',
    'in'        => array(3),
    );

$collection = Mage::getModel('catalog/product')
          ->getCollection()
          ->addAttributeToSelect('*')
          //->addAttributeToFilter('visibility',4)
          ->addAttributeToFilter($filter);

Haven't tested, but should work. Hope this helps. Ref.

3
  • Not working for me. Using above code in Magento 1.9.1.0, I am getting the following error: Fatal error: Call to a member function getBackend() on a non-object in app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 816 Commented Aug 27, 2015 at 6:55
  • Can you try with removing ' ' for numbers. Commented Aug 27, 2015 at 6:57
  • Did that. Same error. Commented Aug 27, 2015 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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