Php Magento list all configurable products out with simple products out of stock

filtermagentoPHP

We have made an xml file for our products and we have a problem.

In our Magento shop all configurable products stay in stock also if simple products have a qty of 0. We need this function for our administration / stock system. We have a filter set that filters out all products that are not in stock (see below). Now we also need a filter for configurable products that are in stock, but the simple products are out of stock and have a qty of 0.

Filter to not show out of stock products

->joinField('is_in_stock',
                'cataloginventory/stock_item',
                'is_in_stock',
                'product_id=entity_id',
                'is_in_stock=1',
                '{{table}}.stock_id=1',
                'left');

        $products->addAttributeToFilter('is_in_stock', array('eq' => 1));

So example:

Configurable product: is in stock
Simple 1, simple 2, simple 3: are out of stock

We need to filter out the configurable product with all simple products with status out of stock or qty0

It would be great if someone has the answer because we just do not see the solution.

Best Answer

Here is a script with which you can find all those configurables that do not have simples with stock. Afterwards you could then use that array of Ids to exclude them in your filter.

<?php
require_once('app/Mage.php');
umask(0);
Mage::app();

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

$outOfStockConfis = array();
foreach ($collectionConfigurable as $_configurableproduct) {
    $product = Mage::getModel('catalog/product')->load($_configurableproduct->getId());
    if (!$product->getData('is_salable')) {
       $outOfStockConfis[] = $product->getId();
    }
}

Then use this to remove the configurable products which are not saleable from your collection:

$products->addAttributeToFilter('entity_id',array('nin' => $outOfStockConfis));
Related Topic