Enhancing Magento: Redirecting “Continue Shopping” to Previous Category
The “Continue Shopping” button is a crucial feature on the Magento shopping cart page that helps users navigate back to browsing products after adding items to their cart. However, by default, it redirects them to the homepage rather than the category they were previously browsing. If you’re looking to change this behaviorâsending customers back to the last-added-to-cart product’s categoryâthe following guide will walk you through the necessary steps.
Understanding the Default Behavior
In Magento 2, the continue shopping button is linked to the homepage by default because it’s designed for a seamless user experience across different categories. The core logic responsible for this behavior lies within the vendor/magento/module-checkout/view/frontend/templates/items.phtml template file.
<a href="<?php /* @escapeNotVerified */ echo $block->getContinueShoppingUrl(); ?>">
<?php /* @escapeNotVerified */ echo __('Continue Shopping') ?>
</a>
The getContinueShoppingUrl() function in the core Magento\Checkout\Block\Cart class returns the URL to the store homepage. This is why clicking “Continue Shopping” sends users back to the home page.
Redirecting to the Previous Category
To achieve your desired functionality, you’ll need to override this template and change the URL calculation logic. Here’s a step-by-step approach using a custom module:
-
Create a new Magento 2 module (e.g.,
Your_Company_ContinueShopping). -
In the
etc/di.xmlfile of your module, add the following XML configuration to replace the core template:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Block\Cart">
<plugin name="custom_continue_shopping_url" type="YourCompany\ContinueShopping\Plugin\CartPlugin"/>
</type>
</config>
- Create a
CartPlugin.phpfile in thePluginfolder of your module with the following content:
<?php
namespace YourCompany\ContinueShopping\Plugin;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\View\Element\Template;
class CartPlugin
{
/**
* @var RequestInterface
*/
private $request;
public function __construct(RequestInterface $request)
{
$this->request = $request;
}
public function aroundGetContinueShoppingUrl(Template $subject, callable $proceed): string
{
if ($this->request->getActionFlag('checkout_cart_index') === '1' && $lastAddedItemId = $this->request->getParam('product')) {
// Fetch the last added product category ID
$product = \Magento\Framework\App\ObjectManager::getInstance()->create(
'\Magento\Catalog\Model\Product'
)->load($lastAddedItemId);
$categoryIds = $product->getCategoryIds();
if ($categoryIds) {
$lastCategoryId = end($categoryIds);
return $this->_url->getUrl('catalog/category/view', ['id' => $lastCategoryId]);
}
}
// Fallback to default homepage URL
return $proceed();
}
}
- Clear the Magento cache and deploy your static content.
Conclusion
By following this guide, you’ve successfully overridden the core “Continue Shopping” functionality in Magento 2 to redirect users back to the category of the last-added-to-cart product. This enhances the user experience by keeping them within the context of their current browsing session.
Remember that modifying core files directly is not recommended, as it can cause issues during updates and migrations. Leveraging a custom module with plugins allows you to maintain compatibility and easily manage your changes. If you’re looking for more advanced customization options or professional support in implementing such solutions, consider reaching out to the experts at Laravel Company.
Don’t hesitate to explore other Magento customization possibilities to create a truly unique and engaging shopping experience for your customers.