覆盖Magento 2中的接口



我在我的Magento 2.1.3安装中发现了一个缺陷,并将其缩小到我的数据库中的一个关键关系,即Magento社区中的开放票证。

长话短说,我似乎可以解决问题的唯一方法是更改核心 Magento 文件中的常量。由于Magento 2使用依赖注入,因此我正在尝试覆盖具有需要更改的常量的接口。

我的问题在于我一直在尝试覆盖该界面,就像我对模型、布局等进行其他修改一样,但似乎没有任何效果。以下是我认为与此问题相关的几个文件。我还搜索了上下,以更好地了解PHP中的接口是什么以及它与Magento 2生态系统的关系,但没有任何帮助。

注意:为了保护隐私,我提取了我们的供应商名称并替换为[供应商名称]。此外,我有所需的注册.php文件,并且安装程序:升级运行没有错误。

app/code/[vendorname]/[vendorname]Customer/API/AddressMetadataInterface.php

namespace MagentoCustomerApi;
/**
* Interface for retrieval information about customer address attributes metadata.
* @api
*/
interface AddressMetadataInterfaceRev extends MetadataInterface
{
const ATTRIBUTE_SET_ID_ADDRESS = 6;
const ENTITY_TYPE_ADDRESS = 'customer_address';
const DATA_INTERFACE_NAME = 'MagentoCustomerApiDataAddressInterface';
}

app/code/[vendorname]/[vendorname]Customer/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="MagentoCustomerApiAddressMetadataInterface" type="[vendorname][vendorname]CustomerApiAddressMetadataInterface" />
</config>

使用此代码。尝试运行安装程序时出现以下错误:di:compile

Fatal error: Cannot declare interface MagentoCustomerApiAddressMetadataInterface, because the name is already in use in /Users/[username]/Sites/[sitename]/app/code/[vendorname]/[vendorname]Customer/Api/AddressMetadataInterface.php on line 13

任何帮助将不胜感激。

看起来您需要更改首选项类的命名空间以匹配模块的命名空间而不是Magento的命名空间。然后你应该让你的类扩展原始的(MagentoCustomerApiAddressMetadataInterface),这样你只需要覆盖需要更改的常量:

app/code/[YourVendor]/[ModuleName]/Api/AddressMetadataInterface.php

namespace [YourVendor][ModuleName]Api;
/**
* Interface for retrieval information about customer address attributes metadata.
* @api
*/
interface AddressMetadataInterface extends MagentoCustomerApiAddressMetadataInterface
{
const ATTRIBUTE_SET_ID_ADDRESS = 2;
const ENTITY_TYPE_ADDRESS = 'customer_address';
const DATA_INTERFACE_NAME = 'MagentoCustomerApiDataAddressInterface';
}

重要说明

由于常量的性质以及如何在不实例化类(MagentoCustomerApiAddressMetadataInterface::DATA_INTERFACE_NAME)的情况下访问它们的值,我不知道这是否会解决您的问题,因为我刚刚提到的每个用例都引用原始类而不是您的覆盖。据我所知,类首选项仅在通过对象管理器或依赖注入请求类名时才有效。可能值得一些调查。

最新更新