How To Override Product NewWidget Block in Magento 2

Magento 2 recommends not to edit the default core files as you may lose your customizations when upgrading to the latest Magento 2 version. The better solution to this issue is to modify the core functionality by overriding the core files, classes, blocks, and methods to serve this purpose.

Sometimes it requires placing the related template file into the override block with the same directory path as the original when we override the widget block class.

Follow this method to override product NewWidget block in Magento 2 when you want to modify new widget block class.

Method to Override Product NewWidget Block in Magento 2

1. Create di.xml file at app/code/Vendor/Module/etc into the custom module.

<?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\Catalog\Block\Product\Widget\NewWidget"
              type="Vendor\Module\Block\Product\Widget\NewWidget"/>
</config>

2. Create a block file NewWidget.php at app/code/Vendor/Module/Block/Product/Widget to extend the widget class.

<?php
namespace Vendor\Module\Block\Product\Widget;
use Magento\Catalog\Block\Product\Widget\NewWidget;
class NewWidget extends NewWidget
{
}

Do consider while using preference that another module has not used the same preference in the di.xml file.

That’s it! Likewise you can also override product view page social meta tags in Magento 2, as a store owner it will help you to hide social meta tag or dynamically change content of social meta tags.

Feel free to share the solution with Magento Community via social media.

Thank You.

Sanjay Jethva

Article by

Sanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for...