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.