2

I am a magento 2 starter with much experience in magento 1. I can't find the relevant scripts which are used to calculate the price for configurable products.

I need a basic guide on how the configuration works please so that I can modify it.

E.g. I need to add a customise-able option with title Length and the price should change based on it's value.

So if the price is $1000 and 1000 millimetres is set then the price should be $1000,

if 500 millimetres is set, then the price should be 500$.

If 2000 millimetres is set then the price should be 2000$ and so on...

price = (price * (length / 1000))

Where do I have to add this logic?

1

Magento 2 config product price is different with Magento 1.

  • In Magento 1 the price is calculated based on super attributes.

  • In Magento 2 the price is taken from child product.

In your case, we have two methods:

  1. Using Magento Backend: Generate the children with option Apply unique prices by attribute to each SKU, enter fixed price to each value of length attribute.

  2. Using Magento source code: create a 3rd module and override getValue() function in /vendor/magento/module-catalog/Pricing/Price/BasePrice.php class using di.xml. The code should be:

    public function getValue()
    {
        if ($this->value === null) {
            $this->value = false;
            foreach ($this->priceInfo->getPrices() as $price) {
                if ($price instanceof BasePriceProviderInterface && $price->getValue() !== false) {
                    $this->value = min($price->getValue(), $this->value ?: $price->getValue());
                }
            }
        }
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->create('Magento\Catalog\Model\Product')->load($this->product->getId());
        if ($product) {
            $length = $product->getResource()->getAttribute('length')->getFrontend()->getValue($product);
            if ($length) {
                $this->value = $this->value * ($length / 1000);
            }
        }
        return $this->value;
    }
    

    Tested in Magento 2.3.0

That's it.

| improve this answer | |
  • But the length attribute ranges from 0 to 6000, so do I need to create 6000 values? I think it would be better to create a logic so that the price is multiplied by length / 1000. – Black Jul 8 '19 at 10:10
  • @Black So it's configurable product + length or simple product + length. So you will need to override the Core function in order to change the price. Let create an override module for this. – Jamy Jul 8 '19 at 10:22
  • And where are the relevant files? I also need to do this for the price display on the frontend, how does magento know which price to show in frontend? Does it work by javascript, is a request to a controller made? – Black Jul 8 '19 at 10:34
  • @Black If you only want to change the base price (excluding tax and option prices), let override this file /vendor/magento/module-catalog/Pricing/Price/BasePrice.php. Add this line at the end of getValue() function (before return): $this->value = $this->value * ($this->product->getLength / 1000)) – Jamy Jul 8 '19 at 10:38
  • ok, but note that not every configurable product will have a length. Does it also take care of the calculation in the frontend? – Black Jul 8 '19 at 10:41

Your Answer

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.