1

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

1

3 Answers 3

1
  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.
2
  • It is no new attribute, it's been there for a while. However after reindexing I can getParentSku() with no problem.
    – Alex
    Nov 28, 2017 at 11:41
  • Yes, I knew it that's why I mentioned it First
    – PЯINCƎ
    Nov 28, 2017 at 12:54
2

Please try this code:

$subCollection = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect('*')
        ->addFieldToFilter('parent_sku', $parent_sku);
2
  • I think it's addAttributeToFilter for product collections, not addFieldToFilter
    – Piyush
    Nov 25, 2017 at 5:09
  • I still get no parent_sku in the collection, I tried addAttributeToSelect and addFieldToFilter with '*' and with array('id','parent_sku') but I still can't get the parent_sku custom attribute in the collection.
    – Alex
    Nov 27, 2017 at 15:14
0

try this code:

$subCollection = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect('*')
        ->addFieldToFilter('parent_sku', array('eq' => $parent_sku))
        ->load();
1
  • I tried the load at the end, but still no parent_skull custom attribute in the collection
    – Alex
    Nov 27, 2017 at 15:14

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.