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();
}
}
?>