1

I have the following script which I run to tell me if there are any configurable products the have their stock availability setting set incorrectly.

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

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

foreach ($collectionConfigurable as $_configurableproduct) {
    /**
    * Load product by product id
    */
    $product = Mage::getModel('catalog/product')->load($_configurableproduct->getId());

    /**
    * Process All Products
    */




        /**
        * Get children products (all associated children products data)
        */
        $childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);

        $stockItem = $product->getStockItem();

        if($stockItem->getIsInStock()){
            /**
            * All configurable products, which are in stock
            */

            $instock_childrenisinstock = false;

            foreach ($childProducts as $childProduct) {
                $qty = Mage::getModel('cataloginventory/stock_item')->loadByProduct($childProduct)->getQty();
                if ($qty > 0) {
                    $instock_childrenisinstock = true;
                }
            }
            if (!$instock_childrenisinstock) {
                echo "<p>".$product->getName()." (".$product->getId().") is <span style='font-weight: bold;color: red'>IN STOCK<span></p>";

            }

        } else {
            /**
            * All configurable products, which are out of stock
            */

            $outofstock_childrenisinstock = false;

            foreach ($childProducts as $childProduct) {
                $qty = Mage::getModel('cataloginventory/stock_item')->loadByProduct($childProduct)->getQty();
                if ($qty > 0) {
                    $outofstock_childrenisinstock = true;
                }
            }
            if ($outofstock_childrenisinstock) {
                echo "<p>".$product->getName()." (".$product->getId().") is <span style='font-weight: bold;color: red'>OUT OF STOCK<span></p>";

            }
        }

}

?>

This will list on screen any configurable products that are set incorrectly. I have another very similar script which runs on cron to send me an email if the script has a result. So my process is I receive a notification email(from cron job) I run the above script and then change the products manually through the magento backend.

Can I alter this script so that it runs on a cron job and automatically updates the configurable products? It seems like it should be easy but I'm a complete php noob so I'm looking for some help.

My thoughts are the code:

echo "<p>".$product->getName()." (".$product->getId().") is <span style='font-weight: bold;color: red'>IN STOCK<span></p>";

Should be replaced with something like: product(ID) set to out of stock But I have no idea what the code is for this.

Cheers

New Code From Nidheesh Implemented:

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

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

foreach ($collectionConfigurable as $_configurableproduct) {
    /**
    * Load product by product id
    */
    $product = Mage::getModel('catalog/product')->load($_configurableproduct->getId());

    /**
    * Process All Products
    */




        /**
        * Get children products (all associated children products data)
        */
        $childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);

        $stockItem = $product->getStockItem();

        if($stockItem->getIsInStock()){
            /**
            * All configurable products, which are in stock
            */

            $instock_childrenisinstock = false;

            foreach ($childProducts as $childProduct) {
                $qty = Mage::getModel('cataloginventory/stock_item')->loadByProduct($childProduct)->getQty();
                if ($qty > 0) {
                    $instock_childrenisinstock = true;
                }
            }

            // Addition From @Nidheesh
            $stockItem->setIsInStock(true);
            $stockItem->save();
            //To Make sure reindex is running
            $product->save();

        } else {
            /**
            * All configurable products, which are out of stock
            */

            $outofstock_childrenisinstock = false;


            foreach ($childProducts as $childProduct) {
        $qty = Mage::getModel('cataloginventory/stock_item')->loadByProduct($childProduct)->getQty();
        if ($qty > 0) {
            $instock_childrenisinstock = true;

            break; // Addition From @Nidheesh
        }
    }
            // Addition From @Nidheesh
            $stockItem->setIsInStock(false);
            $stockItem->save();
            //To Make sure reindex is running
            $product->save();
        }

}

?>
1
  • Tried the above script as suggested by Nidheesh but I get no result. Doesn't return anything.
    – Pragman
    Commented Sep 28, 2016 at 8:23

1 Answer 1

2

The complete script will looks like this:

<?php

require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();

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

foreach ($collectionConfigurable as $_configurableproduct) {
    /**
     * Load product by product id
     */
    $product = Mage::getModel('catalog/product')->load($_configurableproduct->getId());

    /**
     * Process All Products
     */
    /**
     * Get children products (all associated children products data)
     */
    $childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $product);

    $stockItem = $product->getStockItem();

    if (!$stockItem->getIsInStock()) {
        /**
         * All configurable products, which are in stock
         */
        $instock_childrenisinstock = false;

        foreach ($childProducts as $childProduct) {
            $qty = Mage::getModel('cataloginventory/stock_item')->loadByProduct($childProduct)->getQty();
            if ($qty > 0) {
                $instock_childrenisinstock = true;
                break;
            }
        }
        if ($instock_childrenisinstock) {
            $stockItem->setIsInStock(true);
            $stockItem->save();
            //To Make sure reindex is running
            $product->save();
        }
    } else {
        /**
         * All configurable products, which are out of stock
         */
        $outofstock_childrenisinstock = false;


        foreach ($childProducts as $childProduct) {
            $qty = Mage::getModel('cataloginventory/stock_item')->loadByProduct($childProduct)->getQty();
            if ($qty > 0) {
                $instock_childrenisinstock = true;

                break; 
            }
        }
        if (!$instock_childrenisinstock) {
            $stockItem->setIsInStock(false);
            $stockItem->save();
            //To Make sure reindex is running
            $product->save();
        }
    }
}
?>
10
  • @ZUBU Check and let me know if any problem
    – Nidheesh
    Commented Sep 28, 2016 at 5:41
  • I'll give both of these a try. Is the alternative method a php script? The code looks very similar
    – Pragman
    Commented Sep 28, 2016 at 6:38
  • Yes, it's also a php script but using direct sql queries to get the instock/out of stock config products to speed up the process than using collection
    – Nidheesh
    Commented Sep 28, 2016 at 6:41
  • Hi @Nidheesh I have tried the above code and it seems to run without any errors but it is not changing the status of any products. No Errors it just runs for about 2-3 mins(Using Putty). Any Ideas?? Also tried running the other script (can't comment there as I dont have enough rep but I'm getting a syntax error. Don't know if I should post about that question here??
    – Pragman
    Commented Sep 28, 2016 at 7:46
  • Better you paste the syntax error here
    – Nidheesh
    Commented Sep 28, 2016 at 8:56

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.