Hello @Zendo
Have you checked my solution, it will remove payment method when sombody go to payment page.
Let me know if you have any issue
@Sunil Patel I tried.. But it just breaks the whole site.. some issues with the di.xml file as far as I understand
Hello @Zendo
there is one tag mismatch, i edited that msg , please check again.
make sure if you already did compile then do again, otherwise flush cache and check it.
If work then marks as a solution.
Yes, that's great! Your code does the job! How would I get customers group ID in the plugin? I know if I was using the Observer I could get it through:
$observer->getEvent()->getQuote();
$quote->getCustomerGroupId()
Right, I give you ten thousand kudos, but am unsure if I should mark your code as a solution? Since you solved it without even using Observer... Let me know if this would be appropriate.
Hello @Zendo
<?php namespace CompanyName\ModuleName\Plugin\Model\Method; class Available { protected $customercreditHelper; public function __construct( \CompanyName\ModuleName\Helper\Data $customercreditHelper ) { $this->customercreditHelper = $customercreditHelper; } /** * * @param \Magento\Payment\Model\MethodList $subject * @param $result * @return bool * @throws \Magento\Framework\Exception\LocalizedException */ public function afterGetAvailableMethods($subject, $result) { foreach ($result as $key=>$_result) { if ($_result->getCode() == "customercredit") { $isAllowed = $this->customercreditHelper->checkCustomerGroup(); if (!$isAllowed) { unset($result[$key]); } } } return $result; } }
In helper class
<?php namespace CompanyName\ModudleName\Helper; class Data extends \Magento\Framework\App\Helper\AbstractHelper { protected $customerSession; protected $customerFactory; protected $_eventManager; public function __construct( \Magento\Framework\App\Helper\Context $context, \Magento\Customer\Model\Session $customerSession, ) { parent::__construct($context); $this->customerSession = $customerSession; } public function checkCustomerGroup() { $customerGroup=''; if($this->_customerSession->isLoggedIn())){ $customerGroup=$this->_customerSession->getCustomer()->getGroupId(); } if($customerGroup==1) { return true; }else{ return false; } } }
Hope it will help you.
Thanks! This is what I have now and it works
<?php
namespace Zen\PaymentMethod\Plugin\Model\Method;
class Available extends \Magento\Framework\Session\SessionManager
{
protected $customerSession;
public function __construct(\Magento\Customer\Model\Session $customerSession)
{
$this->customerSession = $customerSession;
}
public function afterGetAvailableMethods($subject, $result)
{
$group = $this->customerSession->getCustomer()->getGroupId();
if($group != 4)
{
foreach ($result as $key => $_result) {
if ($_result->getCode() == "purchaseorder") {
$isAllowed = false;
if (!$isAllowed) {
unset($result[$key]);
}
}
}
}
return $result;
}
}
Just wanted to add this here: according to Magento docs, events' data must not be modified by observers, so @Sunil Patel answer was the only way to do it and my original question about the observer was invalid..
14. Events
14.1. All values (including objects) passed to an event MUST NOT be modified in the event observer. Instead, plugins SHOULD BE used for modifying the input or output of a function.