我正在编写一个扩展程序,可以在单击产品页面上的“添加到购物车”按钮时直接转到结帐页面。我找到了Magento 1 here的解决方案,我尝试将其改编为Magento 2.这是我的文件:
文件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);
}
}
我应该更改或添加什么才能使其正常工作?
任何指导都将不胜感激。
答案 0 :(得分:3)
以下是完整的工作代码。我使用if用于我的模块。
<?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>
观察者代码是:
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();
}
}
但此代码仅适用于详细信息页面。在列表页面中它不起作用,因为它由Ajax管理。那么解决方案是什么呢?很简单,只需为 Checkout / Controller / Cart / Add.php 创建一个插件,然后在此文件中编写逻辑。
答案 1 :(得分:2)
您必须使用以下代码从Magento2中的Observer重定向
public function execute(Observer $observer)
{
$redirect = $observer->getEvent()->getRedirect();
$redirect->setRedirect(true)->setPath('checkout')->setArguments([]);
return $this;
}
答案 2 :(得分:0)
Magento已经为这些设置提供了管理员设置,
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
将下拉列表值设置为要求,
答案 3 :(得分:0)
对于Magento 2,请在Observer中使用以下代码:
public function execute(Observer $observer)
{
$observer->getRequest()->setParam('return_url', $this->urlInterface->getUrl('checkout'));
return $this;
}