12

While browsing the code of magento 2.0.7's catalogrules module, i came accross a class Magento\CatalogRule\Model\Product\PriceModifier, Which was passed to Magento\Catalog\Model\Product\PriceModifier\Composite class as an argument via the di.xml of the catalogrules module.

Now the PriceModifier has a method named modifyPrice which takes existing price of the Product and modifies using its own mechanism and returns the new price.

I tried to test this functionality to check if it works by mimicking the same thing in my custom module, but the method is not called.

I tried to dig deeper into this and look at the Magento\Catalog\Model\Product\PriceModifier\Composite

<?php
/**
 * Composite price modifier can be used.
 * Any module can add its price modifier to extend price modification from other modules.
 *
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Catalog\Model\Product\PriceModifier;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\PriceModifierInterface;
use Magento\Framework\ObjectManagerInterface;

class Composite implements PriceModifierInterface
{
    /**
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $objectManager;

    /**
     * @var array
     */
    protected $modifiers;

    /**
     * @param ObjectManagerInterface $objectManager
     * @param array $modifiers
     */
    public function __construct(ObjectManagerInterface $objectManager, array $modifiers = [])
    {
        $this->objectManager = $objectManager;
        $this->modifiers = $modifiers;
    }

    /**
     * Modify price
     *
     * @param mixed $price
     * @param Product $product
     * @return mixed
     */
    public function modifyPrice($price, Product $product)
    {
        foreach ($this->modifiers as $modifierClass) {
            $price = $this->objectManager->get($modifierClass)->modifyPrice($price, $product);
        }
        return $price;
    }
}

As we can see, the modifyPrice loops through all the modifiers, and calls their respective modifyPrice method and returns the modified price.

But the method is not called from any where. I need to know if this code is used from anywhere or is it just an unused code? And in case if it is not used, do we have any alternate mechanism to modify the price of product using similar approach? Any suggestion will be appreciated.

3 Answers 3

3

Price modifiers are applied during catalog price indexing. In order to add your modifier declare it in di.xml as follows:

    <type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BasePriceModifier">
        <arguments>
            <argument name="priceModifiers" xsi:type="array">
                <item name="yourPriceModifier" xsi:type="object">Your\Module\Model\Product\PriceModifier</item>
            </argument>
        </arguments>
    </type>

and then create a class implementing Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\PriceModifierInterface.

1

Define in di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Product\PriceModifier\Composite">
        <arguments>
            <argument name="modifiers" xsi:type="array">
                <item name="custom_price" xsi:type="string">CompanyName\ModuleName\Model\Product\PriceModifier</item>
            </argument>
        </arguments>
    </type>
</config>

and create one modifier class like below

<?php


namespace CompanyName\ModuleName\Model\Product;


use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\PriceModifierInterface;

class PriceModifier implements PriceModifierInterface
{

    /**
     * Modify price
     *
     * @param mixed $price
     * @param Product $product
     * @return mixed
     */
    public function modifyPrice($price, Product $product)
    {
        return 50;
    }
}
0

I stumple on this doc yesterday

http://devdocs.magento.com/guides/v2.0/architecture/behavior/pricing.html

Seemse to be a well kept secret.

I successfully changed alle price using a little amount of code.

You just have to add a extra plugin on the finalprice method to save the price to correct way to the quote.

I will finish a blog about this with sample module next week.

1
  • 1
    Well, seems that functionality is gone now!
    – Erfan
    Commented Oct 19, 2018 at 9:19

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.