Question

I'm having issues trying to create a collection of child / associated products given a particular parent / configurable product.

I have no issue creating the collection for the parent (i.e. configurable) products that I want, no issue getting my entity IDs for the child products (that is, in the below code snippet, $childIds contains the correct values) but when I attempt to build my collection, $childrenCollection->getSize() is sometimes different than count($childIds[0]), even though I'm filtering based on those values.

<?php

include_once('app/Mage.php');
Mage::app();

$attCodeBrand = 'brand';

$attBrand = Mage::getModel('catalog/product')->getResource()->getAttribute($attCodeBrand);

$brandId = $attBrand->getSource()->getOptionId('Sears');

$configurableCollection = Mage::getResourceModel('catalog/product_collection')
        ->addAttributeToFilter('type_id', array('eq' => 'configurable'))
        ->addAttributeToFilter('brand', array('eq'=>$brandId));

while ($parent = $configurableCollection->fetchItem())
{
        $parentId = $parent->getId();

        $childIds = Mage::getSingleton('catalog/product_type_configurable')->getChildrenIds($parentId);

        $childrenCollection = Mage::getResourceModel('catalog/product_collection')
        ->addAttributeToFilter('entity_id', array('in'=>$childIds[0]));
}
?>

I thought perhaps that my associated products somehow linked to a product with a bad ID, but I have tested explicitly loading the products that are missed in $childrenCollection and there's no issue there.

Is there some sort of implicit filtering that's going on that I'm not aware of?

Edit: Magento version is 1.14.2.4 EE.

Was it helpful?

Solution

I forgot to set the store ID.

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top