0

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:

  1. via a direct query to the database.
  2. 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

Savage
  • 11
  • 1

2 Answers2

0

My solution:

  1. 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(); }

  2. 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.

Savage
  • 11
  • 1
0

the same solution, only through a direct query to the database:

 $query = \Drupal::database()->update('commerce_product_field_data');
  $query->fields([
    'status' => 0
  ]);
  $query->condition('status', 1);
  $query->execute();
Savage
  • 11
  • 1