Magento 2 override core framework

Magento 2 override core framework

October 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 override core framework

Overriding Core Magento Framework: A Step-by-Step Guide

Are you trying to override the AbstractBlock class in your Magento 2 project, but facing difficulties? You’re not alone. In this post, we’ll walk through a step-by-step guide on how to correctly override core Magento classes using preferences and di.xml.

Understanding the Problem

The issue lies in the way you’ve structured your preference declaration in the di.xml file. Let’s dive into why it’s not working as expected.

Setting Up Your Custom Module

First, ensure you have a custom module set up correctly:

  • Create a new namespace: YourCompany/YourModule.
  • Set up the necessary directories and files.
  • Include your composer.json and module.xml.

Declaring Preferences in di.xml

The di.xml file is where you declare your preferences. However, you’ve missed a crucial part—the root element of the XML file:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Framework\View\Element\AbstractBlock" type="YourCompany\YourModule\Framework\View\Element\AbstractBlock"/>
</config>

Ensure you include the root <config> element. This tells Magento where to find the schema location for validation.

Creating Your Custom AbstractBlock.php

Next, let’s create your custom AbstractBlock.php file:

<?php
namespace YourCompany\YourModule\Framework\View\Element;

abstract class AbstractBlock extends \Magento\Framework\View\Element\AbstractBlock
{
    /**
     * Retrieve child block HTML
     *
     * @param   string $alias
     * @param   boolean $useCache
     * @return  string
     */
    public function getChildHtml($alias = '', $useCache = true)
    {
        die("here");
    }
}

The key here is to use the correct namespace. It should match your custom module structure (YourCompany/YourModule).

Resolving Conflicts: Using Your Custom Class

Magento’s core classes are registered in the global scope, while your preference is registered in a local scope. To resolve this conflict and ensure Magento uses your custom class:

  1. Flush Magento cache: bin/magento cache:flush.
  2. Clear generated files: rm -rf generated/.
  3. Recompile: bin/magento setup:di:compile.

Conclusion

Overriding core Magento classes is a powerful feature but requires careful implementation.

Remember these key takeaways:

  • Always use the correct root element in your di.xml file.
  • Ensure your custom namespace matches your module structure exactly.
  • After making changes, always flush the cache and recompile to ensure your preferences are applied correctly.

If you’re still facing issues, consider reaching out to Magento experts or checking official documentation for more detailed guidance. Happy coding!

Magento 2 Extensions Configuration Deployment

Our network