1

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!

4
  • 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.
    – cilefen
    Oct 19, 2022 at 18:26
  • @cilefen I appreciate the comment! I would rather execute it manually (upload the file). 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.
    – cilefen
    Oct 20, 2022 at 19:48
  • Perfect. Thanks for the info. Oct 21, 2022 at 15:37

2 Answers 2

1

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.

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.