Setting the ‘continue shopping’ URL to last product’s category page in magento
19May10
Here is a code snippet for setting the ‘Continue Shopping’ location to the category listing page of the last product added to the cart. Open the template/checkout/cart.phtml file in your active theme. Place the following code in the foreach loop
(ie. after this line of code)
<?php foreach($this->getItems() as $_item): ?>
that generates the products list in the cart.
<?php
$_categories = $_item->getProduct()->getCategoryIds();
$_category = Mage::getModel(‘catalog/category’)->load($_categories[0]);
$url = $this->getUrl($_category->getUrlPath());
Mage::getSingleton(‘checkout/session’)->setContinueShoppingUrl($url);
?>
You are done!
Filed under: Magentocommerce, PHP | 1 Comment
Tags: magento, php
in addition if you’d like empty cart to point to home page, open Cart.php at \app\code\core\Mage\Checkout\Block\ and replace public function getContinueShoppingUrl() with the following:
public function getContinueShoppingUrl()
{
$url = $this->getData(‘continue_shopping_url’);
if (is_null($url)) {
$url = Mage::getSingleton(‘checkout/session’)->getContinueShoppingUrl(true);
if (!$url) {
$url = Mage::getUrl();
$url = rtrim($url, ‘/’);
}
elseif (Mage::helper(‘checkout/cart’)->getCart()->getItemsCount() == 0)
$url = Mage::getUrl(”);
$this->setData(‘continue_shopping_url’, $url);
}
return $url;
}