[英]Validate Payment Method in Magento 2
I want to check whether a customer is allowed to use one of the activated payment methods. 我想检查是否允许客户使用已激活的付款方式之一。
For example: 例如:
If the customer had enabled "Cash on Delivery", he should see the method but I can't find anything similar in the code. 如果客户启用了“货到付款”,他应该看到该方法,但是我在代码中找不到任何类似的东西。
In Magento 1 there was: 在Magento 1中,有:
public function isAvailable()
Is there any similar way to do it with Magento 2 ? Magento 2有类似的方法吗?
There are 2 approaches you could take: 您可以采用2种方法:
If you take a look at \\Magento\\Payment\\Model\\Method\\AbstractMethod
(the parent class of \\Magento\\OfflinePayments\\Model\\Cashondelivery
) in the isAvailable
method, you will find that it dispatches the payment_method_is_active
event which could be used to performs some additional logic (such as determine if a customer had enabled the specific method or not): 如果您在
isAvailable
方法中查看\\Magento\\Payment\\Model\\Method\\AbstractMethod
( \\Magento\\OfflinePayments\\Model\\Cashondelivery
的父类),您会发现它调度了payment_method_is_active
事件,该事件可用于执行一些其他逻辑(例如,确定客户是否启用了特定方法):
$this->eventManager->dispatch( 'payment_method_is_active', [ 'result' => $checkResult, 'method_instance' => $this, 'quote' => $quote ] );
Since isAvailable
is a public method, you could add a plugin to the method. 由于
isAvailable
是公共方法,因此可以向该方法添加插件。 Whether you use before, after, or around depends on your specific needs. 在使用之前,之后还是前后使用取决于您的特定需求。 However, a good place for learning how to do this is the Magento DevDocs site: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html
但是,Magento DevDocs网站是学习如何执行此操作的好地方: http ://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.