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.
You need to write a plugin to achieve this. For that define di.xml
file in your module.
File: app/code/<Namespace>/<Module>/etc/frontend/di.xml
<?xml version="1.0" ?>
<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 disabled="false" name="NamespaceModuleBlockCartBeforePlugin" sortOrder="1" type="Namespace\Module\Plugin\Magento\Checkout\Block\Cart"/>
</type>
</config>
Then in your plugin class, add the below code:
File: app/code/<Namespace>/<Module>/Plugin/Magento/Checkout/Block/Cart.php
<?php
namespace Namespace\Module\Plugin\Magento\Checkout\Block;
use Magento\Checkout\Block\Cart as CheckoutCart;
use Magento\Checkout\Model\Session;
class Cart
{
protected $_checkoutSession;
public function __construct(Session $session) {
$this->_checkoutSession = $session;
}
public function beforeGetContinueShoppingUrl(CheckoutCart $subject)
{
$url = $subject->getData('continue_shopping_url');
if ($url === null) {
$url = $this->_checkoutSession->getContinueShoppingUrl(true);
if (!$url) {
$url = $this->getCustomUrl();
}
$subject->setData('continue_shopping_url', $url);
}
return $this;
}
public function getCustomUrl()
{
//add the logic to get your custom url
return $customUrl;
}
}
As you can see, our plugin method is beforeGetContinueShoppingUrl
here. The code is similar to the method \Magento\Checkout\Block\Cart::getContinueShoppingUrl()
, except that we are setting the default URL with the call $this->getCustomUrl()
. In this way, we are respecting the core code and hence an update to the core code logic to generate (in future) continue_shopping_url
will not be altered.
Now you need to define the method getCustomUrl()
as you needed.
After the changes made, it is good to perform php bin/magento setup:di:compile
and php bin/magento cache:flush
.
You are done.
Best Answer
"It's just a configuration setting. System -> Configuration -> Sales tab -> Checkout and then in the Shopping Cart tab you set the After Adding a Product Redirect to Shopping Cart to No. Easy! Good luck with the Big M :) "
Source here: https://stackoverflow.com/questions/12042318/magento-how-can-i-prevent-to-go-immediately-to-the-shopping-cart-page-once-i-h