Magento 1.7 Cart – Difference Between getAddUrl and getAddToCartUrl Functions

cartmagento-1.7

I have found two function returning the exactly same value.

getAddUrl in \app\code\local\Mage\Checkout\Helper\Cart.php

getAddToCartUrl in \app\code\local\Mage\Catalog\Block\Product\Abstract.php

what is really the difference between these two, as I am adding an add to cart button in my block.
Which function should I use to add the product to the cart ?

Best Answer

The code under Mage_Catalog_Block_Product_Abstract has a very useful comment.

/**
 * Retrieve url for add product to cart
 * Will return product view page URL if product has required options
 *
 * @param Mage_Catalog_Model_Product $product
 * @param array $additional
 * @return string
 */
public function getAddToCartUrl($product, $additional = array())
{
    if ($product->getTypeInstance(true)->hasRequiredOptions($product)) {
        if (!isset($additional['_escape'])) {
            $additional['_escape'] = true;
        }
        if (!isset($additional['_query'])) {
            $additional['_query'] = array();
        }
        $additional['_query']['options'] = 'cart';

        return $this->getProductUrl($product, $additional);
    }
    return $this->helper('checkout/cart')->getAddUrl($product, $additional);
}

The difference is that the Mage_Catalog_Block_Product_Abstract::getAddToCartUrl will set the url as the product view page if the product has required options, otherwise it will just return Mage_Checkout_Helper_Cart::getAddUrl.

The getAddToCartUrl can be used when you are not on the product view page, say for the wishlist page. So that you do not get an error when the user tries to add a product to the cart that has required options.

Related Topic