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
The block that I was trying to extend has already been extended by another block in:
So in general, all I needed to do is to extend that block mentioned above.
If you override a block, you also want to add a
sequence
in yourmodule.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 moduleMagento_Sales
to yoursequence
.If that doesn’t work:
Posting this solution for Magento v-2.3.5
Override this class MagentoSalesBlockAdminhtmlOrderTotals
app/code/Taktheer/ExtendCoupanTotals/etc/di.xml
app/code/Taktheer/ExtendCoupanTotals/Rewrite/Magento/Sales/Block/Adminhtml/Order/Totals.php
======================== Happy Coding =============================