问题
I am writing an extension which allows to go directly to the checkout page when clicking on the add-to-cart button on the product page. I found a solution for Magento 1 here and I tried to adapt it to Magento 2. Here are my files:
File etc/frontend/events.xml:
<?xml version="1.0" encoding="utf-8" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_cart_add_product_complete">
<observer
name="mycompany_go_to_checkout"
instance="MyCompany\GoToCheckout\Observer\GoToCheckout" />
</event>
</config>
File Observer/GoToCheckout.php:
namespace MyCompany\GoToCheckout\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class GoToCheckout implements ObserverInterface
{
protected $_url;
public function execute(Observer $observer)
{
$urlInterface = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\UrlInterface');
$url = $urlInterface->getUrl('checkout');
$observer->getControllerAction()->getResponse()->setRedirect($url);
}
}
What should I change or add to make it work?
Any guidance will be appreciated.
回答1:
Below is the full working code. I used if for my module.
<?xml version="1.0" encoding="utf-8" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_cart_add_product_complete">
<observer
name="mycompany_go_to_checkout"
instance="MyCompany\GoToCheckout\Observer\GoToCheckout" />
</event>
</config>
And observer code is:
namespace MyCompany\GoToCheckout\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class GoToCheckout implements ObserverInterface
{
protected $uri;
protected $responseFactory;
protected $_urlinterface;
public function __construct(
\Zend\Validator\Uri $uri,
\Magento\Framework\UrlInterface $urlinterface,
\Magento\Framework\App\ResponseFactory $responseFactory,
\Magento\Framework\App\RequestInterface $request
) {
$this->uri = $uri;
$this->_urlinterface = $urlinterface;
$this->responseFactory = $responseFactory;
$this->_request = $request;
}
public function execute(Observer $observer)
{
$resultRedirect = $this->responseFactory->create();
$resultRedirect->setRedirect($this->_urlinterface->getUrl('checkout'))->sendResponse('200');
exit();
}
}
But this code only works for detail page. In listing page it will not work because its managed by Ajax. So what was the solutions for that? Easy, just create one Plugin for Checkout/Controller/Cart/Add.php and write your logic in this file.
回答2:
you must use following code to redirect from Observer in Magento2
public function execute(Observer $observer)
{
$redirect = $observer->getEvent()->getRedirect();
$redirect->setRedirect(true)->setPath('checkout')->setArguments([]);
return $this;
}
回答3:
Magento already provide these setting with admin settings,
Magento1
Admin > System > Configuration > Checkout > Shopping Cart > After Adding a Product Redirect to Shopping Cart >YES.
Magento2
Admin-> Store ->Configuration->Sales->Checkout ->After Adding a Product Redirect to Shopping Cart
Set Dropdown value as requirements,
来源:https://stackoverflow.com/questions/37536452/magento-2-go-directly-to-the-checkout-page-when-adding-a-product-to-the-cart