Magento 2, You can create a plugin to TotalsInformationManagementInterface of Checkout Module to modify or change quote total.
An Interface responsible for Calculate quote totals based on address and shipping method.
Interface class:
Magento\Checkout\Api\TotalsInformationManagementInterface
An interface contains the calculate() method to fetch quote totals based on the address and shipping method.
if you need to modify the total information you can use before plugin based on your total customization.
Path: app/code/Jesadiya/TotalInfo/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\Checkout\Api\TotalsInformationManagementInterface"> <plugin name="total_modify_plugin" type="Jesadiya/TotalInfo\Plugin\TotalsInformationManagementPlugin" sortOrder="10"/> </type> </config>
Now you need to create a PHP class file to defining our logic before calculate the final quote total.
Path: app/code/Jesadiya/TotalInfo/Plugin/TotalsInformationManagementPlugin.php
<?php namespace Jesadiya\TotalInfo\Plugin; use Magento\Quote\Model\Quote; use Magento\Quote\Api\CartRepositoryInterface; use Magento\Checkout\Api\Data\TotalsInformationInterface; use Magento\Checkout\Api\TotalsInformationManagementInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; class TotalsInformationManagementPlugin { /** * @var CartRepositoryInterface */ private $cartRepository; public function __construct( Config $config, CartRepositoryInterface $cartRepository ) { $this->cartRepository = $cartRepository; } /** * @param TotalsInformationManagementInterface $subject * @param int $cartId * @param TotalsInformationInterface $addressInformation * * @return mixed[]|null * @throws LocalizedException * @throws NoSuchEntityException * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeCalculate( TotalsInformationManagementInterface $subject, int $cartId, TotalsInformationInterface $addressInformation ) { // You got the current quote by cart id and modify your code based on customization /** @var Quote $quote */ $quote = $this->cartRepository->get($cartId); // start your own logic..... return null; } }
Using the above method beforeCalculate() you can modify the behaviour of the calculate method.
You can also use AfterCalculate() method if you want to change the total info after the final total calculated.