Not able to override grand.php file can anyone help me out.
1 Answer
You can create di.xml in etc/di.xml or if you want it to be area specific you can put in etc/frontend/di.xml or which ever area you want to override. Example of global preference app/code/StackoverFlow/GrandOverride/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">
<preference for="Magento\Quote\Model\Quote\Address\Total\Grand"
type="StackoverFlow\GrandOverride\Model\Quote\Address\Total\Grand"/>
</config>
and in file app/code/StackoverFlow/GrandOverride/Model/Quote/Address/Total/Grand.php you can override methods of the file
<?php
namespace StackoverFlow\GrandOverride\Model\Quote\Address\Total;
class Grand extends \Magento\Quote\Model\Quote\Address\Total\Grand
{
}
As you want to update public method i would suggest to use a plugin . You can it by adding below code in app/code/StackoverFlow/GrandOverride/etc/di.xml(remove preference you added before )
and in plugin class **StackoverFlow\GrandOverride\Plugin\PrecisionUpdate**<?php
namespace StackoverFlow\GrandOverride\Plugin;
use Magento\Quote\Api\Data\ShippingAssignmentInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Address\Total;
use Magento\Quote\Model\Quote\Address\Total\Grand;
use Magento\Quote\Model\ShippingAssignment;
class PrecisionUpdate
{
/**
* @param Grand $subject
* @param Grand $result
* @param Quote $subject
* @param Grand $result
* @param ShippingAssignmentInterface $subject
* @param Grand $result
* @param Total $subject
* @param Grand $result
* @return Grand
*/
public function afterCollect(Grand $subject, Grand $result, Quote $quote, ShippingAssignment $shippingAssignment, Total $total)
{
$grandTotal = $this->priceRounder->roundPrice($total->getGrandTotal(), 0);
$baseGrandTotal = $this->priceRounder->roundPrice($total->getBaseGrandTotal(), 0);
$total->setGrandTotal($grandTotal);
$total->setBaseGrandTotal($baseGrandTotal);
return $result;
}
}
Update the plugin logic as you require , i have not tested the code . And hope you have created a new module , this code will not be executed untill you register your module . you can find details here https://devdocs.magento.com/videos/fundamentals/create-a-new-module/
-
1
-
-
Here is the path for you app/code/Roundoff/GrandOverride/Model/Quote/Address/Total Oct 13, 2020 at 10:32
-
not reflected in frontend ? can you please explain what is not shown in frontend ? Oct 14, 2020 at 7:43
-
1