2

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.

2

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;
    }
}
|improve this answer|||||
1

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

|improve this answer|||||
  • 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. – Partab Saifuddin Zakir Nov 15 '19 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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