magento2 – Display different price using customer’s custom attribute

Note :
I want to display different price using customers’s custom attribute i.e. custom_group. But one problem occured and i want to explain using below example.

  • I have generated issue with magento 2.4.2 version with fresh setup
  • Enabled all the cache

Right now i am just checking condition based on customer’s first name because i want to make example very easy so any people can easily generate this issue. No need to create custom_group attribute and plugin for that. This issue generated using any attrubute so i just checked with first name.

Suppose i have 3 customer in my site and all 3 customer have first name like below.

  1. A
  2. B
  3. C

In my site only simple products found. So i am returning price based on customer’s firstname and condition like below. I have just directly put code in file and file path is below

vendor/magento/module-catalog/Model/Product/Type/Price.php

I have changed code in getPrice() function. You can also make after plugin for getPrice function but i have directly put in core file becuase i want to make things easy for you. So you do not need to create plugin and custom attribute for this issue. You can check how getPrice function looks like

public function getPrice($product)
{
    $objectManager = MagentoFrameworkAppObjectManager::getInstance(); // Do not use object manager directly. You can inject in constructor
    $customer = $objectManager->get('MagentoCustomerModelSessionFactory')->create()->getCustomer();
    $price = $product->getData('price');
    if($customer->getFirstname() == 'A') {
        return $price - (($price * 10) / 100); // 10% of price
    } else if($customer->getFirstname() == 'B') {
        return $price - (($price * 20) / 100); // 20% of price
    } else {
        return $price - (($price * 50) / 100); // 50% of price
    }
}

As per above code

  • If customer Firstname is A then getting 10% discount of product price
  • If customer Firstname is B then getting 20% discount of product price
  • If customer Firstname is any other except A and B then getting 50% discount of product price

Suppoose i have one product name is Product-A and price is 100 rupees. So price should be display like below
A – 90 rs.
B – 80 rs.
Other than A and B – 50 rs.

Issue :
Case 1 : When i loggend in with customer whose firstname is A then display price 90rs. Same time i am logged in with customer whose firstname is B in private window and i can see B also getting 10% discount instead of 20%. Also i have trued with all the customer and they also getting 10% discount instead of 50%.

Case 2 : Now i have just flush cache and directly referesh page of customer whose firstname is B then i can see 20% discount. But when i referesh page of customer whose firstname is A then A is getting 20% discount instead of 10%. Also i have tried with other customer and they also getting 20% instead of 50%

And yes this issue only in display. When i am adding that product to cart then cart showing me correct price. So issue is related to caching. This issue is generated when i am enabling block html and fpc. If these both cache are disabled then no issue generated. But that is not solution because if i can disable this cache then my site become very slow.

Also when i am assigning this 2 customer in 2 different group(magento default group) then also showing me correct price so magento caching page based on customer group.

I want to display correct price with enabling block html and fpc. Also want to keep all the customer in same magento group and want to resolved this issue.

Thanks in advance.

Any help would be appreciated!