为addComment Magento 2创建扩展属性



我需要为核心api端点添加扩展属性->https://magento.redoc.ly/2.4.5-admin/tag/ordersidcomments#operation/PostV1OrdersIdComments

在以下插件之前创建


<type name="MagentoSalesModelServiceOrderService">
<plugin name="set_order_data_plugin" type="NamespaceModuleNamePluginModelOrderSetOrderDataPlugin" 
sortOrder="1"/>
</type>

创建了扩展名_attributes.xml文件,也是

<extension_attributes for="MagentoSalesModelServiceOrderService">
<attribute code="custom_id" type="string" />
</extension_attributes>

在poster中调用api时->休息/V1/订单/2/评论

低于错误

"message": "Property "CustomId" does not have accessor method "getCustomId" in class "Magento\Sales\Api\Data\OrderStatusHistoryExtensionInterface"."

Getter和Setter未设置。请帮帮我。

您需要优化代码或共享整个模块回购。然而,错误很明显,您没有在接口OrderStatusHistoryExtensionInterface中创建custom_id的setter和getter方法,并且需要在实现该接口的类中实现它。

Magento ExtensionInterface需要具有在extension_attributes.xml中声明的所有方法。

请在您的自定义界面中创建一个setter和方法,如:

/**
* Gets the Custom ID for the order status history.
*
* @return int|null Order status history ID.
*/
public function getCustomId();
/**
* Sets custom ID.
*
* @param int $customId
* @return $this
*/
public function setCustomId($customId);

Magento 2中的默认接口是:OrderStatusHistoryInterface.php检查链接:https://github.com/pepe1518/magento2/blob/master/vendor/magento/module-sales/Api/Data/OrderStatusHistoryInterface.php

遵循并检查销售订单历史记录的完整文档:https://www.magentoextensions.org/documentation/interface_magento_1_1_sales_1_1_api_1_1_data_1_1_order_status_history_interface.html

app\code\Vendor\Extension\Setup\InstallData.php

<?php
namespace VendorpExtensionSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();

$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
'your_attribute_id',
[
'type' => 'text',
'label' => 'Attribute Label',
'input' => 'text',
'required' => false,
'sort_order' => 4,
'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
'group' => 'Attribute Groupe',
'note' => 'Attribute Comment'
]
);
$setup->endSetup();
}
}

最新更新