0

I realized I have a lot of products that have quantity above 0 but are not in stock. So I would like to do a search in my database for products that have quantiy > 0 and stock_status 0, this is the code I am using:

UPDATE `cataloginventory_stock_status` SET `stock_status` = replace(stock_status, '0', '1') WHERE qty > '0.0000' AND stock_status = '0'

While this will find all products and change stock_status in the database, the products stock status will not change. I also updated cataloginventory_stock_status_idx but to no avail. Are there other tables I need to set stock status?

1 Answer 1

0

You can do this using Magento code also.

First find all products, which have qty>0 and stock_status=0 and set stock status using code below:

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

$productCollection = Mage::getModel('catalog/product')
     ->getCollection()
     ->addAttributeToSelect('*')
     ->joinField('qty',
                 'cataloginventory/stock_item',
                 'qty',
                 'product_id=entity_id',
                 '{{table}}.is_in_stock=0',
                 'left')
     ->addAttributeToFilter('qty', array("gt" => 0));

foreach($productCollection as $product) { //print_r($product->getData());exit;
    $_product = Mage::getModel('catalog/product')->load($product->getId());
    $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
    $stockItem->setData('is_in_stock', 1); // is 0 or 1
    $stock_item->setData('manage_stock', 1);
    try {
        $stock_item->save();
    } catch (Exception $e) {
        echo "{$e}";
    }
}
echo 'Done';
?>

Please let me know if you find any problem.

1
  • This isn't doing anything for me I'm afraid
    – loeffel
    Aug 22, 2016 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.