How to override block in Magento 2?

by filiberto , in category: PHP CMS , 2 years ago

How to override block in Magento 2?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by dedrick , a year ago

@filiberto 

In Magento 2, you can override a block by creating a new block class and extending the original block class. Here's an example of how you can do this:

  1. Create a new module and register it in app/code/[Vendor]/[Module]/registration.php:
1
2
3
4
5
6
<?php
MagentoFrameworkComponentComponentRegistrar::register(
    MagentoFrameworkComponentComponentRegistrar::MODULE,
    '[Vendor]_[Module]',
    __DIR__
);


  1. Create a di.xml file in app/code/[Vendor]/[Module]/etc to specify the original block class and the new block class:
1
2
3
4
5
<?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="MagentoCatalogBlockProductView" type="[Vendor][Module]BlockProductView"/>
</preference>
</config>


  1. Create the new block class in app/code/[Vendor]/[Module]/Block/Product/View.php:
1
2
3
4
5
6
7
8
<?php

namespace [Vendor][Module]BlockProduct;

class View extends MagentoCatalogBlockProductView
{
    // Your custom code goes here
}


  1. Run the following commands to enable the new module and clear the cache:
1
2
3
php bin/magento module:enable [Vendor]_[Module]
php bin/magento setup:upgrade
php bin/magento cache:clean


This will override the original block class with your new block class. You can then add custom functionality to the new block class by defining new methods or overriding existing methods.


Note: It is generally recommended to use plugins or preferences to extend or modify core functionality in Magento 2 instead of directly overriding classes. However, in some cases, overriding a block may be necessary.

Member

by brandy , 10 months ago

@filiberto 

To override a block in Magento 2, you can follow these steps:

  1. Create a new module: First, create a new module in Magento 2 by creating the necessary module files. You can refer to the official Magento 2 documentation to create a module.
  2. Create a layout file: In your module, create a layout file your_module_name/view/frontend/layout/your_layout_handle.xml where your_layout_handle is the specific layout handle for the page where you want to override the block.
  3. Declare the block override: In the layout file, declare the block override using the
  4. Create a template file: Create a template file at your_module_name/view/frontend/templates/your_template.phtml that will be used as the new template for the overridden block. Customize the template as per your requirements.
  5. Clear the cache: After making the changes, clear the Magento cache by running the following command: php bin/magento cache:clean.
  6. Test the changes: Finally, test the changes by visiting the page where you have overridden the block. It should now use your custom template instead of the default template.


Remember to always follow the best practices and keep your changes in a separate module rather than modifying the core code directly.