2
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

    }

New contributor
Muhammad Abdullah is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2

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);
  • Thanks for your response,but after injecting this dependency ,nothing is shown on the screen, addAttributeInConditionFilteraddCustomerGroupFilter do you think this line is properly defined? – Muhammad Abdullah Jan 14 at 14:39
  • 1
    to debug this, you can enable the developer mode to print the error on the screen or check the logs....BTW check your Magento version to confirm whether this method is present in this file \Magento\SalesRule\Model\ResourceModel\Rule\Collection – Haijerome Jan 14 at 15:53
  • Yes ,there are two method found in the file \Magento\SalesRule\Model\ResourceModel\Rule\Collection 'public function addAttributeInConditionFilter($attributeCode)' 'public function addCustomerGroupFilter($customerGroupId)' but from which one of above we have to used?for my solution and in which way? – Muhammad Abdullah 2 days ago
  • Thanks alot,addCustomerGroupFilter($customerGroupId) function work for me – Muhammad Abdullah 2 days ago
2

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.

Your Answer

Muhammad Abdullah is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.