2

I have seen some suggested changes to make to call the FORM KEY to allow the ADD TO CART FROM URL to work.

<?php

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

<form action="/checkout/cart/add/product/<?php echo $productid; ?>" method="post">
    <input type="hidden" name="form_key" value="<?php echo $formKey; ?>" />

    <input type="text" name="qty"> QTY

    <input type="submit" value="Add to basket" />
</form>

However, I don't know which files to change. Can anyone help me?

Many thanks for any help given

  • 1
    Can you explain a bit what are you trying to achieve? An example would help. Your question is unclear. – Marius Oct 2 '14 at 11:11
  • I have a Magento site. However, My colleague has a non magento html site which also sells the same products. He doesn't want to change his site to a Magento site but we want to use the same shopping cart. I want to create an ADD TO CART URL that he can use on his site. At the moment, I can get the shopping cart using mydomain.com/checkout/cart/add?product=13&qty=1 but it is empty – Techiescot Oct 2 '14 at 11:30
  • possible duplicate of Adding to Cart with Querystring - Takes me to homepage? – brentwpeterson Jul 13 '15 at 12:19
  • Not a duplicate question - the issue here is that the external system needs a form key, or the form key needs to be bypassed. – benmarks Jul 13 '15 at 12:48
10

Starting Magento 1.8 it's not possible anymore to add a product to the cart via an external referrer. All the add to cart actions depend on the user form key that is different from session to session.
For version prior to 1.8 you could have done this:

<a href="ROOT_URL/checkout/cart/add/product/ID_HERE">Add</a>

What you can do is to create your own module that has one controller.
Then you can point the urls on your second website to this url, and redirect to the correct add to cart url.
Something like this.

Let's say your module is called Easylife_Cart. You will need the following files.
app/etc/module/Easylife_Cart.xml - the declaration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Cart>
            <codePool>local</codePool>
            <active>true</active>
            <depends>
                <Mage_Checkout />
            </depends>
        </Easylife_Cart>
    </modules>
</config>

app/code/local/Easylife/Cart/etc/config.xml - the configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Cart>
            <version>1.0.0</version>
        </Easylife_Cart>
    </modules>
    <frontend>
        <routers>
            <easylife_cart>
                <use>standard</use>
                <args>
                    <module>Easylife_Cart</module>
                    <frontName>cart</frontName>
                </args>
            </easylife_cart>
        </routers>
    </frontend>
</config>

app/code/local/Easylife/Cart/controllers/IndexController.php - your controller

<?php
class Easylife_Cart_IndexController extends Mage_Core_Controller_Front_Action{
    public function indexAction(){
        $product = $this->getRequest()->getParam('product');
        $key =  Mage::getSingleton('core/session')->getFormKey();;
        if (empty($product)){ //if no product id, redirect to homepage
            $this->_redirect('');
        }
        else{ //redirect to the add to cart with the form key
            $this->_redirect('checkout/cart/add', array('product'=>$product, 'form_key'=>$key));
        }
    }
}

I didn't test it, so watch out for syntax errors.
Now you should be able to put links like this in the external website:

<a href="MAGENTO_WEBSITE_ROOT/cart/index/index/product/ID_HERE">Add to cart</a>

Make sure you clear the cache after adding this module.

  • You are a star ... Thank you ... It works ... Now I just have to work out if there is any way to change the LOGO on the shopping cart depending on which external website it comes from – Techiescot Oct 2 '14 at 12:38
  • If this answers solved your problem mark it as accepted. For your other problem feel free to ask an other question. Let's keep this website clean and play by the rules – Marius Oct 2 '14 at 12:41
  • How would you get this to work in 1.9.2? – Aravona Oct 28 '16 at 11:01
  • How would you add a simple product (size option of configurable product)? - same as selecting a T-shirt in size SMALL on the product page ... but then for /cart/add? thx – snh_nl Nov 9 '18 at 18:26
1

for more information read Adding to Cart with Querystring - Takes me to homepage?

The url for adding needs to include a form_key.

/checkout/cart/add/product/554/form_key/QOTCSV8HqoLA0K3T/

  • 1
    Not the same problem. – benmarks Jul 13 '15 at 12:49

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.