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
Leave a comment
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)
Tag Cloud
500 internal server error admin answers attribute bug category checkbox checkout cookie customer difference domain name EAV error event extension interview invoice jquery linux magento magento2 magento admin magento error magento interview questions magento orm mysql observer order pinterest product products questions redirect register remove script session simplexml to array state status study guide tax url wordpress
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)
===
===
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
===
===
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
===
===
-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.