I am currently using Magento

I want to add a product to the cart by URL execution.

I am trying the solutions pointed here: http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/catalog/adding_a_product_to_the_cart_via_querystring but none of them works.

Any ideas how i can make it ?

Thanks in advance!

  • there have also comment see. As of version 1.3.X, it appears that adding a product to the cart by a URL no longer works. – ravi patel Aug 30 '14 at 11:31

If you want to do this on a CMS page, here's a simple approach:

On the CMS page, add the following content:

<a href="/checkout/cart/add?product=<product_id>&qty=<qty>&form_key={{block type="core/template" name="getformkey" template="getformkey.phtml"}}">Add to cart</a>

(Replace <product_id> and <qty> with the desired values.)

Now create a new template to at app/design/package/theme/template/getformkey.phtml:

<?php echo Mage::getSingleton('core/session')->getFormKey();

Your CMS page will now be able to create an "Add to cart" link with the correct form key in the front-end, for example:

| improve this answer | |
  • I added the product to cart and it worked. However, I tried to add the product again and it does not add to cart. The Form Key is the same. After I flush cache storage, the form key changes than I can add to cart successfully. How can we change the form without flushig storage cache? – Elvis Mar 24 '17 at 13:06
  • I'm not sure what happens if you try to add the product twice via the same URL. Perhaps a better solution would be to set the quantity to "2" when first adding it? – Louis B. Mar 24 '17 at 16:35
  • Why 2? Its working now. However, I see it now working. When I clear storage cache, the form key changes. – Elvis Mar 24 '17 at 18:49

Based on the answer of @louis-b I used it in php which works. You can use this as href of with setLocation in javascript.

<?php echo Mage::getUrl('checkout/cart/add', array('product'=>$_item->getId(),'qty'=>1, 'form_key' => Mage::getSingleton('core/session')->getFormKey())) ?>
| improve this answer | |

As of Magento 1.8 you need to include a form_key along with the post.

Inside your php call the following to retrieve the form key:


| improve this answer | |

Tried This?


$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" />
| improve this answer | |
  • If it's for a template you can use echo $this->getBlockHtml('formkey') to output a whole hidden input element. – clockworkgeek Aug 30 '14 at 10:12

Try this, it worked for me

getSubmitUrl($_product, array('_secure' => $this->_isSecure())) ?>" method="post" >

        <?php echo $this->getBlockHtml('formkey') ?>
        <div class="no-display">
            <input type="hidden" name="product" value="<?php echo $_product->getId() ?>" />
            <input type="hidden" name="related_product" id="related-products-field" value="" />
        <input type="submit" value="Add to basket" />
| improve this answer | |
<form action="<?php echo$this->_getUrl('checkout/cart/add', $routeParams) ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>

 <input name="qty" value="  1" size="4" title="<?php echo $this->__('Qty') ?>" class="input-text qty" maxlength="12" id="cart[<?php echo $_product->getId() ?>][qty]"/>

                     <button type="button" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Add+')) ?>" onclick="this.form.submit()" id="product-addtocart-button" class="button btn-cart" ><span><span><?php echo $this->__('Add+') ?></span></span></button>


 $routeParams = array(
            /*Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->_getHelperInstance('core')
       $routeParams = array(       
            Mage_Core_Model_Url::FORM_KEY => $this->_getSingletonModel('core/session')->getFormKey()


| improve this answer | |
  • Some explanation of your answer would be helpful. – brentwpeterson Sep 14 '15 at 14:36

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.