You are here

function commerce_product_post_update_6 in Commerce Core 8.2

Grants the "manage variations" permission to roles that can update products.

File

modules/product/commerce_product.post_update.php, line 99
Post update functions for Product.

Code

function commerce_product_post_update_6() {
  $entity_type_manager = \Drupal::entityTypeManager();

  /** @var \Drupal\commerce_product\Entity\ProductTypeInterface[] $product_types */
  $product_types = $entity_type_manager
    ->getStorage('commerce_product_type')
    ->loadMultiple();

  /** @var \Drupal\user\RoleInterface[] $roles */
  $roles = $entity_type_manager
    ->getStorage('user_role')
    ->loadMultiple();
  foreach ($roles as $role) {
    foreach ($product_types as $product_type) {

      // If the role had any update permission, grant the manage permission.
      if ($role
        ->hasPermission("update any {$product_type->id()} commerce_product") || $role
        ->hasPermission("update own {$product_type->id()} commerce_product")) {
        $role
          ->grantPermission("manage {$product_type->getVariationTypeId()} commerce_product_variation");
      }
    }
    $role
      ->save();
  }
}