1
votes

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.

7
Ok, I got this. I was wrong with using CMS page to place Add to cart button, as it requires Magento's PHP code, not possible in CMS. So, thanks to all who tried to help. I'm using custom template file for my home page now.Prateek

7 Answers

3
votes

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.

1
votes

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>
1
votes

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.

1
votes

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>';
0
votes

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>
0
votes

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

0
votes

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