0

I'm just wondering if there is a way to set my products as out of stock if they haven't been updated within a day?

Any method will do, I am importing products via CSV and if a product isn't in the CSV but is in my products database I want it to appear as out of stock.

Any idea how I can do this?

Thanks

1

If you're happy with your stock to be updated at a specific time of the day, I'd suggest a scheduled (corn) task to look for all products not in the CSV and then update the stock for those items.

First, get a list of all the IDs. I'm assuming the IDs are in your CSV? But this may be more involved if you want to match by SKU for example. Once you have these IDs, you can run something like:

$ids = array(1,2); // assuming this is being built from your CSV

$_collection = Mage::getModel("catalog/product")->getCollection()->addAttributeToFilter('id', array('nin' => $ids));

foreach($_collection as $_product) {
    $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product->getId());
    $stockItem->setData('product_id', $product->getId());
    $stockItem->setData('qty', 0); // might want to set stock to 0?
    $stockItem->setData('is_in_stock', 0);
    $stockItem->save();
}
  • Hi Douglas, I have a few questions about this if you could spare some time for a fellow Yorkshireman? Basically would I put this in it's own PHP file anywhere on my server to cron it or does this block need inserting somewhere specific? Also I'm just wondering what $ids = array(1,2); is actually doing here could you explain it a little more? Is that the CSV columns listed? Thanks! – Uncle Kracker May 13 '15 at 20:58
  • Hi Nick, It could either be a module, or a standalone script if you'd prefer. The list of IDs (in this instance) are Magento's internal product IDs that might be in your CSV. You could probably use SKUs or somesuch if you prefered. This is then used in the collection line so that you get all products NOT in that list (nin). – Douglas Radburn May 14 '15 at 15:49

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.