Skip to content

TypeError: Argument 3 passed to Plugin::around must be an instance of Totals, Totals/Interceptor given, called in Interceptor.php on line 135

I am facing this error

TypeError: Argument 3 passed to CatwalkTransitCoverPluginFeesPlugin::aroundAddTotal() must be an instance of MagentoSalesBlockAdminhtmlOrderInvoiceTotals, instance of MagentoSalesBlockOrderTotalsInterceptor given, called in vendor/magento/framework/Interception/Interceptor.php on line 135 and defined in app/code/Catwalk/TransitCover/Plugin/FeesPlugin.php:24

Here is my plugin code

app/code/Catwalk/TransitCover/Plugin/FeesPlugin.php

<?php
declare(strict_types=1);

namespace CatwalkTransitCoverPlugin;

// use AmastyExtrafeeBlockSalesFees;
// use MagentoSalesBlockAdminhtmlOrderInvoiceTotals;
// use AmastyExtrafeeModelExtrafeeOrder;

class FeesPlugin
{

    /**
     * @param AmastyExtrafeeBlockSalesFees $subject
     * @param Closure $proceed
     * @param MagentoSalesBlockAdminhtmlOrderInvoiceTotals $parent
     * @param string $code
     * @param string $taxDisplay
     * @param float $feeAmount
     * @param float $baseFeeAmount
     * @param string $feeLabel
     * @param string $feeOptionLabel
     */
    public function aroundAddTotal(
        AmastyExtrafeeBlockSalesFees $subject,
        Closure $proceed,
        MagentoSalesBlockAdminhtmlOrderInvoiceTotals $parent,
        $code, 
        $taxDisplay, 
        $feeAmount, 
        $baseFeeAmount, 
        $feeLabel, 
        $feeOptionLabel
        )
    {
        // Change the label to 'Transit Cover'
        $feeLabel = __('Transit Cover');

        return $proceed($parent, $code, $taxDisplay, $feeAmount, $baseFeeAmount, $feeLabel, $feeOptionLabel);
    }
}

Here is the file i want to override

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 AmastyExtrafeeBlockSales;

use AmastyExtrafeeModelConfigProvider;
use AmastyExtrafeeModelExtrafeeOrder;
use MagentoFrameworkViewElementTemplate;
use MagentoSalesBlockAdminhtmlOrderInvoiceTotals;
use MagentoTaxModelConfig;

class Fees extends Template
{
    public const FEE_CODE = 'amasty_extrafee';

    /**
     * @var ConfigProvider
     */
    protected $configProvider;

    public function __construct(
        TemplateContext $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 MagentoFrameworkDataObject(
            [
                'code' => $code,
                'strong' => false,
                'value' => $feeAmount,
                'base_value' => $baseFeeAmount,
                'label' => __('Extra Fee %1: %2 (%3)', $taxDisplay, $feeLabel, $feeOptionLabel),
            ]
        );

        $parent->addTotal($fee, 'shipping');
    }
}