Score:1

How to automate the creation of product types?

kw flag

I'm using Drupal 9 with Commerce 2. I have a large number of product types that need to be created. I have for each product type which fields need to be added. Let's say this information is stored in a json (I can vary this if the solution requires it). I was wondering if there is a way to create a module or something that iterates through this json file, and it creates all the product types. If there is, how would I go about implementing this?

I found some code examples with which I can create products programmatically here. I'm just not entirely sure how I would use this code inside a custom module.

I appreciate the help in advance!

id flag
We do something quite like this, including not only adding but also updating the items if the data changes. I think the answer here would depend on whether you intend to execute this manually, say, by uploading a file, or whether you want to automate to work on an ongoing basis.
levente.nas avatar
kw flag
@cilefen I appreciate the comment! I would rather execute it manually (upload the file).
id flag
It looks like some modules exist which do that, like https://www.drupal.org/project/entity_import and https://www.drupal.org/project/entities_import. They may work as-is for you, or, you could modify or extend them.
levente.nas avatar
kw flag
Perfect. Thanks for the info.
Score:1
it flag

There is no way, just custom code all the way down. That said, it may be to your advantage not to have a proliferation of product types. They do create overhead on your site. Might be better simply to have optional fields on fewer, especially if their creation will be automated? Could always have a sub-type field on the product type itself that controls which fields are shown on the edit form via form states and even make various fields conditionally required.

levente.nas avatar
kw flag
I see... Would, for example, 200 different product types be too many? I like your suggestion though.
it flag
I wouldn't architect a site that way, but you can always run testing on it. Just pay attention to rendering individual products and product lists.
levente.nas avatar
kw flag
Ok, thanks for the suggestions.
Score:1
fr flag

I havent done this exactly with Commerce Entities, but I have done with custom entities using Migrate API.

What you can do is provide json or csv migrate source to migration and map those fields accordingly in my_products_migration.yml file , you can use contribute modules like, migrate_plus, migrate_source_csv you can even refer migrate_json_example module from migrate_plus which has a reference example for products migration.

You can refer commerce migrate documentation for additional details

levente.nas avatar
kw flag
Awesome. Thanks for the information. I'll see what I can do with it.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.