在magento 2.2.7中,单击 AddToCart 按钮时,我需要 验证递送邮政编码字段。仅当客户输入 产品应添加到购物车的正确邮政编码。
传递邮政编码是一个单独的扩展名。扩展内 我在插件之前写过(使用beforeAddProduct类)并尝试获取 邮政编码参数,但我无法这样做,但我可以 获取与产品相关的参数。我可以的产品数量 能得到。能否请您指导我如何获取邮政编码值 插件内?
扩展文件夹结构:https://www.screencast.com/t/AP1nr6cPkWvn
我的模板代码:https://www.screencast.com/t/sEFZ0rUM
我的插件代码
/**
* @var \Magento\Framework\App\Request\Http
*/
protected $request;
/**
* @var \MagePrashant\CheckDelivery\Helper\Data
*/
protected $helper;
/**
* @var \Magento\Framework\Message\ManagerInterface
*/
protected $message;
/**
* Plugin constructor.
*
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Framework\App\Request\Http $request
* @param \MagePrashant\CheckDelivery\Helper\Data $helper
* @param \Magento\Framework\Message\ManagerInterface $message
*/
public function __construct(
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Framework\App\Request\Http $request,
\MagePrashant\CheckDelivery\Helper\Data $helper,
\Magento\Framework\Message\ManagerInterface $message
) {
$this->quote = $checkoutSession->getQuote();
$this->request = $request;
$this->helper = $helper;
$this->message = $message;
}
/**
* beforeAddProduct
*
* @param $subject
* @param $productInfo
* @param null $requestInfo
*
* @return array
* @throws LocalizedException
*/
public function beforeAddProduct($subject, $productInfo, $requestInfo = null)
{
$postcode = $this->request->getParam('postcode');
print_r($postcode);
//Null
print_r($this->request->getParams());
//Array ( [uenc] => aHR0cDovLzEyNy4wLjAuMS9QZXRzeS9wZWRpZ3JlZS1wdXBweS1taWxrLWFuZC12ZWdldGFibGUuaHRtbA,, [product] => 59 [selected_configurable_option] => [related_product] => [item] => 59 [form_key] => BIYNJmVlffX0A95H [super_attribute] => Array ( [140] => 10 ) [qty] => 1 )
}
}