1

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.

Prateek
  • 274
  • 1
  • 4
  • 18
  • 1
    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 Apr 11 '14 at 08:44

7 Answers7

3

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.

vbak
  • 349
  • 1
  • 7
  • 18
1

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>
  • could you pls help me, in my cms page add to cart not working -> code -> https://justpaste.it/49eqz – Gem Aug 17 '18 at 11:45
1

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.

Prateek
  • 274
  • 1
  • 4
  • 18
  • could you pls help me, in my cms page add to cart not working -> code -> https://justpaste.it/49eqz – Gem Aug 17 '18 at 11:45
  • @Gem, there is no context for `$this` in that code, probably why it's not working. Try posting a question with more details as to where you are using this code in Magento application, and how it's being called. – Prateek Jan 06 '19 at 18:46
  • I am using the above code in my CMS page, my concept like all of my products display and that customer can add to cart the entire products in single cms page. @Prateek – Gem Jan 07 '19 at 04:49
  • It won't work like that. As I mentioned before, you can't use PHP code inside CMS editor area in admin. – Prateek Jan 07 '19 at 12:31
  • Okay, may i know the best way like all products list in the single page along with an add-to-cart button? @Prateek – Gem Jan 08 '19 at 04:32
1

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

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>
Rajat
  • 56
  • 1
0

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

asishint
  • 109
  • 9
0

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

Chandresh
  • 361
  • 2
  • 7
  • As I mentioned in the question, I was trying to do this from admin editor. As I mentioned in the comment, it is not possible to do so from within admin and I got this working from template. Please read both carefully. With latest versions of Magento, even this will not work, as there is no form key. Thanks for reminding me of this question, I'll add what I did as the answer soon. – Prateek Mar 16 '16 at 08:40