How to get rule id with respect to customer group id
-
29-03-2021 - |
Question
class CouponCode extends \Magento\Framework\View\Element\Template
{
protected $_customerSession;
protected $_customerGroupCollection;
public function __construct(
\Magento\Customer\Model\Session $customerSession,
\Magento\Customer\Model\Group $customerGroupCollection,
\Magento\Framework\View\Element\Template\Context $context)
{
$this->_customerSession = $customerSession;
$this->_customerGroupCollection = $customerGroupCollection;
parent::__construct($context);
}
public function getCouponCodes()
{
$currentGroupId = $this->_customerSession->getCustomer()->getGroupId(); //Get customer group Id , you have already this so directly get name
$collection = $this->_customerGroupCollection->load($currentGroupId);
return $collection->getCustomerGroupCode();//Get group name
}
La solution
In your block constructor inject
\Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory $salesRuleCollectionFactory,
and then filter the sales rules collection by customer group id
$_salesRulecollectionByCustomerGroup = $this->_salesRuleCollectionFactory->create()->addCustomerGroupFilter($currentGroupId);
Autres conseils
Please add this code in your file. Youo will get what you want
$currentGroupId = $this->_customerSession->getCustomer()->getGroupId();
$_salesRulecollectionByCustomerGroup = $this->_salesRuleCollectionFactory->create()->addCustomerGroupFilter($currentGroupId);
$collection = $this->_customerGroupCollection->load($currentGroupId);
$result=$_salesRulecollectionByCustomerGroup->getData();
foreach ($result as $res) {
print_r($res['rule_id']);
}
And Customize it according to your code.
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange