0

I am trying to use a formula that looks at whether a customer is wholesale or not, and then discounts the price automatically for a product if they are, but on a custom priced product. (So I can't just go to advanced pricing and set a tier price). In my formula, I have a variable setup like this

squareinchcost=>('customerGroupId' == 'wholesale' ? .39 : .58);

The 'customerGroupId' is what I need to figure out. What do I need to enter to have the formula actually pull the customerGroupId so that this can equate to either .39 or .58 depending on the customer group?

5
  • You want to get customer id on phtml ?
    – Pawan
    Commented Sep 19, 2019 at 3:54
  • Yes. On a specific product page
    – Josiah
    Commented Sep 19, 2019 at 16:23
  • Do you have any custom module on your website ?
    – Pawan
    Commented Sep 19, 2019 at 17:23
  • yeah a custom math formula module for the product
    – Josiah
    Commented Sep 19, 2019 at 17:34
  • What is the exact version of magento 2 ?
    – Pawan
    Commented Sep 20, 2019 at 3:12

2 Answers 2

0

This code is for get customer group. You can put in any file and call the function and it will be work.

/**
 * Customer Group
 *
 * @var \Magento\Customer\Model\ResourceModel\Group\Collection
 */
protected $_customerGroup;

/**
 * @param \Magento\Backend\Block\Template\Context $context
 * @param \Magento\Customer\Model\ResourceModel\Group\Collection $customerGroup
 * @param array $data
 */
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Customer\Model\ResourceModel\Group\Collection $customerGroup,        
    array $data = []
) {
    $this->_customerGroup = $customerGroup;        
    parent::__construct($context, $data);
}
/**
 * Get customer groups
 * 
 * @return array
 */ 
public function getCustomerGroups() {
    $customerGroups = $this->_customerGroup->toOptionArray();
    array_unshift($customerGroups, array('value'=>'', 'label'=>'Any'));
    return $customerGroups;
}

Thanks

7
  • How can I run this on just one product page?
    – Josiah
    Commented Sep 19, 2019 at 16:23
  • One product page means? @Josiah
    – Mayur
    Commented Sep 19, 2019 at 17:36
  • I just need to be able to call the function on a product page so that it will return whether a customer is a wholesale customer or not for my formula
    – Josiah
    Commented Sep 19, 2019 at 17:48
  • Yes so when you will call this function you will get customer group label and you just need to check in if condition for that. @Josiah
    – Mayur
    Commented Sep 19, 2019 at 17:51
  • Where can I put this function though so that it can be called on a product page?
    – Josiah
    Commented Sep 19, 2019 at 17:55
0

To get the customer group Id, you can use this code.

protected $_customerSession;

public function __construct(\Magento\Customer\Model\Session $customerSession) 
{
    $this->_customerSession = $customerSession;
}

public function getCustomerGroupId()
{
    if($this->_customerSession->isLoggedIn()):
        return $this->_customerSession->getCustomer()->getGroupId();
    endif;
}
2
  • How can I run this on just one product page?
    – Josiah
    Commented Sep 19, 2019 at 16:10
  • You need to to put above code into your block file and then call that function into your phtml file. Commented Sep 23, 2019 at 4:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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