skip to Main Content

I am trying to override magento block but everytime main block from vendor is executed. No errors are shown.

Magento block:

vendor/magento/module_sales/block/adminhtml/totals.php

Created block in custom module:

[vendor]/[module]/block/adminhtml/totals.php

Modified di.xml file in:

[vendor]/[module]/etc/di.xml

Preference in di.xml file:

...
<preference for="MagentoSalesBlockAdminhtmlTotals" 
type="IwaysSalesBlockAdminhtmlTotals" />
...

Content of block in custom module:

namespace IwaysSalesBlockAdminhtml;

use MagentoFrameworkDataObject;
use MagentoSalesBlockAdminhtmlTotals as DefaultTotals;

class Totals extends DefaultTotals
{
...

I have tried to check if file is executed with xdebug but it isnt.

3

Answers


  1. Chosen as BEST ANSWER

    The block that I was trying to extend has already been extended by another block in:

    module_sales/block/adminhtml/order/totals.php
    

    So in general, all I needed to do is to extend that block mentioned above.


  2. If you override a block, you also want to add a sequence in your module.xml. You have to make sure the module of the block you want to override is being loaded before your module is being loaded. See Component load order. Add the module Magento_Sales to your sequence.

    If that doesn’t work:

    • Are you sure your module is registered by Magento, can you find your module in `app/etc/config.php’?
    • Are you sure there’s no other module which overrides that block already?
    Login or Signup to reply.
  3. Posting this solution for Magento v-2.3.5

    Override this class MagentoSalesBlockAdminhtmlOrderTotals

    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="MagentoSalesBlockAdminhtmlOrderTotals" type="TaktheerExtendCoupanTotalsRewriteMagentoSalesBlockAdminhtmlOrderTotals"/>
    </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 TaktheerExtendCoupanTotalsRewriteMagentoSalesBlockAdminhtmlOrder;
    
    class Totals extends MagentoSalesBlockAdminhtmlOrderTotals
    {
          /**
         * 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 MagentoFrameworkDataObject(
                [
                    'code' => 'discount',
                    'value' => $order->getDiscountAmount(),
                    'base_value' => $order->getBaseDiscountAmount(),
                    'label' => $discountLabel,
           
    
         ]
            );
            return $this;
        }
    
    }
    

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search