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?

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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