1

I want to add custom attribute for Order

From what I have learned, there are two ways this attribtue can be associated to Order entity

  1. Having extension_attributes.xml + custom repository (and database table) + Plugin (afterGet and afterSave)

  2. Creating Setup Patch script as shown in \Magento\GiftMessage\Setup\Patch\Data\AddGiftMessageAttributes = creating EAV attribute for order entity type

My question is:

  • What is better?
  • When should I choose option 1 and when it is better to choose option 2?

Thank you

asked Aug 2, 2021 at 8:26

1 Answer 1

0

I think you are mixing two things, there is on the one side the attribute creation,saying to magento : Hey my orders have this attribute. And on the other side there is the handling of the data behavior, saving it for example.

Doing a plugin, or a preference on a save and get method will allow you to handle the data.

Doing a patch or an installer or an upgrader will allow you to create that attribute.

Custom repository and custom table : No, this makes no sense, you never override the sales_order table (or any other base table from magento). You always create attribute that are then linked to these tables.

The xml file can be a way to create that attribute indeed, but I would say it's more something you will use on the installation of a module. If your module is already installed, I would recommend using a patch / upgrader. This allow you to keep track of when everything is done regardless of your module version.

On my side, I never use the xml file, even for installation as we can still use InstallData and InstallSchema, the same way we use UpgradeData and UpgradeSchema.

If there is an advantage to use the xml I don't have it.

answered Aug 2, 2021 at 8:55
1
  • > Custom repository and custom table : No, this makes no sense, you never override the sales_order table (or any other base table from magento). You always create attribute that are then linked to these tables. That is not true, if we consider creating static attributes, which in my opinion are desired, when creating some ext_id attributes, which then will be used for loading entity. Commented Nov 7, 2022 at 20:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.