0

创建了一个简单的模块来在下拉菜单中显示促销产品。现在我想要一个按钮“添加到购物车/购物篮”来立即添加这个产品。这种方法在哪里或如何做?

4

1 回答 1

1

方法一:

如果您位于扩展 Mage_Catalog_Block_Product_Abstract 的块中,您可以在块类本身或此块的 phtml 模板文件中使用这行代码来获取添加到购物车的 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>

方法二:

将产品对象传递给结帐/购物车助手以获取添加到购物车的 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
<a href="<?php echo $this->helper('checkout/cart')->getAddUrl($product);?>">Add to cart</a> //Get the add to cart url
于 2016-07-25T13:06:58.893 回答