1

I'd like to have items automatically change to disabled once they become "out of stock".

The default options simply remove the add to cart button but the product is still viewable if you have the products URL. This causes issues as products are still showing up in Google shopping, costing my client unnecessarily as customers are clicking through to the product, seeing it's unavailable and leaving.

Is there a way this can be done?

I'm running community edition 1.9.1.0.

1
  • Mostly for SEO and UX reasons, this is not recommended if you have products that are continually going in and out of stock. If you are running a store that is a one-time product that most likely will not appear on the site again, then auto-disabling is fine.
    – Phil M
    Commented Nov 10, 2016 at 21:37

3 Answers 3

0

Yes, this is true, Magento even notifies you that: "Products will still be shown by direct product URLs".

What you can do is to use the event <catalog_controller_product_view> and if the product is out of stock then redirect to $this->_redirect('noroute'); which is the 404 page, otherwise, do nothing (return $this;), so the product page will be displayed like in the normal case.

I hope it helps!

2
  • Thanks, but this wouldn't stop the product from showing in Google. I need the product to be completely disabled if out of stock. Commented Nov 10, 2016 at 14:07
  • Sorry, now I read it once again and I skipped the part where you mentioned: I'd like to have items automatically change to disabled once they become "out of stock". My answer was only a solution for not showing the product to the customer if he is trying to access it directly through the URL.
    – Tudor
    Commented Nov 10, 2016 at 14:08
0

This issue can be resolved with a module. Create a new module with the following files:

/app/code/local/MyCompany/Deactivateproducts/Model/Observer.php

<?php
class MyCompany_Deactivateproducts_Model_Observer
{
    public function salesOrderSaveAfter($observer)
    {

        $storeId = 0; //the admin store view, change this if you want to disable only for the store view from which the order came
        $order= $observer->getEvent()->getOrder();

        foreach ($order->getItemsCollection() as $item) {


            $stockQty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($item->getProductId())->getQty();

            if ($stockQty == 0) {
                Mage::getModel('catalog/product_status')->updateProductStatus($item->getProductId(), $storeId, Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
            }
        }

    }
}

/app/code/local/MyCompany/Deactivateproducts/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <MyCompany_Deactivateproducts>
            <version>0.1.0</version>
        </MyCompany_Deactivateproducts>
    </modules>

    <frontend>
        <events>
            <sales_order_save_after>
                <observers>
                    <deactivateproducts>
                        <type>singleton</type>
                        <class>MyCompany_Deactivateproducts_Model_Observer</class>
                        <method>salesOrderSaveAfter</method>
                    </deactivateproducts>
                </observers>
            </sales_order_save_after>
        </events>
        <routers>
            <MyCompany_Deactivateproducts>
                <use>standard</use>
                <args>
                    <module>MyModule_Deactivateproducts</module>
                    <frontName>Deactivateproducts</frontName>
                </args>
            </MyCompany_Deactivateproducts>
        </routers>
    </frontend>
</config>

Also don't forget to create the /app/etc/modules/MyCompany_Deactivateproducts.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <MyCompany_Deactivateproducts>
            <active>true</active>
            <codePool>local</codePool>
        </MyCompany_Deactivateproducts>
    </modules>
</config>

And replace "MyCompany" string from the above code with the name of your Package/Company

3
  • Personally, I would avoid working on anything during the Sales Order process unless it relates directly to the order — avoiding added complexity on checkout. Instead I would set this up as a CRON every X minutes. Its more effort to get the collection, and the results would not be immediate... but its much better than risking an error in your code would prevent an order from being placed.
    – Phil M
    Commented Nov 10, 2016 at 21:35
  • @PhilMobley any suggestions on how to do this? Commented Nov 22, 2016 at 16:40
  • @sMiLEysLOth You can configure the cron to run every 5 minutes. Take a look at this simple example: inchoo.net/magento/creating-cron-script-in-magento. The code that the cron runs would be more along the lines of loading a collection of products that match your stock requirements (out of stock). Check out the answer here for how to do that: magento.stackexchange.com/questions/88699/…
    – Phil M
    Commented Nov 22, 2016 at 21:43
0

If your intent is to HIDE products that are no longer in stock, this is a feature built into default Magento. In the Admin, go to System > Configuration > Inventory (Catalog Section). Then under the "Stock Options" group set the option "Display Out of Stock Products" to "No".

This does not actually disable those products. If you still want that as an automated feature you will need to create a module to handle that.

However, if your only goal is to prevent the products from appearing on the website then this option is designed just for that purpose. (You could always mass disable products of QTY = 0 from the Admin Product Grid on a daily/weekly basis.)

1
  • As stated in my original question, my intent is to completely disable the product. The default feature simply removes the product from the catalog so you can't see the product on the website. If you come from outside the website you'll still be able to get to the page if you have the url, you just won't be able to add the product to the cart. This is what I want to prevent. I believe a custom module is the route I need to go down. Was just hoping there was an easier way. Commented Nov 23, 2016 at 10:01

Your Answer

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

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