Magento 1.9 – Fix Add to Cart Button Not Working

magento-1.9

I recently copied some code from an online tutorial to put featured products on my home page, and it worked, up to a point. The products appear as desired, but the Add to Cart button on each product doesn't work. When you click on it nothing happens. Here is the code that creates the button (I think I've got the right part!)

 <?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><span><?php echo $this->__('Out of stock') ?></span></p>
 <?php endif; ?>

I'm afraid I don't know enough about PHP to know why this doesn't work. Can anyone help?

Smartie's comment makes me think I should post the whole code, so:

 <?php
 $totalPerPage = ($this->show_total) ? $this->show_total : 6;
 $visibility = array(
 Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
 Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
 );
 $storeId = Mage::app()->getStore()->getId();
 $_productCollection = Mage::getResourceModel('reports/product_collection')
 ->addAttributeToSelect('*')
 ->setStoreId($storeId)
 ->addStoreFilter($storeId)
 ->addAttributeToFilter('visibility', $visibility)
 ->addAttributeToFilter('featured', true)
 ->setOrder('created_at', 'desc')
 ->addAttributeToSelect('status')
 ->setPageSize($totalPerPage);

 Mage::getSingleton('catalog/product_status') ->  addVisibleFilterToCollection($_productCollection);
 Mage::getSingleton('cataloginventory/stock') -> addInStockFilterToCollection($_productCollection);
 Mage::getSingleton('catalog/product_visibility') -> addVisibleInSearchFilterToCollection($_productCollection);
 ?>

 <h1>Featured products</h1><br><br><div>
 <?php $_collectionSize = $_productCollection->count() ?>
 <div id="featured-div">
 <?php foreach ($_productCollection as $_product): ?>
 <div class="featuredproduct">
 <p class="product-image" style="width:240px; margin-top:20px; padding:10px 20px;">
 <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
 <img class="featuredpic" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(170, 170); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /></a></p>
 <h5><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h5><div>
 <?php echo $_product->getShortDescription() ?></div><div>
 <?php if ($_product->getSpecialPrice()>0):?>
 <span class="old featuredprice"><?php echo $this->helper('checkout')->formatPrice($_product->getPrice()) ?></span>&nbsp;
 <span class="new featuredprice"><?php echo $this->helper('checkout')->formatPrice($_product->getSpecialPrice()) ?></span><?php else: ?>
 <span class="new featuredprice"><?php echo $this->helper('checkout')->formatPrice($_product->getPrice()) ?></span>
 <?php endif; ?><?php if($_product->isSaleable()): ?>
 <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart featuredbutton"
  onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button><?php else: ?>
 <p><span><?php echo $this->__('Out of stock') ?></span></p>
 <?php endif; ?></div><div class="clear"></div></div>
 <div class="clear"></div>
 <?php endforeach ?>
 </div>
  <script type="text/javascript">decorateTable('product-list-table')</script></div>

Best Answer

Change

setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')

to

setLocation('<?php echo Mage::helper('checkout/cart')->getAddUrl($_product) ?>')
Related Topic