Here's the code for reference:
file: _di.xml_
...
<type name="Magento\Framework\Pricing\Adjustment\Collection">
<arguments>
<argument name="adjustments" xsi:type="array">
<item name="test_pricing_adjustment" xsi:type="const">Vendor\Namespace\Pricing\Adjustment::ADJUSTMENT_CODE</item>
</argument>
</arguments>
</type>
<type name="Magento\Framework\Pricing\Adjustment\Pool">
<arguments>
<argument name="adjustments" xsi:type="array">
<item name="test_pricing_adjustment" xsi:type="array">
<item name="className" xsi:type="string">Vendor\Namespace\Pricing\Adjustment</item>
<item name="sortOrder" xsi:type="string">9999</item>
</item>
</argument>
</arguments>
</type>
...
file: _Adjustment.php_
...
{
const ADJUSTMENT_CODE = 'test_pricing_adjustment';
protected $sortOrder;
public function __construct($sortOrder = null)
{
$this->sortOrder = $sortOrder;
}
public function getAdjustmentCode()
{
return self::ADJUSTMENT_CODE;
}
public function isIncludedInBasePrice()
{
return false;
}
public function isIncludedInDisplayPrice()
{
return true;
}
public function extractAdjustment($amount, SaleableInterface $saleableItem, $context = [])
{
return 2;
}
public function applyAdjustment($amount, SaleableInterface $saleableItem, $context = [])
{
return $amount + 2;
}
public function isExcludedWith($adjustmentCode)
{
return $this->getAdjustmentCode() === $adjustmentCode;
}
public function getSortOrder()
{
return $this->sortOrder;
}
}
It is hard to understand how exactly this feature should work, since there is little to nothing documentation on this subject, but as for me the expected result is that product price is adjusted using logic in the adjustment class on all pages thorough M2
Product price adjustment is applied on PDP and Category page, but once item is added to quote - adjustment is discarded.
@arthurlataks thank you for your report.
Please use the issue reporting guidelines to report an issue. Please, edit issue description and include the preconditions, the steps required to reproduce, the actual result, and the expected result.
Please, add the following information:
@magento-engcom-team Comment is updated as per your comment.
@arthurlataks thank you for your bug report.
We've created internal ticket MAGETWO-75331 to track progress on the issue.
I would like to work on this isssue.
After the researching in source code and architectural concept, I have found out that the Price Adjustments should not affect item's prices/totals when being added to quote.
Magento uses them separately from total collectors in order to adjust prices before rendering on storefront (similarly to price.html in Magento 1.*).
In order to get these adjustments in quote and quote items, developer needs to move his adjustment's logic to separated service (calculator), implement the quote total collector and use his service from total collector as well as from adjustment.
Thanks for the comment and looking into this @strell.
Implementing a quote total collector is quite different from changing the quote item price though. The former adds a new totals row, adding or subtracting an amount. The latter directly changes a quote item's price. (Please correct me if I'm wrong.)
Is there some official method or guide on implementing custom price changes for quote items (not custom totals collector)?
I was really hoping this Pricing Library feature would be it. At the moment I need to do workarounds, such as calling collect totals in plugin events, and so on - and it doesn't quite seem to work consistently.
CC @arthurlataks
Hi @erfanimani, @arthurlataks,
If you need just to change the item price before totals you can implement your own price model for specific product type (or to the custom one, depends on your needs) and put the logic there. In this case, the price will be changed before being added to the shopping cart and will be also changed before showing on a storefront.
But in case if you want to do some price adjustments (for ex. web-shop fee, etc) you need to implement both - the adjustment model and the total collector. Adjustments are being used for run-time price calculation (when you are showing it on PLP or PDP) while total collectors are being used to make a final price calculation before the item will get added to the quote (and on totals recollection).
For instance, we have a tax calculator which adds the tax value on PDP and PLP using adjustments and adds taxes to quote items when the item gets added to quote using total collector. (the same exist for wee taxes)
With the total collector, you can add a new total row as well as change/modify quote item`s prices (depending on your needs).
@strell
Thanks for looking into this issue. Is it possible to add some more information about price adjustments to documentation then? So it is clear right away that this functionality will not amend price of the quote item.
@strell, champion. Thanks for clarifying.
Hi @arthurlataks,
Yeah, it would be great to have a more extended explanation how prices adjustments and total collectors are working, could you create an issue in devdocs of Magento? Then Magento team can take care if this part of the documentation.
@strell Ok, cool, I will add new issue there referencing this issue.
Most helpful comment
Hi @erfanimani, @arthurlataks,
If you need just to change the item price before totals you can implement your own price model for specific product type (or to the custom one, depends on your needs) and put the logic there. In this case, the price will be changed before being added to the shopping cart and will be also changed before showing on a storefront.
But in case if you want to do some price adjustments (for ex. web-shop fee, etc) you need to implement both - the adjustment model and the total collector. Adjustments are being used for run-time price calculation (when you are showing it on PLP or PDP) while total collectors are being used to make a final price calculation before the item will get added to the quote (and on totals recollection).
For instance, we have a tax calculator which adds the tax value on PDP and PLP using adjustments and adds taxes to quote items when the item gets added to quote using total collector. (the same exist for wee taxes)
With the total collector, you can add a new total row as well as change/modify quote item`s prices (depending on your needs).