用插件覆盖magento \ checkout \ controller \ cart \ cart :: getbackull
题
我一直在尝试在购物车中覆盖Carchout的Card Controller中的getbackull方法。偏好我在没有问题的情况下管理,但插件有点挑战。
这是我到目前为止的内容。
di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Controller\Cart\Add">
<plugin name="[namespace]_[module]_checkout_controller_cart_add_before" type="[Namespace]\[Module]\Controller\Checkout\Cart\Plugin" sortOrder="1" />
</type>
</config>
.
Controller/Checkout/Cart/Plugin.php
namespace [Namespace]\[Module]\Controller\Checkout\Cart;
class Plugin
{
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $_config;
/**
* @var \Magento\Framework\Url
*/
protected $_url;
/**
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
* @param \Magento\Framework\Url $url
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $config,
\Magento\Framework\Url $url
) {
$this->_config = $config;
$this->_url = $url;
}
/**
* Get resolved back url, rewritten to return checkout URL instead of cart url
*
* @param \Magento\Checkout\Controller\Cart\Add $subject
* @return string
*/
protected function beforeGetBackUrl(\Magento\Checkout\Controller\Cart\Add $subject)
{
return $this->_url->getUrl('some/custom/url');
}
}
.
无论我尝试什么,我都无法让它到达beforeGetBackUrl
方法。
解决方案
可以仅插入公共非静态方法: http://devdocs.magento.com/guide/v2.0/extension-dev-guide/plugins.html#plugin-limit
不可能插入protected
,private
,static
方法。
也不可能插入公共属性划线码(构造函数)。
https://github.com/magento/magento2/issues/2265
https://github.com/magento/magento2/issues/2367
https://github.com/magento/magento2/issues/2202.suecomment-151599087
其他提示
你不能将getbackul插件,但你可以geturl。
为此,您需要:
- 从\ magento \ framework \ url 创建虚拟类型myurlbulder
- 使用它作为\ magento \ checkout \ controller \ cart \ add 的参数
- 为myurlbulder虚拟类型 声明插件