Categories
Magento

Price Adjustment in Product and Category pages in Magento2

One can adjust the price of saleable entity at product and category page.

For adjustments, create file etc/di.xml in your module:

<type name="Magento\Framework\Pricing\Adjustment\Collection">
  <arguments>
    <argument name="adjustments" xsi:type="array">
      <item name="price_adjustment" xsi:type="const">VENDOR\MODULE\Pricing\Adjustment::ADJUSTMENT_CODE</item>
    </argument>
  </arguments>
</type>
<type name="Magento\Framework\Pricing\Adjustment\Pool">
  <arguments>
     <argument name="adjustments" xsi:type="array">
        <item name="price_adjustment" xsi:type="array">
           <item name="className" xsi:type="string">VENDOR\MODULE\Pricing\Adjustment</item>
           <item name="sortOrder" xsi:type="string">10</item>
        </item>
     </argument>
  </arguments>
</type>

Now create the Adjustment.php file : VENDOR\MODULE\Pricing\Adjustment.php

namespace VENDOR\MODULE\Pricing;
use Magento\Framework\Pricing\Adjustment\AdjustmentInterface;
use Magento\Framework\Pricing\SaleableInterface;
class Adjustment implements AdjustmentInterface
{
    const ADJUSTMENT_CODE = 'price_adjustment';
    const ADJUSTMENT_PRICE = 1.79;
    /**
     * Get adjustment code
     *
     * @return string
     */
    public function getAdjustmentCode()
    {
        return self::ADJUSTMENT_CODE;
    }
    /**
     * Define if adjustment is included in base price
     *
     * @return bool
     */
    public function isIncludedInBasePrice()
    {
        return true;
    }
    /**
     * Define if adjustment is included in display price
     *
     * @return bool
     */
    public function isIncludedInDisplayPrice()
    {
        return true;
    }
    /**
     * Extract adjustment amount from the given amount value
     *
     * @param float $amount
     * @param SaleableInterface $saleableItem
     * @param null|array $context
     * @return float
     */
    public function extractAdjustment($amount, SaleableInterface $saleableItem, $context = [])
    {
        return $amount - self::ADJUSTMENT_PRICE;
    }
    /**
     * Apply adjustment amount and return result value
     *
     * @param float $amount
     * @param SaleableInterface $saleableItem
     * @param null|array $context
     * @return float
     */
    public function applyAdjustment($amount, SaleableInterface $saleableItem, $context = [])
    {
        return $amount + self::ADJUSTMENT_PRICE;
    }
    /**
     * Check if adjustment should be excluded from calculations along with the given adjustment
     *
     * @param string $adjustmentCode
     * @return bool
     */
    public function isExcludedWith($adjustmentCode)
    {
        return $this->getAdjustmentCode() === $adjustmentCode;
    }
    /**
     * Return sort order position
     *
     * @return int
     */
    public function getSortOrder()
    {
        return 21;
    }
}

From the above code one can adjust the price for all the saleable item in the inventory.

ADJUSTMENT_CODE is unique for the adjustment Collection class.

Collection Class : Magento\Framework\Pricing\Adjustment\Collection collection.

The price of the saleable items in the inventory increase to amount ADJUSTMENT_PRICE defined as constant in Adjustment.php.

Functions extractAdjustment and applyAdjustment in Adjustment.php contains the adjustment logic.

Price adjustments affect only the price of the saleable items in product and category pages.

For adjusting the price of specific ID or specific type, one can get the info of saleable item from SaleableInterface $saleableItem. Price adjustments will not affect price of the items in the cart. Resource

Price Adjustment in Product and Category pages in Magento2

If you still have any issue feel free to ask and let us know your views to make the better. Share this solution with your other Magento buddies via social media.

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *