1

Since I updated our version of Magento to 1.9 every time I click the Add to Card button on the category view page I get redirected to the empty shopping cart. What have we done wrong while updating?

4
  • have you use any custom theme? or default Magento
    – Keyul Shah
    Feb 20, 2015 at 5:00
  • 1
    Is your session key formed?
    – versedi
    Feb 20, 2015 at 5:38
  • i already done this step: Go to: Magento backend -> Sytem -> Configuration -> Web -> Session and Cookie Management Set cookie lifetime to 86400 but still doent work. :( Feb 23, 2015 at 2:52
  • yes i have used custom theme. Feb 23, 2015 at 2:53

4 Answers 4

2

The best way to upgrade your custom theme is to look at the same template file in base/default - with a bit of knowledge on what to look for.

I'm assuming you upgraded from before 1.7.x or lower and are now facing the form key issue. That means you're looking for any code that puts a form key in your template files.

The templates affected are:

  • customer/form/newsletter.phtml
  • customer/form/edit.phtml
  • customer/address/edit.phtml
  • wishlist/sharing.phtml
  • wishlist/view.phtml
  • sendfriend/send.phtml

Look for the string Nasir posted.

On all other pages, the template doesn't matter. It uses various calls to standard URL objects and finally incorporates the method Mage_Core_Model_Session::getFormKey() into the URL.

Various stores I've seen have a local copy of Mage_Core_Model_Session for various reasons in app/codel/local/Mage/Core/Model/Session.php which is missed when upgrading. Check if this file is present and if it contains the method getFormKey() and compare that to the code in app/code/core/Mage/Core/Model/Session.php.

The reason the absence of this method doesn't break your site is because any method not defined that starts with get (or set, or has or uns), is routed to __call() and implemented as a getter (or setter or hasAttribute or unsetter) for data contained on the object it is operating on. If no such data can be found it simple returns null, meaning no formkey is set.

This then breaks code in Mage_Core_Controller_Varien_Action.php::_validateFormKey(), which verifies if a form key is passed to the page and if so it matches the form key set on the user session.

2

add this code to starting of addtocart.phtml file

$_product = $this->getProduct(); 

change the onclick on addtocart button

onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($_product) ?>')"
1

Try using the following code inside your <form></form> tag

<?php echo $this->getBlockHtml('formkey') ?>
3
  • which file should i update? Feb 23, 2015 at 2:53
  • design/frontend/your_package/your_theme/template/catalog/product/view.phtml Feb 23, 2015 at 5:39
  • add to cart button is ok in product view page... add to cart button is not working on category view page. Feb 24, 2015 at 1:46
1

Well I think you have problem in your product list page, wherein you're not getting a product's addtocart url properly, or url params aren't passed properly in the generated addtocart url of products.

Assuming you still have an object like $_product from loop (foreach($collection as $_product)), available, use it to load the product anywhere in page before the foreach loop I described ends, just like:

$product = Mage::getModel('catalog/product')->load($_product->getId());

Then get the it's addtocart url as following:

$productCartUrl = Mage::helper('checkout/cart')->getAddUrl($product);

and finally replace the addtocart button which isn't working proper with below:

<?php if($_product->isSaleable()): ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $productCartUrl ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>

Hope this helps you resolve your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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