Jan 5, 2014
kalpesh

Magento continue shopping link to last added product’s category page

In Magento checkout cart, link Continue shopping button to last added product’s category page. Below code will check last added product’s id in cart and gets the last category assigned to the product.

Put below code at the start of the checkout/cart.phtml file

$lastProductIdAddedToCart = Mage::getSingleton('checkout/session')->getLastAddedProductId();
if($lastProductIdAddedToCart) {
    $productCategoryIdsArray = Mage::getModel('catalog/product')->load($lastProductIdAddedToCart)->getCategoryIds();
    //print_r($productCategoryIdsArray);    
    $continueShoppingCategoryUrl = Mage::getModel('catalog/category')->load(end($productCategoryIdsArray))->getUrl();
}

Replace continue shopping button with below code, in the checkout/cart.phtml file

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

4 Comments

  • Just what I wanted. Thanks a bunch!

    Regards.

  • This solution is deficient for two reasons. First, this code only executes properly the first time the user lands on the cart page after adding a new item. If they leave the cart and return without adding an item or simply refresh the cart, then the last item added is forgotten. Second, it chooses the highest category ID blindly and uses that. This is unpredictable if the owners ever change their categories around. Having researched a few alternatives, I’m offering this solution instead:

    catalog/category/view.phtml (Add this at the top)
    ===

    // I want to track the category page last viewed by the user
        // so I can use it for the 'Continue Shopping' button
        $currentUrl = $this->helper('core/url')->getCurrentUrl();
        $session = Mage::getSingleton("core/session", array("name"=>"frontend"));
        $session->setData("last_category", $currentUrl);

    ===

    The only problem with this lovely approach is that the user may never actually hit a category page. This isn’t so bad and we can combine the two approaches to the problem for a more complete solution.

    checkout/cart.php
    ===

    $session = Mage::getSingleton("core/session", array("name"=>"frontend"));

    // this runs only the first time I hit the cart after adding a new item.
    $lastProductIdAddedToCart = Mage::getSingleton('checkout/session')->getLastAddedProductId();
    if($lastProductIdAddedToCart){
        // get all category id's for this item
        $productCategoryIdsArray = Mage::getModel('catalog/product')->load($lastProductIdAddedToCart)->getCategoryIds();
        // get the url for the last one (highest id)
        $continueShoppingCategoryUrl = Mage::getModel('catalog/category')->load(end($productCategoryIdsArray))->getUrl();
        // save the url into the session
        $session->setData("last_item_category", $continueShoppingCategoryUrl);
    }
    // This will work if they have visited a category page before getting to the cart
    $lastCategoryUrl = $session->getData("last_category");
    // if they didn't visit a category page
    if(!$lastCategoryUrl){
        // pull a category from the last item set into the cart
        $lastCategoryUrl = $session->getData("last_item_category");
    }
    // if we still don't have a url,
    if(!$lastCategoryUrl){
        // use the 'all products' category. Hopefully this stays at id 42.
        $lastCategoryUrl = Mage::getModel('catalog/category')->load(42)->getURl();
    }

    ===

    For my store id 42 was the ‘All Products’. YMMV.

    Then change the Continue Checkout link to use the $lastCategoryUrl or default as your solution shows.

    NOTE:
    The continue shopping button appears more places. You must fix each.

    checkout/cart/noItems.phtml
    checkout/onepage/failure.phtml
    checkout/multishipping/success.phtml
    ===

    $session = Mage::getSingleton("core/session", array("name"=>"frontend"));

    // This will work if they have visited a category page before getting to the cart
    $lastCategoryUrl = $session->getData("last_category");
    // if they didn't visit a category page
    if(!$lastCategoryUrl){
        // pull a category from the last item set into the cart
        $lastCategoryUrl = $session->getData("last_item_category");
    }
    // if we still don't have a url,
    if(!$lastCategoryUrl){
        // use the 'all products' category. Hopefully this stays at id 42.
        $lastCategoryUrl = Mage::getModel('catalog/category')->load(42)->getURl();
    }

    ===

    -Joe
    Certifications: None.

    • Hi Joe, thanks for the code. 1.) Are you sure the code only executes properly first time? getLastAddedProductId() is being pulled from session and will stay there until the cart changes again, so no matter how many times you refresh the cart or go back and forth it will still work. 2.) The code gets the category ID of the product, but agreed it’s difficult to know the correct category ID of the product last added if that product resides in multiple categories.

      • Kalpesh,

        After just a few simple tests, yes, I am sure. At least for the magento version I am using (the latest 1.9). Also, multiple other bloggers I sto- borrowed code from confirmed this.

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions