You are here

public function BulkVariationsCreator::getUsedAttributesCombinations in Commerce Bulk 8

Gets used combinations on a product.

Parameters

array $variations: The commerce product variations.

Return value

array|null last variation, variation attributes ids and options and already used used attributes combinations, if they are found:

  • "last_variation": The variation on the last inline entity form array.
  • "attributes": An array with attributes ids and options:
    • "ids": The array of field_name => id pairs.
    • "options": The array of id => field_label pairs.
  • "used_combinations": The already used attributes combinations.

Overrides BulkVariationsCreatorInterface::getUsedAttributesCombinations

1 call to BulkVariationsCreator::getUsedAttributesCombinations()
BulkVariationsCreator::getAttributesCombinations in src/BulkVariationsCreator.php
Gets all ids combinations of the commerce_product's attribute fields.

File

src/BulkVariationsCreator.php, line 323

Class

BulkVariationsCreator
Default implementation of the BulkVariationsCreatorInterface.

Namespace

Drupal\commerce_bulk

Code

public function getUsedAttributesCombinations(array $variations) {
  $all = [];
  $all['duplicated'] = $all['used_combinations'] = [];
  $all['last_variation'] = end($variations);
  $all['attributes'] = $this
    ->getAttributeFieldOptionIds(end($variations));
  $nones = array_fill_keys(array_keys($all['attributes']['ids']), '_none');
  foreach ($variations as $index => $variation) {

    // ProductVariation->getAttributeValueIds() does not return empty optional
    // fields. Merge 'field_name' => '_none' as a choice in the combination.
    // @todo Render '_none' option on an Add to Cart form.
    // @see ProductVariationAttributesWidget->formElement()
    // @see CommerceProductRenderedAttribute::processRadios()
    $combination = array_merge($nones, $variation
      ->getAttributeValueIds());
    if (in_array($combination, $all['used_combinations'])) {
      $all['duplicated'][$index] = $combination;
    }
    else {
      $all['used_combinations'][$index] = $combination;
    }
  }
  $all['used'] = count($all['used_combinations']);
  $all['count'] = $all['attributes']['count'];
  return $all;
}