2

I found this code through Google to add a Button to my shopping cart page under where I have some products begin displayed. However when I click on the button, it redirects me to the shopping cart but nothing is being added to the cart and I do not know why.

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

This is my full block of code for the products to be displayed in my cart after some changes:

    <div class="upsell">

<?php if (Mage::helper('checkout/cart')->getQuote()->getSubtotal() < 75.00)
{?>
 <h1><?php echo 'We see your subtotal is under £75, why not add these products to qualify for Free Delivery';?></h1>
 <br>
<?php

$categoryid = 1054;

$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');



$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
?>
<div class="upsell_products">

<?php $i = 0; ?>

<?php foreach ($collection as $_product){ ?>



<div class="product">
<a href="<?php echo $_product->getProductUrl() ?>">

<div class="product_img_cart">

<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image'); ?>" alt="<?php echo $_product->getName(); ?>" /></a>
</div>
<a href="<?php echo $_product->getProductUrl(); ?>">
<p class="upsell_pro_name"><?php echo $_product->getName(); ?></p>
<p class="upsell_pro_price"><?php echo Mage::helper('core')->formatPrice($_product->getPrice());?></a></p>
<div class="add_cart_btn">
<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>
</div>
</div>
<?php if($i++ == 3) break; ?>

<?php } }

If you can help that would be appreciated.

  • I'm confused because your question title doesn't match what you're asking in the body. – andyjv Aug 11 '14 at 12:01
  • You can test this: onclick="setLocation('<?php //echo $this->getAddToCartUrl($_product)?>') ?? – osrecio Aug 11 '14 at 12:01
  • @andyjv sorry that was form an old post, have changed now. – Adam Allen Aug 11 '14 at 12:22
  • @osrecio nah didnt seem to work :( – Adam Allen Aug 11 '14 at 12:23
  • You remove the slashes = "//" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product)?>') ?? – osrecio Aug 11 '14 at 12:39
1

Try one thing:

  • Load product by product id ($_product->getId()).

    $product = Mage::getModel("catalog/product")->load($_product->getId());

  • Then use:

$product instead on $_product in onclick. Code is below:

onclick="setLocation('<?php echo $this->getAddToCartUrl($product) ?>')"

I hope, it will help.

Below is the updated code for working Add To Cart Url:

<div class="upsell">

<?php if (Mage::helper('checkout/cart')->getQuote()->getSubtotal() < 75.00)
{?>
 <h1><?php echo 'We see your subtotal is under £75, why not add these products to qualify for Free Delivery';?></h1>
 <br>
<?php

$categoryid = 1054;

$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');



$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
?>
<div class="upsell_products">

<?php $i = 0; ?>

<?php foreach ($collection as $_product){ ?>
<?php $product = Mage::getModel('catalog/product')->load($_product->getId());?>


<div class="product">
<a href="<?php echo $_product->getProductUrl() ?>">

<div class="product_img_cart">

<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image'); ?>" alt="<?php echo $_product->getName(); ?>" /></a>
</div>
<a href="<?php echo $_product->getProductUrl(); ?>">
<p class="upsell_pro_name"><?php echo $_product->getName(); ?></p>
<p class="upsell_pro_price"><?php echo Mage::helper('core')->formatPrice($_product->getPrice());?></a></p>
<div class="add_cart_btn">
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($product)?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
</div>
</div>
<?php if($i++ == 3) break; ?>

<?php } }
  • where should i put this line ?? $product = Mage::getModel("catalog/product")->load($_product->getId()); – Adam Allen Aug 11 '14 at 15:26
  • where you have <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo Mage::getUrl('checkout/cart/add/')?>product/<?php echo $_product->getId() ?>/')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button> here you need to just change onclick action and load product before that line. – Mohit Kumar Arora Aug 12 '14 at 6:42
  • Nah it just re directs me to the cart again without adding anything. – Adam Allen Aug 12 '14 at 9:24
  • I think you are not understanding my point. I have said to first load product from product id then use that variable instead of $_product while passing in getAddToCartUrl function. – Mohit Kumar Arora Aug 12 '14 at 10:50
  • i just keep getting this error Fatal error: Call to a member function getId() on a non-object in /home/senorehe/public_html/app/design/frontend/my_theme/default/template/checkout/cart.phtml on line 133 – Adam Allen Aug 12 '14 at 16:08

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.