You are here

public function BulkVariationsCreator::duplicateAllProductVariations in Commerce Bulk 8

File

src/BulkVariationsCreator.php, line 165

Class

BulkVariationsCreator
Default implementation of the BulkVariationsCreatorInterface.

Namespace

Drupal\commerce_bulk

Code

public function duplicateAllProductVariations(Product $product) {
  if (!($variations = $product
    ->getVariations())) {
    return $variations;
  }
  if (!($product_id = $product
    ->id())) {
    $product
      ->save();
    $product_id = $product
      ->id();
  }
  $settings = static::getSkuSettings(end($variations));
  extract($settings);
  $prefix = isset($prefix) ? $prefix : '';
  $suffix = isset($suffix) ? $suffix : '';
  $more_entropy = isset($more_entropy) ? $more_entropy : FALSE;
  $duplicates = [];
  foreach ($variations as $variation) {
    $duplicate = $variation
      ->createDuplicate();
    $duplicate
      ->setSku(\uniqid($prefix, $more_entropy) . $suffix);
    $duplicate
      ->set('product_id', $product_id);
    $duplicates[] = $duplicate;
  }
  return $duplicates;
}