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

时间:2017-08-01 05:25:59

标签: php magento2

我试图覆盖magento 2扩展帮助文件,但它无效。

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

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

1 个答案:

答案 0 :(得分:0)

//尝试使用以下代码进行帮助程序覆盖。

第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