1

In my catalog I have affiliate products that when someone clicks the “Add to Cart” button, they should get forwarded to an external URL. Does anyone have any ideas how I could do that? I am new to Magento.

1

Overview

This can be done using an event observer in conjunction with product attribute. For the purposes of this answer, I will assume you can follow the linked guides and find other resources/answers about creating event observers and product attributes.

Solution

You could create a new product attribute called external_link which you will fill in for your products that have external links. Then by observing the controller_action_predispatch_checkout_cart_add event, check whether the product has a value for that attribute and redirect the customer if so. Something like this would do the trick:

etc/config.xml

Under config/frontend/events:

<controller_action_predispatch_checkout_cart_add>
    <observers>
        <redirectExternalLink>
            <class>My_AffiliateProducts_Model_Observer</class>
            <method>redirectExternalLink</method>
        </redirectExternalLink>
    </observers>
</controller_action_predispatch_checkout_cart_add>

Model/Observer.php

class My_AffiliateProducts_Model_Observer
{
    public function redirectExternalLink(Varien_Event_Observer $observer)
    {
        $productId = Mage::app()->getRequest()->getParam('product', 0);
        $product = Mage::getModel('catalog/product')->load($productId);
        // Check for value set on this product for external_link attribute.
        if ($url = $product->getExternalLink()) {
            Mage::app()->getResponse()->setRedirect($url);
            Mage::app()->getResponse()->sendResponse();
            exit;
        }
        return $this;
    }
}
| improve this answer | |
  • thank you so much sir for your response.... i tried it but i am not able to redirect.. dropbox.com/s/qpzge60r69t44h4/app.zip?dl=0 here i have attached my code please review it ... i can't find my mistake @fantasticrice – Mild Milly May 1 '15 at 4:16
  • @MildMilly - Have you confirmed that the extension is set up properly and the observer is firing? Your code did not include the module definition, so I am just curious what debugging steps you have attempted? This method worked fine when I did a proof of concept myself, but I am not going to walk through every detail of creating your extension, there are plenty of other resources that go into detail on this subject. – fantasticrice May 1 '15 at 16:21
  • thanks a lot sir ... with your guidance only got my results now ... i made some silly mistakes ... thank you so much sir @fantasticrice – Mild Milly May 1 '15 at 16:52
  • @MildMilly - Glad to hear, well done. Please mark the answer as accepted to help others. – fantasticrice May 1 '15 at 18:05

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.