2

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?

1

3 Answers 3

3

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

2
  • It's working. Thanks. Commented Jul 3, 2019 at 5:23
  • Glad to hear, it help you. please mark my answer correct so it helps other readers as well Commented Jul 3, 2019 at 7:59
1

round Function

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

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

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

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.