I have created a new module in app code section, Its roll is to override the core file of a third party module, But its not working. Let me share my files app/code/Catwalk/TransitCover/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Catwalk_TransitCover',
__DIR__
);
app/code/Catwalk/TransitCover/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Catwalk_TransitCover" />
<sequence>
<module name="Amasty_Extrafee" />
</sequence>
</config>
app/code/Catwalk/TransitCover/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="Amasty\Extrafee\Block\Sales\Fees" type="Catwalk\TransitCover\Block\Sales\Fees"/>
</config>
app/code/Catwalk/TransitCover/Block/Sales/Fees.php
<?php
namespace Catwalk\TransitCover\Block\Sales;
use Amasty\Extrafee\Block\Sales\Fees as AmastyfeeBlock;
class Fees extends AmastyfeeBlock
{
public function addTotal($parent, $code, $taxDisplay, $feeAmount, $baseFeeAmount, $feeLabel, $feeOptionLabel)
{
$fee = new \Magento\Framework\DataObject(
[
'code' => $code,
'strong' => false,
'value' => $feeAmount,
'base_value' => $baseFeeAmount,
'label' => __('Transit Cover'),
]
);
$parent->addTotal($fee, 'shipping');
}
}
This is all the content of this my developed module. Let me share the code of the file of which i want to overide AddTotal function
vendor/amasty/module-extra-fee/Block/Sales/Fees.php
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) 2022 Amasty (https://www.amasty.com)
* @package Extra Fee for Magento 2
*/
declare(strict_types=1);
namespace Amasty\Extrafee\Block\Sales;
use Amasty\Extrafee\Model\ConfigProvider;
use Amasty\Extrafee\Model\ExtrafeeOrder;
use Magento\Framework\View\Element\Template;
use Magento\Sales\Block\Adminhtml\Order\Invoice\Totals;
use Magento\Tax\Model\Config;
class Fees extends Template
{
public const FEE_CODE = 'amasty_extrafee';
/**
* @var ConfigProvider
*/
private $configProvider;
public function __construct(
Template\Context $context,
ConfigProvider $configProvider,
array $data = []
) {
parent::__construct($context, $data);
$this->configProvider = $configProvider;
}
/**
* @param Totals $parent
* @param ExtrafeeOrder $feeObjectWithOrder
* @return $this
*/
public function getFees($parent, $feeObjectWithOrder)
{
$displayPrices = $this->configProvider->displaySalesPrices();
$feeAmount = $feeObjectWithOrder->getTotalAmount();
$baseFeeAmount = $feeObjectWithOrder->getBaseTotalAmount();
$taxAmount = $feeObjectWithOrder->getTaxAmount();
$baseTaxAmount = $feeObjectWithOrder->getBaseTaxAmount();
$feeId = $feeObjectWithOrder->getFeeId();
$feeOptionId = $feeObjectWithOrder->getOptionId();
$feeLabel = $feeObjectWithOrder->getLabel();
$feeOptionLabel = $feeObjectWithOrder->getOptionLabel();
if ($feeAmount >= 0) {
if ($displayPrices == Config::DISPLAY_TYPE_BOTH) {
$code = self::FEE_CODE . '_excl_tax_' . $feeId . '_' . $feeOptionId;
$this->addTotal($parent, $code, '(Excl.Tax)', $feeAmount, $baseFeeAmount, $feeLabel, $feeOptionLabel);
$feeAmount += $taxAmount;
$baseFeeAmount += $baseTaxAmount;
$code = self::FEE_CODE . '_incl_tax_' . $feeId . '_' . $feeOptionId;
$this->addTotal($parent, $code, '(Incl.Tax)', $feeAmount, $baseFeeAmount, $feeLabel, $feeOptionLabel);
} else {
if ($displayPrices == Config::DISPLAY_TYPE_INCLUDING_TAX) {
$feeAmount += $taxAmount;
$baseFeeAmount += $baseTaxAmount;
}
$code = self::FEE_CODE . '_' . $feeId . '_' . $feeOptionId;
$this->addTotal($parent, $code, '', $feeAmount, $baseFeeAmount, $feeLabel, $feeOptionLabel);
}
}
return $this;
}
/**
* @param Totals $parent
* @param string $code
* @param string $taxDisplay
* @param float $feeAmount
* @param float $baseFeeAmount
* @param string $feeLabel
* @param string $feeOptionLabel
*/
public function addTotal($parent, $code, $taxDisplay, $feeAmount, $baseFeeAmount, $feeLabel, $feeOptionLabel)
{
$fee = new \Magento\Framework\DataObject(
[
'code' => $code,
'strong' => false,
'value' => $feeAmount,
'base_value' => $baseFeeAmount,
'label' => __('Extra Fee %1: %2 (%3)', $taxDisplay, $feeLabel, $feeOptionLabel),
]
);
$parent->addTotal($fee, 'shipping');
}
}
I tried almost everything, not not working for me. Please give me any idea here.
2 Answers 2
In the app/code/Catwalk/TransitCover/etc/module.xml, change:
<module name="Catwalk_TransitCover" />
<sequence>
<module name="Amasty_Extrafee" />
</sequence>
to
<module name="Catwalk_TransitCover">
<sequence>
<module name="Amasty_Extrafee" />
</sequence>
</module>
change the code of this file like this.
app/code/Catwalk/TransitCover/etc/module.xml
<module name="Catwalk_TransitCover">
<sequence>
<module name="Amasty_Extrafee" />
</sequence>
</module>