简体   繁体   中英

Magento - “add to cart” button using static block does not add to cart

I made an "add to cart" button using a static block. I want to make a template which, when user click the cart button, adds the item to the cart like this:

在此处输入图片说明

How could i modify this template, especially the "onclick" part?

    <div class="actions">
    <?php if($_product->isSaleable()): ?>
        <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
    <?php else: ?>
        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
    <?php endif; ?>
</div>
call phtml file in your static block using this
{{block type="core/template" template="catalog/product/custom.phtml"}}

$productId=4;(assign your product id here)
$_product=Mage::getModel('catalog/product')->load($productId);

<div class="actions">
    <?php if($_product->isSaleable()): ?>
        <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
    <?php else: ?>
        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
    <?php endif; ?>
</div>

将您的按钮html替换为以下代码:

<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo (string)Mage::helper('checkout/cart')->getAddUrl($_product);?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM