I need to unpublish all "active" (published status == TRUE) products and products variations before every import of actual assortment on my site. How to do it programmatically? I see 2 ways to do this:
- via a direct query to the database.
- via entytyQuery methodes: 2.1. create an array of ids of all products with the „status“ field == 1 (smth like that: $products_ids = Drupal::entityQuery('commerce_product')->condition('status', 1)->execute();) 2.2. iterate over the given array, changing all the „status“ fields to the value 0 and for each product getting its variations and assign them to the „status“ fields the value 0 (iterate over them in the subcycle).
thank you in advance for your help!
PS the quantity of products (with products variations) ~ 25 000 - 30 000 sku
My solution:
unpublish all products:
$pids = \Drupal::entityQuery('commerce_product')->condition('status', 1)->execute(); $products = Product::loadMultiple($pids); foreach ($products as $product) { $product->status = 0; $product->save(); }
unpublish all product variations:
$pvids = \Drupal::entityQuery('commerce_product_variation')->condition('status', 1)->execute(); $product_variations = ProductVariation::loadMultiple($pvids); foreach ($product_variations as $product_var) { $product_var->status = 0; $product_var->save(); }
Perhaps there is a more productive way to do this? I would be grateful for any hint.