Automatic 301 Redirects for Disabled Products in Magento CE 1.7.0.2

ce-1.7.0.2productredirect

I'm trying to add automatic redirects for a custom Product Type (Event) in Magento 1.7. When an event date has passed, I am marking the product as disabled so users don't go to the wrong page, and I also want to redirect to the list of future classes so I don't have a plethora of 404 errors on my site.

Is there a way to listen to an event for getting the product where the 404 would get thrown, or should I look for a free extension for this somewhere? I've been searching for a while and haven't seen any extensions that work well.

Best Answer

Welcome to Magento.StackExchange!

Unfortunately, there is no out-of-box module that will do this for you.

I'd highly encourage you to reconsider this as an option. Your users are not stupid (no matter how stupidly they behave) - many, many years of eCommerce analysis has shown me that, nearly 80% of the time, users reaching a 404 will hit the on-site-search bar within seconds. Put your effort into fixing up your site search with better categorization (read: keyword stuffing).

Google indexes aren't forever. If you're afraid of 404s, consider sprucing up your 404 page itself. Increase your conversion potential by offering a 5-10% discount coupon to those inconvenienced by hitting a 404. Or, better yet, don't disable product pages. Rather, disable the ability to purchase (e.g. set as out of stock) and provide a static block / link that links to the relevant page/category.

I don't care, just give me codes because internets:

At it's simplest coding a module with an observer that will handle this for you is trivial:

Event designation in config.xml:

<global>
    <events>
        <controller_action_postdispatch_catalog_product_view>
            <observers>
                <yourmodule_capcpv>
                    <class>YourCompany_YourModule_Model_Observer</class>
                    <method>catalogProductViewPostdispatch</method>
                </yourmodule_capcpv>
            </observers>
        </controller_action_postdispatch_catalog_product_view>
    </events>
</global>

app/code/local/YourCompany/YourModule/Model/Observer.php:

<?php

class YourCompany_YourModule_Model_Observer
{
    public function catalogProductViewPostdispatch($observer)
    {
        $controller = $observer->getEvent()->getControllerAction();

        $product = Mage::registry('current_product');
        if($product->getStatus()!=Mage_Catalog_Model_Product_Status::STATUS_ENABLED){
            $action->getResponse()->setRedirect(/* your redirect URL here with Mage::getUrl() */);
        }
    }
}
Related Topic