1

I have a drupal 8 commerce website with 2 stores (store1 and store2). Most of the products belong to both stores but there are a few that only belong to either one.

Now, I need all the products (hundreds) to belong to a new store(store3) that i created on the website.

Obviously, I will not manually edit each product to assign it to the new store but what i was looking into is to write a SQL script (given that it's a one time thing) that will update the products in the DB. Soon after that i found out that it's more than the commerce_product__stores table that i would need to update, i would also have to update the commerce_promotion__stores and commerce_shipping_method__stores tables. and to make the things more complicated, some tables like the commerce_product__stores need its values updated where as with the other ones, i need to add the entries.

What better, Drupal way, ways of doing it are out there? Another option that i found was talking about exporting all the products with the views data export module, modify the data and import it back in with the Feeds module but the author was complaining about some issues that were still not resolved in D8...

Any suggestions?

4

There are two methods that are the "Drupal" way.

One is to write a PHP script that changes the products as needed. You would write a loop, load the products, update, and then save them. You then can the execute this script with drush as drush scr whatever.foo. This will bootstrap Drupal, and then execute the script in the context of a running Drupal install. Just keep in mind that loading a lot of entities in the same process can cause problems with the static cache getting too big. There are other questions here about how to handle this.

The second is to write a hook_update_N() function, and put it in a custom module. This is essentially the same as the standalone script. If you go with this approach, you can batch the changes (say update 50 at a time). You can then run the update from the UI or with drush updb. This approach works a little better with database backups / restores, and also works nicely if you have a dev -> staging -> production environment, especially if it also pulls the database backwards at each step and automatically runs updates.

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