2

I've imported my products with a custom script but whatever I tried, I couldn't get the script to set the stock data so I decided to do it afterwards. Whenever I try to do it in the Magento back end with the change attributes option I get a Integrity constraint violation since there are no records from those products in the table yet. How can I mass update the stock status?

Eddie
  • 227
  • 6
  • 14

1 Answers1

1

This being an old question, for anyone who runs into this: I had this problem a while ago and wrote a small script. Because the store in question had over 6000 products on a not-so-great server, I did this using a "per 500" products:

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSort('entity_id', 'desc')
            ->addStoreFilter()
            ->setPage(5, 500);
    foreach($products as $product){
        $product = Mage::getModel('catalog/product')->load($product->getId());
        $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
        if (!$stockItem->getId()) {
           $stockItem->setData('product_id', $product->getId());
           $stockItem->setData('stock_id', 1); 
        }
        $stockItem->setData('use_config_manage_stock', 1);
        $stockItem->setData('is_in_stock', 1); 
        $stockItem->setData('manage_stock', 0);
        $stockItem->save(); 
    }

Some explanation: setPage(5, 500) is for the 5th run of 500 products in this case. I reload the product in the first line of the foreach just to be sure I have the full and correctly loaded product to work with. The if (!$stockItem->getId()) checks if there is stock data (if not, it adds it) and afterwards the conditions are set.

Isolde
  • 441
  • 3
  • 13