1

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 = \Magento\Framework\App\ObjectManager::getInstance(); // Do not use object manager directly. You can inject in constructor
    $customer = $objectManager->get('\Magento\Customer\Model\SessionFactory')->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.

This is not magento's issue because magento is caching page based on customer group but this is my custom requirement where i want to keep all the customer in same group and want to display different price based on csutomer attribute

Thanks in advance.

Any help would be appreciated!

5
  • You will probably have to load the relevant price using Ajax to get around the cacheing.
    – jiheison
    May 31, 2021 at 5:12
  • Yes this is last option to display price using ajax. Is there another way? May 31, 2021 at 13:27
  • Asynchronous loading is the only way that I know of that maintains overall page cacheing while delivering customer-specific content. Its how many Varnish hole-punching implementations work, and Magento's own "private content" features.
    – jiheison
    May 31, 2021 at 16:50
  • Have you tried tier/group pricing under advanced pricing? You can set prices per customer group, pick All websites, pick customer group for name A, qty as 1 and give your price as %, do the same for group B and C as well..
    – rukrlf
    Dec 20, 2021 at 4:01
  • Did you find a solution for this? Jan 15 at 8:27

1 Answer 1

-1

You need to use magneto default functionality for this one.

  1. Go To Magento Admin.
  2. Catalog -> Products
  3. Edit Any Product
  4. Click on "Advanced Pricing"
  5. There is the option of this "Customer Group Price"
1
  • I know this way. I wrote all my requirements in question May 31, 2021 at 13:26

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.