如何覆盖 Magento 2 扩展助手文件?

How to override Magento 2 extension helper file?

我已经尝试覆盖 magento 2 扩展助手文件,但它不起作用。

谁能告诉我如何覆盖 magento2 扩展帮助文件?

我需要覆盖自定义扩展中的 Data.php 文件。

//尝试使用下面的代码来覆盖帮助程序。

第 1 步:

app/code/YourCompany/YourModule/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="Magento\Catalog\Helper\Data" type="YourCompany\YourModule\Helper\Catalog\Data" />
</config>

步骤:2

Let’s rewrite getProduct() function of class Magento\Catalog\Helper\Data. We will just log some message on var/log/debug.log for this test.

<?php

namespace YourCompany\YourModule\Helper\Catalog;

class Data extends \Magento\Catalog\Helper\Data
{    
    /**
     * Retrieve current Product object
     *
     * @return \Magento\Catalog\Model\Product|null
     */
    public function getProduct()
    {
        // logging to test override    
        $logger = \Magento\Framework\App\ObjectManager::getInstance()->get('\Psr\Log\LoggerInterface');
        $logger->debug('Helper Override Test');

        return $this->_coreRegistry->registry('current_product');
    }
}
?>

More details refer this link