Skip to main content

Below is the code in the cron I used. Could be useful to someone. I use an abstract file for my common functions, which is not included here.

<?php

// File shell/setStock.php

require_once 'abstract.php';

class Mage_Shell_setStock extends Mage_Shell_Scripts_AbstractMage_Shell_Abstract {

    public function run() {

        $stockCollection = Mage::getModel('cataloginventory/stock_item')->getCollection()
                        //->addFieldToFilter('is_in_stock', Mage_CatalogInventory_Model_Stock_Status::STATUS_OUT_OF_STOCK)
                        ->addFieldToFilter('type_id', 'configurable')->load(true);
        foreach ($stockCollection as $stockObject) {
            $childStockQty = 0;
            $product = mage::getModel('catalog/product')->load($stockObject->getProductId());
            if ($product->getSku() != '' && $product->getTypeId() == 'configurable') {
                $children = $product->getTypeInstance()->getUsedProducts();
                if (count($children) > 0) {
                    foreach ($children as $child) {
                        $childStock = $child->getStockItem();
                        $childStockQty += $childStock->getQty();
                    }


                    if ($childStockQty > 0 && $product->getIsInStock() == Mage_CatalogInventory_Model_Stock_Status::STATUS_OUT_OF_STOCK) {
                        echo 'Found configurable that is out of stock, but has stock children : setting in stock. ' . $product->getSku() . "\n";
                        mage::log('Found configurable that is out of stock, but has stock children : setting in stock. ' . $product->getSku());
                        if ($this->getArg('dry-run') == false) {
                            $stockObject->setIsInStock(True);
                            $stockObject->save();
                            $this->_doReindexFlag = true;
                        }
                    } elseif ($childStockQty == 0 && $product->getIsInStock() == Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK) {
                        echo 'Found configurable that is in stock , but has no stock children : setting out of stock. ' . $product->getSku() . "\n";
                        mage::log('Found configurable that is in stock , but has no stock children : setting out of stock. ' . $product->getSku());
                        if ($this->getArg('dry-run') == false) {
                            $stockObject->setIsInStock(False);
                            $stockObject->save();
                            $this->_doReindexFlag = true;
                        }
                    }
                } else {
                    if ($product->getSku() != '') {
                        echo 'Found configurable that has no children : setting out of stock. ' . $product->getSku() . "\n";
                        mage::log('Found configurable that has no children : setting out of stock. ' . $product->getSku());
                        if ($this->getArg('dry-run') == false) {
                            $stockObject->setIsInStock(False);
                            $stockObject->save();
                            $this->_doReindexFlag = true;
                        }
                    }
                }
            } else {
                //
            }
        }

        if ($this->_doReindexFlag) {
            mage::log("Re-indexing");
            $this->reindex(array('cataloginventory_stock',
                'catalogsearch_fulltext',
                'catalog_url'));
        }
    }

}

$shell = new Mage_Shell_setStock();
$shell->run();

Below is the code in the cron I used. Could be useful to someone. I use an abstract file for my common functions, which is not included here.

class Mage_Shell_setStock extends Mage_Shell_Scripts_Abstract {

