Magento – Get Product price by customer Group magento 2

customercustomer-groupmagento2priceproduct

I am trying to get Product price by customer group. Used below code.

$sku = 123;
$customerGroup = $this->getCustomerGroupId();
$currentProduct = $this->loadProduct($sku);
$productPrice = $currentProduct->getPrice();

 public function loadProduct($sku)
 {
    return $this->productRepository->get($sku);
 }

public function getCustomerGroupId(){
    if ($this->customerSession->isLoggedIn()) {
        $customerGroup = $this->customerSession->getCustomer()->getGroupId();
        return $customerGroup;
    }
    return;
}

Above code returning the product final price.

Can we get product price by customer Group code?

 $customerGroup 

is returing the current customer group that is id = 5 in my case. How to get the product price from customer group?

Can anyone look into this and update me your ideas. Thanks

Best Answer

Try this one but don't use ObjectManager, you can inject Magento\Catalog\Model\Product & \Magento\Customer\Api\GroupRepositoryInterface class in constructor:

$productId = 9;//your product id here

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);

$productAllTierPrices = $_product->getData('tier_price');

$allTierPrices = array();
foreach($productAllTierPrices as $tierPrices){
    $groupname = $objectManager->create('\Magento\Customer\Api\GroupRepositoryInterface')-> getById($customerGroupId)->getCustomerGroupCode();
    $allTierPrices[$groupname] = $tierPrices['price'];
}

echo "<pre>";
print_r($allTierPrices);
Related Topic