Placing “Add to cart” button on homepage in Magento

时光怂恿深爱的人放手 提交于 2019-12-24 15:19:19

问题


I'm trying to develop a shopping portal in Magento. At homepage, I want to show "add to cart" button next to every product shown there. Home Page is a simple static CMS page. When i tried this code,

<button class="button btn-cart" title="Add to Cart" onclick="setLocation('/n/magento/checkout/cart/add/product/644/qty/1')" type="button"><span><span>Add to Cart</span></span></button>

where 644 is product id, page was redirected to cart page, but product is not being added in the cart. I tried it in firefox, chrome and IE as well but with nothing. I searched through many sites for this, but couldn't find anything useful. If anyone could help regarding this, it will be of great help. Thanks in advance.


回答1:


Try this link :

Add to cart Hope it helps.

Or try this:

<?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; ?>

Clear your cache and reload your page.




回答2:


It will works perfectly
pass your product as $_product

<?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>



回答3:


It's been a long time since I posted this question, and I eventually found the answer but forgot to add that here.

There is no way I can have "Add to cart" button from within inside admin wysiwyg editor as it requires calling Magento classes via PHP which is not possible from the admin editor(It is not for PHP code).

What I did, was called a template in admin like this :

<block type="core/template" name="home_products" template="home/product.phtml">

And then, Inside that file, I used PHP functions to have the form that Magento requires for a proper Add to cart button. I simply loaded the product via catalog/product model and then created the form similarly like what is inside catalog/product/view/addtocart.phtml file. Also, with latest versions of Magento, formkey should also be present inside the form to get it working properly.




回答4:


Try this

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



回答5:


Add To Cart link for your product any where on a Magento website::

The following code may helpful:

$product = Mage::getModel('catalog/product')->load($YourProductID);

echo Mage::helper('checkout/cart')->getAddUrl($product);




回答6:


Put below code into your .phtml file.

$productId = '168';   // Your Product Id
$_product = Mage::getModel('catalog/product')->load($productId);

<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo Mage::helper('checkout/cart')->getAddUrl($_product); ?>')"><span><span><img src="<?php echo $this->getSkinUrl('images/buy.jpg') ?>" alt="" /></span></span></button>

Code Taken from here : http://chandreshrana.blogspot.in/2016/03/adding-custom-add-to-cart-button-in.html




回答7:


It's working, try this:

$product = Mage::getModel('catalog/product')->load(1);


echo '<a href=' . Mage::helper('checkout/cart')->getAddUrl($product) .'>CONFIRM AND PROCEED TO CHECKOUT </a>';


来源:https://stackoverflow.com/questions/20179581/placing-add-to-cart-button-on-homepage-in-magento

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!