    public function run() {

        $stockCollection = Mage::getModel('cataloginventory/stock_item')->getCollection()
                        //->addFieldToFilter('is_in_stock', Mage_CatalogInventory_Model_Stock_Status::STATUS_OUT_OF_STOCK)
                        ->addFieldToFilter('type_id', 'configurable')->load(true);
        foreach ($stockCollection as $stockObject) {
            $childStockQty = 0;
            $product = mage::getModel('catalog/product')->load($stockObject->getProductId());
            if ($product->getSku() != '' && $product->getTypeId() == 'configurable') {
                $children = $product->getTypeInstance()->getUsedProducts();
                if (count($children) > 0) {
                    foreach ($children as $child) {
                        $childStock = $child->getStockItem();
                        $childStockQty += $childStock->getQty();
                    }


                    if ($childStockQty > 0 && $product->getIsInStock() == Mage_CatalogInventory_Model_Stock_Status::STATUS_OUT_OF_STOCK) {
                        echo 'Found configurable that is out of stock, but has stock children : setting in stock. ' . $product->getSku() . "\n";
                        mage::log('Found configurable that is out of stock, but has stock children : setting in stock. ' . $product->getSku());
                        if ($this->getArg('dry-run') == false) {
                            $stockObject->setIsInStock(True);
                            $stockObject->save();
                            $this->_doReindexFlag = true;
                        }
                    } elseif ($childStockQty == 0 && $product->getIsInStock() == Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK) {
                        echo 'Found configurable that is in stock , but has no stock children : setting out of stock. ' . $product->getSku() . "\n";
                        mage::log('Found configurable that is in stock , but has no stock children : setting out of stock. ' . $product->getSku());
                        if ($this->getArg('dry-run') == false) {
                            $stockObject->setIsInStock(False);
                            $stockObject->save();
                            $this->_doReindexFlag = true;
                        }
                    }
                } else {
                    if ($product->getSku() != '') {
                        echo 'Found configurable that has no children : setting out of stock. ' . $product->getSku() . "\n";
                        mage::log('Found configurable that has no children : setting out of stock. ' . $product->getSku());
                        if ($this->getArg('dry-run') == false) {
                            $stockObject->setIsInStock(False);
                            $stockObject->save();
                            $this->_doReindexFlag = true;
                        }
                    }
                }
            } else {
                //
            }
        }

        if ($this->_doReindexFlag) {
            mage::log("Re-indexing");
            $this->reindex(array('cataloginventory_stock',
                'catalogsearch_fulltext',
                'catalog_url'));
        }
    }

}

$shell = new Mage_Shell_setStock();
$shell->run();

Below is the code in the cron I used.

<?php

// File shell/setStock.php

require_once 'abstract.php';

class Mage_Shell_setStock extends Mage_Shell_Abstract {

    public function run() {

        $stockCollection = Mage::getModel('cataloginventory/stock_item')->getCollection()
                        //->addFieldToFilter('is_in_stock', Mage_CatalogInventory_Model_Stock_Status::STATUS_OUT_OF_STOCK)
                        ->addFieldToFilter('type_id', 'configurable')->load(true);
        foreach ($stockCollection as $stockObject) {
            $childStockQty = 0;
            $product = mage::getModel('catalog/product')->load($stockObject->getProductId());
            if ($product->getSku() != '' && $product->getTypeId() == 'configurable') {
                $children = $product->getTypeInstance()->getUsedProducts();
                if (count($children) > 0) {
                    foreach ($children as $child) {
                        $childStock = $child->getStockItem();
                        $childStockQty += $childStock->getQty();
                    }


                    if ($childStockQty > 0 && $product->getIsInStock() == Mage_CatalogInventory_Model_Stock_Status::STATUS_OUT_OF_STOCK) {
                        echo 'Found configurable that is out of stock, but has stock children : setting in stock. ' . $product->getSku() . "\n";
                        mage::log('Found configurable that is out of stock, but has stock children : setting in stock. ' . $product->getSku());
                        if ($this->getArg('dry-run') == false) {
                            $stockObject->setIsInStock(True);
                            $stockObject->save();
                            $this->_doReindexFlag = true;
                        }
                    } elseif ($childStockQty == 0 && $product->getIsInStock() == Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK) {
                        echo 'Found configurable that is in stock , but has no stock children : setting out of stock. ' . $product->getSku() . "\n";
                        mage::log('Found configurable that is in stock , but has no stock children : setting out of stock. ' . $product->getSku());
                        if ($this->getArg('dry-run') == false) {
                            $stockObject->setIsInStock(False);
                            $stockObject->save();
                            $this->_doReindexFlag = true;
                        }
                    }
                } else {
                    if ($product->getSku() != '') {
                        echo 'Found configurable that has no children : setting out of stock. ' . $product->getSku() . "\n";
                        mage::log('Found configurable that has no children : setting out of stock. ' . $product->getSku());
                        if ($this->getArg('dry-run') == false) {
                            $stockObject->setIsInStock(False);
                            $stockObject->save();
                            $this->_doReindexFlag = true;
                        }
                    }
                }
            } else {
                //
            }
        }

        if ($this->_doReindexFlag) {
            mage::log("Re-indexing");
            $this->reindex(array('cataloginventory_stock',
                'catalogsearch_fulltext',
                'catalog_url'));
        }
    }

}

