Get number of configurable products with at least one in-stock associated product
-
31-10-2019 - |
سؤال
I used following code to retrieve number of in-stock associated products in a category ($cat) for a huge store with thousands of products.
$cnt = 0;
$prods = $cat->getProductCollection()
->addAttributeToSelect('entity_id')
->addAttributeToFilter('status',Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
->addAttributeToFilter('visibility', array(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG))
->addAttributeToFilter('type_id', 'configurable');
foreach( $prods as $product) {
$simpleProducts = Mage::getModel('catalog/product_type_configurable')
->getUsedProducts(null,$product);
foreach( $simpleProducts as $simple ){
$cvc = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simple);
if($cvc->getIsInStock() && $cvc->getQty() > 0) {
$cnt++;
break;
}
}
}
Above code works but its really slow. I mean it takes almost half an hour to calculate number of products for all categories in my store. What's the problem ? I know I can use caching (and I already did) but the question is that how can I make the code faster or what I missed in the above code that made it really slow ?
UPDATE
I want to count all configurable products with at least one simple product in stock. I don't care about configurable product stock by it self, only its associated products matter because in my store configurable product's in-stock availability don't matter. Only its associated products is important. I.e: if a configurable have 3 in-stock products, we should count it once.
لا يوجد حل صحيح