public function BulkVariationsCreator::createAllProductVariations in Commerce Bulk 8
Creates all possible variations for commerce_product.
Parameters
\Drupal\commerce_product\Entity\Product $product: A commerce product, whether new or having some variations saved on it.
array $variation_custom_values: (optional) An associative array of a variation property values which will be used to auto create all variations.
array $all: (optional) An associative array of variations combinations that will be to auto create all variations.
Return value
array|null An array of all commerce product variations that were missed before.
Overrides BulkVariationsCreatorInterface::createAllProductVariations
See also
\Drupal\commerce_product\Entity\Product->getVariations()
self->getAllAttributesCombinations()
File
- src/
BulkVariationsCreator.php, line 192
Class
- BulkVariationsCreator
- Default implementation of the BulkVariationsCreatorInterface.
Namespace
Drupal\commerce_bulkCode
public function createAllProductVariations(Product $product, array $variation_custom_values = [], array $all = []) {
$timestamp = time();
$shuffle_variations = !empty($all['shuffle_variations']);
$not_all['not_all'] = $max = !empty($all['max_nb_skus']) ? $all['max_nb_skus'] - 2 : TRUE;
unset($all['shuffle_variations'], $all['max_nb_skus']);
if (!$all) {
$variations = $product
->getVariations();
if (empty($variations) || !empty($variation_custom_values)) {
$variations[] = $this
->createProductVariation($product, $variation_custom_values, $not_all);
$timestamp--;
}
if (!($all = $this
->getAttributesCombinations($variations, $not_all))) {
return;
}
}
else {
$variations = $all['variations'];
$all = $all['all'];
}
// Improve perfomance by getting sku settings just once instead of
// calling static::getAutoSku() in the loop.
$settings = static::getSkuSettings($all['last_variation']);
extract($settings);
$prefix = isset($prefix) ? $prefix : '';
$suffix = isset($suffix) ? $suffix : '';
$more_entropy = isset($more_entropy) ? $more_entropy : FALSE;
$module_handler = \Drupal::moduleHandler();
$clone = clone $all['last_variation'];
$shuffle_variations && shuffle($all['not_used_combinations']);
$max = is_numeric($max) ? $max : count($all['not_used_combinations']);
foreach ($all['not_used_combinations'] as $combination) {
$variation = $all['last_variation']
->createDuplicate()
->setChangedTime($timestamp)
->setCreatedTime($timestamp);
$sku = \uniqid($prefix, $more_entropy) . $suffix;
$settings['combination'] = $combination;
$module_handler
->alter("bulk_creator_sku", $sku, $settings, $clone);
$variation
->setSku($sku);
foreach ($settings['combination'] as $field_name => $id) {
$variation
->get($field_name)
->setValue([
'target_id' => $id == '_none' ? NULL : $id,
]);
}
$variation
->updateOriginalValues();
$variations[] = $variation;
// To avoid the same CreatedTime on multiple variations decrease the
// $timestamp by one second instead of calling time() in the loop.
$timestamp--;
if (!$max--) {
break;
}
}
return $variations;
}