Magento – Redirect “Continue Shopping” Button to the Current Product Category it was added from

cartcategoryfiltermagento-1.9shopping-cart

I know how to redirect Magento's Continue Shopping button to the first and last categories from the array of all categories for the last product added to the cart but I want to redirect to the first page of the 'CURRENT CATEGORY' that the product was added from for the last product added to the shopping cart. Our catalog is too many levels deep for first or last categories from the array to work as needed.

Here is an example. So if someone adds a product from category (Sub-Sub-Category: 2A) the continue shopping button would need to take them back to the first page of (Sub-Sub-Category: 2A). How do I do this with arrays?

Category: A
Sub-Category: 1A
Sub-Sub-Category: 2A
Sub-Sub-Sub-Category: 3A

app/design/frontend/yourpackage/yourtheme/template/checkout/cart.phtml

Code

<?php
    $lastProductAddedToCartId = Mage::getSingleton('checkout/session')->getLastAddedProductId();
    if($lastProductAddedToCartId) {
        $productCategoryIdsArray = Mage::getModel('catalog/product')->load($lastProductAddedToCartId)->getCategoryIds();
        $continueShoppingCategoryUrl = Mage::getModel('catalog/category')->load($productCategoryIdsArray[0])->getUrl();
    }
?>

Shopping Cart Button

   <?php if($this->getContinueShoppingUrl()): ?>
        <button type="button" title="<?php echo $this->__('Continue Shopping') ?>" class="button2 btn-continue" onclick="setLocation('<?php echo (isset($continueShoppingCategoryUrl)) ? $continueShoppingCategoryUrl : $this->getContinueShoppingUrl(); ?>')"><span><span><?php echo $this->__('Continue Shopping') ?></span></span></button>
    <?php endif; ?>

Best Answer

I added a similar feature to my site. Instead of looking at the product I set a variable from the URL on the last category page viewed which in most cases would be the category the items was added from.

This is the code added to template->Catalog->Category->view.phtml

<?php
$_helper    = $this->helper('catalog/output');
$_category  = $this->getCurrentCategory();
$currentUrl = $this->helper('core/url')->getCurrentUrl();
$session = Mage::getSingleton("core/session",  array("name"=>"frontend"));
$session->setData("last_category", $currentUrl);?>

Then in template->Checkout->cart.phtml to replace the current button:

<?php if($this->getContinueShoppingUrl()): ?>
    <?php $session = Mage::getSingleton("core/session",  array("name"=>"frontend"));
    $lastUrl = $session->getData("last_category");
    if(empty($lastUrl)):
        $lastUrl = $this->getContinueShoppingUrl();
    endif; ?>
    <button type="button" title="<?php echo $this->__('Continue Shopping') ?>" class="btn btn-primary" onclick="setLocation('<?php echo $lastUrl ?>')"><span><span><?php echo $this->__('Continue Shopping') ?></span></span></button>
<?php endif; ?>

This also checks to make sure they have visited a category before, if not it sends them to the default "continue shopping" location.

I got this solution from somewhere else, can't recall where though.