1

I've just upgraded from 1.6.2.0 to the latest 1.9.2.4. I've noticed an issue on product list pages, where the Add to Cart button now doesn't actually add the product to the cart - it simply takes you to the product detail page.

This is because in the onclick event the query string (options=cart) is missing. I have tracked this down and it seems to be as a result of some changes to Mage_Catalog_Model_Product_Url->getUrl(), whereby a product's URL is generated once and subsequently is retrieved from a stored version. This is an issue because query parameters passed through to that method have no effect!

Here's the onclick event:

onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"

It seems to be identical to the base theme's markup.

Am I missing something or is this a bug? And what would be the best way of remedying it?

1

if the product is configurable product or you set custom option on the product than it redirect to the product view page. you check this things.

  • It redirects to this page for all products. For configurable products I expect to see a warning message telling me to choose options, but this doesn't appear (because the query string is not appended to the URL). – piemanji Sep 23 '16 at 12:53
1

getAddToCartUrl() is method of this Mage_Catalog_Block_Product_Abstract class. and this class extend by only Mage_Catalog_Block_Product so you can not access this method on your category page.

this method contains below code

public function getAddToCartUrl($product, $additional = array())
{
    if (!$product->getTypeInstance(true)->hasRequiredOptions($product)) {
        return $this->helper('checkout/cart')->getAddUrl($product, $additional);
    }
    $additional = array_merge(
        $additional,
        array(Mage_Core_Model_Url::FORM_KEY => $this->_getSingletonModel('core/session')->getFormKey())
    );
    if (!isset($additional['_escape'])) {
        $additional['_escape'] = true;
    }
    if (!isset($additional['_query'])) {
        $additional['_query'] = array();
    }
    $additional['_query']['options'] = 'cart';
    return $this->getProductUrl($product, $additional);
}

I am not sure but if you want to use this function at category page you can use by following

<?php echo $this->getLayout()->createBlock('catalog/product')->setProduct($product)->getAddToCartUrl($product)->toHtml() ?>

I am not sure you can use above code directly in category page as this method is part of Abstract class

  • Thanks Murutuza, I'm not sure this is the problem though, since the method in question follows the base theme and does function correctly if it is first call to get a product URL. I did find a solution though, see my answer. – piemanji Sep 26 '16 at 7:42
1

The solution ended up being replacing

<?php echo $this->getAddToCartUrl($_product) ?>

with

<?php echo Mage::helper('checkout/cart')->getAddUrl($_product) ?>

- however it isn't implemented this way in the standard template files, so still not sure whether there's an issue elsewhere in my project or it's just a bug.

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.