Magento: Get the add to cart url of a product

First, create the product, then pass this product object to the checkout/cart helper to get the add to cart url.

$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

echo Mage::helper('checkout/cart')->getAddUrl($product);//Get the add to cart url

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.


Search within Codexpedia

Custom Search

Search the entire web

Custom Search