system->configuration->sales section ->checkout->shopping cart->After Adding a Product Redirect to Shopping Cart=no
mage->checkout->controllers->cartcontroller->
you need to work on addAction method (you can directly give $url=home url or product list url.and you need to override cartcontroller).
if (!$this->_getSession()->getNoCartRedirect(true)) {
if (!$cart->getQuote()->getHasError()) {
$message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->escapeHtml($product->getName()));
$this->_getSession()->addSuccess($message);
}
$this->_goBack();
}
........... ....
$url = $this->_getSession()->getRedirectUrl(true);
if ($url) {
$this->getResponse()->setRedirect($url);
} else {
$this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
}
There are many things wrong here and I feel compelled to do a code review with you. See the end for the correct approach.
Code review:
This line is loading a product with an undefined $sku var:
$product = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('sku', $sku)
->addAttributeToSelect('*')
->getFirstItem();
Now you're reloading the $product var with an undefined value, returns a blank model or null.
$product->load($product_id);
Getting the cart model (but not necessarily mine from my customer session):
$cart = Mage::getSingleton('checkout/cart');
Wait - now we're loading another instance of a blank object:
$productInstance = Mage::getModel('catalog/product')->load($product_id);
Clearing the brand new cart:
$cart->truncate();
Saving our empty cart:
$cart->save();
Getting all the items in our empty cart, clearing them, and saving again:
$cart->getItems()->clear()->save();
The right way:
All in all this code makes no sense. Some Google searching turns up the right way to add a product to the current user's cart with the correct price attached:
$quote = Mage::getSingleton('checkout/session')->getQuote();
$quote->addProduct($product, $qty);
$quote->collectTotals()->save();
Where $product
and $qty
are both defined, and $product
is an instance of Mage_Catalog_Model_Product
and $qty
is an integer.
Best Answer
Replaced
$this->getAddToCartURL($_product)
withMage::helper('checkout/cart')->getAddUrl($_product)
and now it is working fine.