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; ?>