Question

I want to modification in the product price. For example, if the product has priced $13.45 then it should display $13 but the price is more than $13.50 it should be $14.

So how to modify product price?

Was it helpful?

Solution

First you need to override Magento\Catalog\Model\Product using di.xml

Path of di.xml: Vendor/Module/etc/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">
    <plugin name="productpricechange" type=" Vendor\Module\Plugin\Product" sortOrder="1" />
  </type>
</config>

Now create a plugin file path should be: Vendor/Module/Plugin/Product.php

<?php

namespace Vendor\Module\Plugin;

    class Product
    {
        public function afterGetPrice(\Magento\Catalog\Model\Product $subject, $result)
        {
            return round($result);
        }
    }

I hope this will help

OTHER TIPS

round Function

<?php
$price = 13.45;
echo(round($price)); //output 13

$price_change = 13.50;
echo(round($price_change)); //output 13
?>

First, you need to create di.xml for create plugin :

/app/code/Vendor/Module/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> 
<type name="Magento\Directory\Model\Currency">
    <plugin name="vendor_module" type="Vendor\Module\Model\Plugin\Currency" />
</type>

Now, create plugin file

/app/code/Vendor/Module/Model/Plugin/Currency.php

<?php
namespace Vendor\Module\Model\Plugin;
use Magento\Framework\Exception\InputException;

class Currency
{
    public function aroundConvert($subject, $proceed, $price, $toCurrency = null)
    {
        $price = $proceed($price, $toCurrency);
        // Your logic
        return $price; // It will be affect in price of shipping also.
    }
}

It will be rounding price with Multi-Currency Stores.

For reference : click here

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top