Question

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.

Was it helpful?

Solution

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 } }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top