2

Some times Magento will not update the stock of the configurable product if there is any stock change happened to its child products. Usually this happens when some third party modules creating orders on magento.

1 Answer 1

1

I created below script and set the file as a cron job by placing it in magento root.

chdir(dirname(__FILE__));
require_once 'app/Mage.php';
Mage::app();

$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');

//Checking whether the configurable product is out of stock when its simple product is instock
$outofstockConfigurableQuery = "SELECT `cpe`.`entity_id`, `cpe`.`sku`" .
    " FROM `catalog_product_entity` as `cpe`" .
    " LEFT JOIN `cataloginventory_stock_item` as `csc`" .
    " ON `cpe`.`entity_id` = `csc`.`product_id`" .
    " LEFT JOIN `catalog_product_relation` as  `cpr`" .
    " ON `cpr`.`parent_id` = `cpe`.`entity_id`" .
    " LEFT JOIN `cataloginventory_stock_item` as `csi`" .
    " ON `csi`.`product_id` = `cpr`.`child_id` AND `csi`.`is_in_stock`  = 1" .
    " WHERE `cpe`.`type_id` = 'configurable' AND `csc`.`is_in_stock`  = 0" .
    " GROUP BY `cpe`.`entity_id` HAVING SUM(`csi`.`qty`) > 0";

$outofstockproductCollection = $readConnection->fetchAll($outofstockConfigurableQuery);

foreach ($outofstockproductCollection as $_product) {
    $configurableProduct = Mage::getModel('catalog/product')->load($_product['entity_id']);
    $configurableStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($configurableProduct);

    if ($configurableStock->getData('is_in_stock') == 0) {
        $configurableStock->setIsInStock(True);
        $configurableStock->save();
        //To add to reindex queue
        $configurableProduct->save();
        echo "SKU: " . $_product['sku'] . " Stock Status Updated \n";
    }
}

//Checking whether the configurable product is in stock when all of its simple products are out of stock
$instockConfigurableQuery = "SELECT `cpe`.`entity_id`, `cpe`.`sku`," .
    "SUM(`csi`.`qty`) as `quantity`,sum(`csi`.`is_in_stock`) as `in_stock` " .
    " FROM `catalog_product_entity` as `cpe`" .
    " LEFT JOIN `cataloginventory_stock_item` as `csc`" .
    " ON `cpe`.`entity_id` = `csc`.`product_id`" .
    " LEFT JOIN `catalog_product_relation` as  `cpr`" .
    " ON `cpr`.`parent_id` = `cpe`.`entity_id`" .
    " LEFT JOIN `cataloginventory_stock_item` as `csi`" .
    " ON `csi`.`product_id` = `cpr`.`child_id` " .
    " WHERE `cpe`.`type_id` = 'configurable' AND `csc`.`is_in_stock`  = 1" .
    " GROUP BY `cpe`.`entity_id` HAVING SUM(`csi`.`is_in_stock`) = 0";

$instockProductCollection = $readConnection->fetchAll($instockConfigurableQuery);
foreach ($instockProductCollection as $_product) {
    $configurableStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product['entity_id']);
    if ($configurableStock->getData('is_in_stock') == 1) {
        $configurableStock->setIsInStock(false);
        $configurableStock->save();
        echo "SKU: " . $_product['entity_id'] . " Stock Status Updated \n";
    }
}

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.