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!



One Response to “Setting the ‘continue shopping’ URL to last product’s category page in magento”

  1. 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;
    }


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.