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
Having extension_attributes.xml + custom repository (and database table) + Plugin (afterGet and afterSave)
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
1 Answer 1
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.
-
> 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.dudzio– dudzio2022年11月07日 20:23:29 +00:00Commented Nov 7, 2022 at 20:23
Explore related questions
See similar questions with these tags.