0

I have to add 2 new methods getter and setter in CartItemInterface but i am facing issues. Basically I have added a new column in quote item table and now I want to add this column in cart api.

Please let me know how I can achieve that ? I'm using magento2 version 2.3.

3
  • You cannot override interface classes, create your own custom interface. Magento 2 does not allow us to override the interfaces. Commented Dec 15, 2022 at 13:25
  • What if I have to add a new method in existing interface ? I mean I want to add extra method in magento2 core interface. What is the best approach ?
    – Asad Ali
    Commented Dec 15, 2022 at 13:42
  • You can use Extension attributes, it is used to extend functionality. Commented Dec 15, 2022 at 13:49

2 Answers 2

0

Use below code for adding extension attributes in extension_attributes.xml :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\Quote\Api\Data\CartItemInterface">
<attribute code="custom_attribute_flag" type="string" />
</extension_attributes>
</config>
2
  • I have added extension attribute but it doesn't appear in my quote item table so i have added it by declarative schema and now I want to add it in cart API payload but when i added it and hit the API it gives me an error like "custom_column" doesn't have accessor method. After doing some R&D i came to know that there should be a getter and setter method for custom_column and these are written in cart item interface which is magent core class and I can't change it directly. Please let me know how I can achieve that ?
    – Asad Ali
    Commented Dec 16, 2022 at 15:06
  • Yes, It give you error. You should set the value by extension attribute. Please check this tutorial: developer.adobe.com/commerce/php/development/components/… Commented Dec 16, 2022 at 15:16
0

Extension Attribute gives you declarative way of extending existing interfaces. You can start by creating, etc/extension_attributes.xml Follow this link , https://www.rohanhapani.com/how-to-use-extension-attributes-in-magento-2/ For reference.

1
  • I have added extension attribute but it doesn't appear in my quote item table so i have added it by declarative schema and now I want to add it in cart API payload but when i added it and hit the API it gives me an error like "custom_column" doesn't have accessor method. After doing some R&D i came to know that there should be a getter and setter method for custom_column and these are written in cart item interface which is magent core class and I can't change it directly. Please let me know how I can achieve that ?
    – Asad Ali
    Commented Dec 16, 2022 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.