magento-1.9 – Magento Community Form Key from External Site URL

addtocartcartevent-observermagento-1.9

I have an requirement to allow products to add to the cart from an external link, like in an email campaign. I've created an observer to allow a form key to be added to the url if it doesn't exist, however it only adds if the session already exists (if I visit the site then use the URL it works) otherwise I get a cms no cookies url. Am I using the correct event to hook into for pre-cart but post-session start?

The URL I'm tyring to get working is:

/checkout/cart/add?product=1&qty=1

config.ml:

<?xml version="1.0"?>
<config>
<modules>
    <Vendor_GenerateFormKey>
        <version>1.0.0</version>
    </Vendor_GenerateFormKey>
</modules>
<frontend>
    <events>
        <controller_action_predispatch_checkout_cart_add>
            <observers>
                <Vendor_GenerateFormKey_Model_Observer>
                    <type>singleton</type>
                    <class>Vendor_GenerateFormKey_Model_Observer</class>
                    <method>GenerateFormKey</method>
                </Vendor_GenerateFormKey_Model_Observer>
            </observers>
        </controller_action_predispatch_checkout_cart_add>
    </events>
</frontend>
</config>

Observer.php

<?php

class Vendor_GenerateFormKey_Model_Observer
{
//Put any event as per your requirement
public function GenerateFormKey() {

    $oldkey = Mage::app()->getRequest()->getParam('form_key');

    $domain = Mage::getBaseUrl();

    if (empty($oldkey) && ($domain == 'http://site1.co.uk/' || $domain = 'http:///site2.co.uk/')) {

        Mage::getSingleton("core/session", array("name" => "frontend"));

        $product = Mage::app()->getRequest()->getParam('product');
        $qty = Mage::app()->getRequest()->getParam('qty');

        $key = Mage::getSingleton('core/session')->getFormKey();

        if (empty($product)) { //if no product id, redirect to homepage
            Mage::app()->_redirect('');
        } else { //redirect to the add to cart with the form key

            $url = 'add/product/' . $product . '/qty/' . $qty . '/form_key/' . $key;

            Mage::app()->getRequest()->setParam('return_url', $url);
        }

    }

}
}

I had a look at several 'how tos' for this but I can't seem to find a solution that wants to work with Magento 1.9.2.3. I know the form key doesn't last past the user session, hence the observer to add the form key only once the user is on the site.

Best Answer

Please, try using the code below for the observer. It works fine for me.

public function GenerateFormKey() 
{
    $oldkey = Mage::app()->getRequest()->getParam('form_key');

    $domain = Mage::getBaseUrl();

    if (empty($oldkey) && ($domain == 'http://site1.co.uk/' || $domain = 'http:///site2.co.uk/')) {
        Mage::getSingleton("core/session", array("name" => "frontend"));

        $product = Mage::app()->getRequest()->getParam('product');
        $qty = Mage::app()->getRequest()->getParam('qty');

        $key = Mage::getSingleton('core/session')->getFormKey();

        // Just add the new form key to request params instead of redirecting
        Mage::app()->getRequest()->setParam('form_key', $key);
    }
}

In case you are still receiving "Please enable cookies" error for the action then you have to remove the 'add' action from Mage_Checkout_CartController::$_cookieCheckActions class member.

Related Topic