2

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>
  • If you view the source what is output in the setLocation('')? – Smartie Sep 3 '15 at 13:31
  • onclick="setLocation('')" - which explains a lot. – Alistair67 Sep 3 '15 at 13:39
  • Would I be better posting the whole code of the block in question? I did change it from the original so I may have stuffed something up! – Alistair67 Sep 3 '15 at 13:45
6

Change

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

to

setLocation('<?php echo Mage::helper('checkout/cart')->getAddUrl($_product) ?>')
  • Lovely, works a treat. Smartie, you are well named! Thanks. – Alistair67 Sep 3 '15 at 14:27
  • This is the same, I don't understand why it works? $this->getAddToCartUrl calls Mage::helper('checkout/cart')->getAddUrl($_product) – g.adam May 31 '18 at 14:11
0

app/design/frontend/default/ezzy/template/catalog/product/view/addtocart.phtml

<?php $_product = $this->getProduct(); ?>
<?php $buttonTitle = $this->__('Add to cart'); ?>
<?php if($_product->isSaleable()): ?>
    <div class="add-to-cart">
        <?php if(!$_product->isGrouped()): ?>
            <div class="qty-block">
                <label for="qty"><?php echo $this->__('Qty:') ?></label>
                <input type="text" name="qty" id="qty" maxlength="12" value="1" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
            </div>
        <?php endif; ?>
        <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
        <?php echo $this->getChildHtml('', true, true) ?>
    </div>
<?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.