Magento:Get the add to cart url of a product

Magento:获取产品的添加到购物车的URL

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

首先,创建产品,然后将此产品对象传递给结帐/购物车帮助程序以获取添加到购物车网址

//load the product by product id
$product=Mage::getModel('catalog/product')->load($productId);

//or load the product by sku number
$product=Mage::getModel('catalog/product')>loadByAttribute('sku',$skuNum);

//or load the product from a given store id
$product=Mage::getModel('catalog/product')->setStoreId($storeId)->loadByAttribute('sku',$skuNum);

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

如果您位于扩展Mage_Catalog_Block_Product_Abstract的块中,则可以在块类本身或此块的phtml模板文件中使用此行代码来获取添加到购物车URL。

$this->getAddToCartUrl($product);

如果知道商品的ID,还可以这样执行

<?php $product = new Varien_Object(array('entity_id'=>$_product['product_id'])); ?>

<form action="<?php echo  Mage::helper('checkout/cart')->getAddUrl($product);?>"
 method="post" id="product_addtocart_form_<?php echo $_product['product_id'];?>">
</form>

参考案例:https://www.codexpedia.com/magento/magento-get-the-add-to-cart-url-of-a-product/

Leave a comment

您的电子邮箱地址不会被公开。 必填项已用 * 标注