2

I'm getting a collection of products which are on a customer's wishlist by passing in the IDs of those products (a mix of simple and configurable products) in the $customerListItemIds array and then running a stock check on them in the foreach loop. This is working fine except it's flagging up all configurable products as having 0 stock when, actually, their child products do have stock.

So I tried adding the visibility and type_id filters to the collection to skip configurable products and to include all simple products but this isn't working – the child simple products are not being added to the collection.

My question is how can I include the children of the configurable products and not include their parent?

<?php

    $lowStockCollection = array();
    $stockThreshold = Mage::getStoreConfig('cataloginventory/options/stock_threshold_qty');

    ...

    $productCollection = Mage::getModel('catalog/product')
                         ->getCollection()
                         ->addAttributeToSelect('*')
                         ->joinField('qty',
                             'cataloginventory/stock_item',
                             'qty',
                             'product_id=entity_id',
                             '{{table}}.stock_id=1',
                             'left')
                         ->addAttributeToFilter('visibility', array(1,2,3,4))
                         ->addAttributeToFilter('type_id', Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)
                         ->addAttributeToFilter('qty', array('gt' => 0))
                         ->addAttributeToFilter('entity_id', array('in' => $customerListItemIds));

?>

<?php foreach($productCollection as $product): ?>
        <?php if($product->getQty() < $stockThreshold): ?>
            <?php $lowStockCollection[] = $product; ?>
        <?php endif; ?>
<?php endforeach; ?>

1 Answer 1

2

The problem is here:

->addAttributeToFilter('entity_id', array('in' => $customerListItemIds));

By adding this, you are allowing products that are also in the $customerListItemIds array. So if you set a configurable product ID in here then it's going to allow it to be added regardless of your other conditions. You should add a routine that takes any parent ID and grab the children prior to passing it to the collection.

Keep that addAttributeToFilter line in but add this above your $productCollection:

foreach ($customerListItemIds as $customer_item):
    $product=Mage::getModel('catalog/product')->load($customer_item);
    if ($product->getTypeId()=="configurable"):
        $child_ids = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($product->getId());
        foreach ($child_ids as $cid):
            $customerListItemIds[]=$cid; // Adds the child products to the $customerListItemIds if it's a configurable product.
        endforeach;
    endif;
endforeach;
1
  • You star, I never thought about that! Brilliant. I'll give it a try tomorrow. Commented Jan 25, 2015 at 19:45

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.