Magento 1.9 – Can’t Filter Collection by Custom Attribute

collection;custom-attributesmagento-1.9product-collection

I have a custom attribute called parent_sku, I setted it to yes in Product Listing from the backend menu, and almost all the products have a value in this attribute.

I tried:

$subCollection = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect(array('id','parent_sku'))
        ->addAttributeToFilter('parent_sku', array('eq' => $parent_sku));

I also tried (I know it is the same):

$subCollection = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect(array('id','parent_sku'))
        ->addAttributeToFilter('parent_sku', $parent_sku));

$parent_sku has a valid value, that is the same of the value that have some of the products in parent_sku attribute.

But I get always $subCollection empty.

count($subCollection)  // this is always zero

paren_sku custom attribute configuration

Best Answer

  1. After adding your attribute, you should to reindexAll
  2. Try this:

    $subCollection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect(array('name', 'price', 'parent_sku'));
    
  3. Clear your browser and Magento cache.