3

I want to override Magento\Sales\Block\Adminhtml\Totals this block I did but I don't know what I'm doing wrong.

app/code/[Vendor]/[Module]/etc/adminhtml/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\Sales\Block\Adminhtml\Totals" type="EC\InStorePickupTax\Block\Adminhtml\Totals"/>
</config>

app/code/[Vendor]/[Vendor]/Block/Adminhtml/Totals.php

<?php

namespace [Vendor]\[Vendor]\Block\Adminhtml;

class Totals extends \Magento\Sales\Block\Adminhtml\Totals
{
    /**
     * Initialize order totals array
     *
     * @return $this
     */
    protected function _initTotals()
    {
        $this->_totals = [];
        $this->_totals['subtotal'] = new \Magento\Framework\DataObject(
            [
                'code' => 'subtotal',
                'value' => $this->getSource()->getSubtotal(),
                'base_value' => $this->getSource()->getBaseSubtotal(),
                'label' => __('Subtotal'),
            ]
        );

        /**
         * Add shipping
         */
        if (!$this->getSource()->getIsVirtual() && ((double)$this->getSource()->getShippingAmount() ||
            $this->getSource()->getShippingDescription())
        ) {
            $shippingLabel = __('Shipping & Handling');
            if ($this->isFreeShipping($this->getOrder()) && $this->getSource()->getDiscountDescription()) {
                $shippingLabel .= sprintf(' (%s)', $this->getSource()->getDiscountDescription());
            }
            $this->_totals['shipping'] = new \Magento\Framework\DataObject(
                [
                    'code' => 'shipping',
                    'value' => $this->getSource()->getShippingAmount(),
                    'base_value' => $this->getSource()->getBaseShippingAmount(),
                    'label' => $shippingLabel,
                ]
            );
        }

        /**
         * Add discount
         */
        if ((double)$this->getSource()->getDiscountAmount() != 0) {
            if ($this->getSource()->getDiscountDescription()) {
                $discountLabel = __('Discount (%1)', $this->getSource()->getDiscountDescription());
            } else {
                $discountLabel = __('Discount');
            }
            $this->_totals['discount'] = new \Magento\Framework\DataObject(
                [
                    'code' => 'discount',
                    'value' => $this->getSource()->getDiscountAmount(),
                    'base_value' => $this->getSource()->getBaseDiscountAmount(),
                    'label' => $discountLabel,
                ]
            );
        }

        if($this->getOrder()->getShippingMethod() == 'flatrate_flatrate'){
            $this->_totals['grand_total'] = new \Magento\Framework\DataObject(
                [
                    'code' => 'grand_total',
                    'strong' => true,
                    'value' => $this->getOrder()->getGrandTotal(),
                    'base_value' => $this->getOrder()->getBaseGrandTotal(),
                    'label' => __('Grand Total'),
                    'area' => 'footer',
                ]
            );
        }else{
            $this->_totals['grand_total'] = new \Magento\Framework\DataObject(
                [
                    'code' => 'grand_total',
                    'strong' => true,
                    'value' => $this->getSource()->getGrandTotal(),
                    'base_value' => $this->getSource()->getBaseGrandTotal(),
                    'label' => __('Grand Total'),
                    'area' => 'footer',
                ]
            );
        }

        return $this;
    }
}

Also I also override Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer block, it's working but I know why another block is not.

3 Answers 3

4

I found it, Basically there is a Block Magento\Sales\Block\Adminhtml\Order\Invoice\Totals.php which extends Magento\Sales\Block\Adminhtml\Totals, so I override this block Magento\Sales\Block\Adminhtml\Order\Invoice\Totals.php

app/code/[Vendor]/[Module]/etc/adminhtml/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\Sales\Block\Adminhtml\Order\Invoice\Totals" type="EC\InStorePickupTax\Block\Adminhtml\Order\Invoice\Totals"/>
</config>

app/code/[Vendor]/[Module]/Block/Adminhtml/Order/Invoice/Totals.php

<?php

namespace EC\InStorePickupTax\Block\Adminhtml\Order\Invoice;

class Totals extends \Magento\Sales\Block\Adminhtml\Order\Invoice\Totals
{
    /**
     * Initialize order totals array
     *
     * @return $this
     */
    protected function _initTotals()
    {
        parent::_initTotals();

        if($this->getOrder()->getShippingMethod() == 'flatrate_flatrate'){
            $this->_totals['grand_total'] = new \Magento\Framework\DataObject(
                [
                    'code' => 'grand_total',
                    'strong' => true,
                    'value' => $this->getOrder()->getGrandTotal(),
                    'base_value' => $this->getOrder()->getBaseGrandTotal(),
                    'label' => __('Grand Total'),
                    'area' => 'footer',
                ]
            );
        }else{
            $this->_totals['grand_total'] = new \Magento\Framework\DataObject(
                [
                    'code' => 'grand_total',
                    'strong' => true,
                    'value' => $this->getSource()->getGrandTotal(),
                    'base_value' => $this->getSource()->getBaseGrandTotal(),
                    'label' => __('Grand Total'),
                    'area' => 'footer',
                ]
            );
        }
        return $this;
    }
}
2

You might want to try calling the parent constructor to ensure all class dependencies are loaded as expected.

1
  • I think i found my solution, Magento\Sales\Block\Adminhtml\Order\Invoice\Totals.php is extending Magento\Sales\Block\Adminhtml\Totals, now I'm trying to override Magento\Sales\Block\Adminhtml\Order\Invoice\Totals.php lets see. Commented Nov 15, 2019 at 16:09
0

Posting this solution for Magento v-2.3.5

Override this class \Magento\Sales\Block\Adminhtml\Order\Totals

app/code/Taktheer/ExtendCoupanTotals/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\Sales\Block\Adminhtml\Order\Totals" type="Taktheer\ExtendCoupanTotals\Rewrite\Magento\Sales\Block\Adminhtml\Order\Totals"/>
</config>

app/code/Taktheer/ExtendCoupanTotals/Rewrite/Magento/Sales/Block/Adminhtml/Order/Totals.php

<?php
/**
 * Copyright © Unyscape Infocom Pvt. Ltd. All rights reserved.
 * See COPYING.txt for license details.
 */


   declare(strict_types=1);
    
    namespace Taktheer\ExtendCoupanTotals\Rewrite\Magento\Sales\Block\Adminhtml\Order;

class Totals extends \Magento\Sales\Block\Adminhtml\Order\Totals
{
      /**
     * Initialize order totals array
     *
     * @return $this
     */
    protected function _initTotals()
    {
        parent::_initTotals();
        
        $order = $this->getSource();
        
        if ($order->getCouponCode()) {
            $discountLabel = __('Discount (%1)', $order->getCouponCode());
        } else {
            $discountLabel = __('Discount');
        }
        $this->_totals['discount'] = new \Magento\Framework\DataObject(
            [
                'code' => 'discount',
                'value' => $order->getDiscountAmount(),
                'base_value' => $order->getBaseDiscountAmount(),
                'label' => $discountLabel,
       

     ]
        );
        return $this;
    }

}

======================== Happy Coding =============================

Your Answer

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

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