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!

  • 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.
    – mona lisa
    Commented Oct 19, 2022 at 18:26
  • @cilefen I appreciate the comment! I would rather execute it manually (upload the file). Commented Oct 20, 2022 at 19:16
  • It looks like some modules exist which do that, like drupal.org/project/entity_import and drupal.org/project/entities_import. They may work as-is for you, or, you could modify or extend them.
    – mona lisa
    Commented Oct 20, 2022 at 19:48
  • Perfect. Thanks for the info. Commented Oct 21, 2022 at 15:37

2 Answers 2


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.

  • I see... Would, for example, 200 different product types be too many? I like your suggestion though. Commented Oct 20, 2022 at 19:23
  • 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. Commented Oct 21, 2022 at 16:44
  • Ok, thanks for the suggestions. Commented Oct 21, 2022 at 20:02

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

  • Awesome. Thanks for the information. I'll see what I can do with it. Commented Oct 20, 2022 at 19:23

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.