0

The question is specifically about ./Magento/Checkout/Api/Data/ShippingInformationExtension.php and adding extra columns via extension_attributes.xml.

Modules:

  • Source module: Vendor_Module
  • Overriding/extending module: Vendor2_Module2 (extending JS / templates etc / Plugins) from Source module.

Issue

I need to use a custom extension_attributes.xml inside Vendor2_Module2.

Information

  • The source file:

Vendor_Module\etc\extension_attributes.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
     <extension_attributes for="Magento\Checkout\Api\Data\ShippingInformationInterface">
          <attribute code="pickup_store" type="string"/>
          <attribute code="pickup_date" type="string"/>
     </extension_attributes>
</config>
  • Custom module that is overriding/using plugins and other customizations from source Vendor_Module module:

Vendor2_Module2\etc\extension_attributes.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
     <extension_attributes for="Magento\Checkout\Api\Data\ShippingInformationInterface">
          <!-- trimmed (other attributes) that will be added here -->
          <!-- Custom data for this overridden module -->
          <attribute code="custom_shipping_method_title" type="string"/>
     </extension_attributes>
</config>

Findings

Running bin/magento setup:upgrade && bin/magento setup:di:compile does not generate the extra attribute custom_shipping_method_title in the new overriding module's ShippingInformationExtensionInterface

namespace Magento\Checkout\Api\Data;

/**
 * ExtensionInterface class for @see \Magento\Checkout\Api\Data\ShippingInformationInterface
 */
interface ShippingInformationExtensionInterface extends \Magento\Framework\Api\ExtensionAttributesInterface
{
    /**
     * @return string|null
     */
    public function getPickupStore();

    /**
     * @param string $pickupStore
     * @return $this
     */
    public function setPickupStore($pickupStore);

    /**
     * @return string|null
     */
    public function getPickupDate();

    /**
     * @param string $pickupDate
     * @return $this
     */
    public function setPickupDate($pickupDate);
}
  • As indicated in generated/code/ file above, this only contains getPickupStore() and getPickupDate() (mapped via pickup_store and pickup_date respectively).

Question

  • It is missing the new custom attribute custom_shipping_method_title , eg. getCustomShippingMethodTitle() entry in the generated code.

How do I override / point Magento 2 to use Vendor2_Module2\etc\extension_attributes.xml instead of Vendor_Module\?

I know how to override / use plugins via di.xml for classes but since this is an XML file to be overridden, what do I do?

1 Answer 1

0

I think you may be misunderstanding how extension attributes work. You will not need to overwrite any extension attributes from the vendor module, rather, you can define your own extension attributes on the same entity within your own custom module. You can essentially copy the logic from the vendor module over to your custom module - changing the extension attribute code etc.

I recommend you have a thorough read-through of the following documentation, it provides quite a clear and comprehensive explanation of extension attributes and how to use them:

3
  • Perhaps the information I provided was not clear, but please consider this: Vendor_Module is a third party module that already contains Vendor_Module::etc\extension_attributes.xml; and I need additional fields in Vendor2_Module2\etc\extension_attributes.xml such that it 'appends/replaces' Vendor_Module attributes. Before asking the question, I thought it should be possible to override extension_attributes.xml in the way I attempted (this of course resulted in the missing field still). I did not realise I would need to still use a Plugin for this, feels slightly restrictive. Commented Feb 8, 2023 at 14:13
  • Thank you for the reference! Commented Feb 8, 2023 at 14:15
  • 1
    @CvRChameleon I'm afraid you cannot override a specific extension_attributes.xml file - Magento will essentially just merge all these files together during compilation. If you want to append the data, it's probably best to use a plugin on the method that sets this data. Commented Feb 8, 2023 at 15:27

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.