14

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.

17

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() */);
        }
    }
}
  • 1
    Thanks philwinkle. I'll try and convince the client to go with your first suggestions. I agree that it's probably bad form to hijack a 404. – kayluhb Oct 22 '13 at 12:42

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