0

Created a simple module to display Promoted product in drop-down menu. Now I would like to have a button "Add to cart/basket" to add this product immediately. Where is this method or how to do it ?

Rob D. A.
  • 142
  • 1
  • 11

1 Answers1

1

Method 1:

If you are in a block which extends Mage_Catalog_Block_Product_Abstract, you can use this line of code in the block class itself or in the phtml template file of this block to get the add to cart url.

<button type="button" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Add to Cart')) ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_item) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>

Method 2:

Pass product object to checkout/cart helper to get the add to cart url. Then the add to cart link address will be:

        $product=Mage::getModel('catalog/product')->load($productId);//load the product by product id
        $product=Mage::getModel('catalog/product')->loadByAttribute('sku',$skuNum);//or load the product by sku number
        $product=Mage::getModel('catalog/product')->setStoreId($storeId)->loadByAttribute('sku',$skuNum);//or load the product from a given store id
<a href="<?php echo $this->helper('checkout/cart')->getAddUrl($product);?>">Add to cart</a> //Get the add to cart url
Shahed Jamal
  • 106
  • 5