2

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; ?>

1 Answer 1

5

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.

6
  • Worked! Would you also know how some sites are redirecting back to not only the exact category that the product was added from but also the exact category "page number" the product was added from? Thanks.
    – user9722
    Sep 2, 2015 at 15:00
  • This code should take care of that if the page number is part of the URL. Not sure how to do that if it doesn't. My site is not using pages just loading more products at the bottom of the page. Sep 2, 2015 at 15:13
  • Yeah, if you use pages it sends you to the "first" page of the last category you visited. Does anyone know how to get not only the last category visited (using the code above) but also the "page number"????
    – user9722
    Sep 2, 2015 at 15:33
  • I was incorrect. This in fact does work if you use pagination. And it appears I just needed to flush cache and re-indexed.
    – user9722
    Sep 25, 2015 at 20:34
  • i want to redirect to the product view page. how can i do this.
    – atifaltaf
    Oct 8, 2016 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.