$shell = new Mage_Shell_setStock();
$shell->run();
added 75 characters in body
Source Link
ProxiBlue
  • 9.9k
  • 3
  • 34
  • 60

I have had the same issue in magento 1.12. Is this maybe a bug? Would be great to hear what magento comes back with.

I have had the same issue in magento 1.12

I have had the same issue in magento 1.12. Is this maybe a bug? Would be great to hear what magento comes back with.

Source Link
ProxiBlue
  • 9.9k
  • 3
  • 34
  • 60

I have had the same issue in magento 1.12

At the time I solved it via a cron script, which would find all configurables, and check the children, and if all out of stock, set the configurable as out of stock. It also did teh reverse. I ran it daily to keep them updated.

Not very elegant,but served its purpose at the time.

A better way would be to hook into an event, when stock is changed, and doing the same type of logic in the observer code.

Below is the code in the cron I used. Could be useful to someone. I use an abstract file for my common functions, which is not included here.

class Mage_Shell_setStock extends Mage_Shell_Scripts_Abstract {

    public function run() {

        $stockCollection = Mage::getModel('cataloginventory/stock_item')->getCollection()
                        //->addFieldToFilter('is_in_stock', Mage_CatalogInventory_Model_Stock_Status::STATUS_OUT_OF_STOCK)
                        ->addFieldToFilter('type_id', 'configurable')->load(true);
        foreach ($stockCollection as $stockObject) {
            $childStockQty = 0;
            $product = mage::getModel('catalog/product')->load($stockObject->getProductId());
            if ($product->getSku() != '' && $product->getTypeId() == 'configurable') {
                $children = $product->getTypeInstance()->getUsedProducts();
                if (count($children) > 0) {
                    foreach ($children as $child) {
                        $childStock = $child->getStockItem();
                        $childStockQty += $childStock->getQty();
                    }


                    if ($childStockQty > 0 && $product->getIsInStock() == Mage_CatalogInventory_Model_Stock_Status::STATUS_OUT_OF_STOCK) {
                        echo 'Found configurable that is out of stock, but has stock children : setting in stock. ' . $product->getSku() . "\n";
                        mage::log('Found configurable that is out of stock, but has stock children : setting in stock. ' . $product->getSku());
                        if ($this->getArg('dry-run') == false) {
                            $stockObject->setIsInStock(True);
                            $stockObject->save();
                            $this->_doReindexFlag = true;
                        }
                    } elseif ($childStockQty == 0 && $product->getIsInStock() == Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK) {
                        echo 'Found configurable that is in stock , but has no stock children : setting out of stock. ' . $product->getSku() . "\n";
                        mage::log('Found configurable that is in stock , but has no stock children : setting out of stock. ' . $product->getSku());
                        if ($this->getArg('dry-run') == false) {
                            $stockObject->setIsInStock(False);
                            $stockObject->save();
                            $this->_doReindexFlag = true;
                        }
                    }
                } else {
                    if ($product->getSku() != '') {
                        echo 'Found configurable that has no children : setting out of stock. ' . $product->getSku() . "\n";
                        mage::log('Found configurable that has no children : setting out of stock. ' . $product->getSku());
                        if ($this->getArg('dry-run') == false) {
                            $stockObject->setIsInStock(False);
                            $stockObject->save();
                            $this->_doReindexFlag = true;
                        }
                    }
                }
            } else {
                //
            }
        }

        if ($this->_doReindexFlag) {
            mage::log("Re-indexing");
            $this->reindex(array('cataloginventory_stock',
                'catalogsearch_fulltext',
                'catalog_url'));
        }
    }

}

$shell = new Mage_Shell_setStock();
$shell->run();