0

Not able to override grand.php file can anyone help me out.

1 Answer 1

3

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/

12
  • 1
    Thank you and let me try. Oct 13, 2020 at 7:36
  • Hi its not reflecting in my localsetup frontend Oct 13, 2020 at 10:31
  • 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
    Thank you.Its working fine Oct 16, 2020 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.