skip to Main Content

I need to add extension attributes for core api endpoint -> https://magento.redoc.ly/2.4.5-admin/tag/ordersidcomments#operation/PostV1OrdersIdComments

Created below before plugin


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

Created extension_attributes.xml file also

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

While calling api in postman -> /rest/V1/orders/2/comments

Getting below error

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

Getter and Setter are not setting up. Please help me out here.

2

Answers


  1. You need to optimize your code or share your whole module repo. However, the error is pretty that you didn’t create the setter and getter method of custom_id in the interface OrderStatusHistoryExtensionInterface and need to implement it in the class implementing the interface.

    Magento ExtensionInterfaces is required to have all the methods that are declared in the extension_attributes.xml.

    please create a setter and method in your custom interface like:

    /**
     * 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);
     
    

    The default interface in Magento 2 is: OrderStatusHistoryInterface.php check the link: https://github.com/pepe1518/magento2/blob/master/vendor/magento/module-sales/Api/Data/OrderStatusHistoryInterface.php

    Follow and check the full documentation of Sales Order History:
    https://www.magentoextensions.org/documentation/interface_magento_1_1_sales_1_1_api_1_1_data_1_1_order_status_history_interface.html

    Login or Signup to reply.
  2. appcodeVendorExtensionSetupInstallData.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();
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search