You are here

public function BulkVariationsCreator::getProductVariation in Commerce Bulk 8

Gets a variation for commerce_product.

Parameters

\Drupal\commerce_product\Entity\Product $product: A commerce product, whether new or having some variations saved on it.

Return value

\Drupal\commerce_product\Entity\ProductVariation If exists, the last variation on a commerce_product, otherwise new one.

Overrides BulkVariationsCreatorInterface::getProductVariation

See also

\Drupal\commerce_product\Entity\ProductVariation->create()

self->createProductVariation()

1 call to BulkVariationsCreator::getProductVariation()
BulkVariationsCreator::createProductVariation in src/BulkVariationsCreator.php
Creates a variation for commerce_product.

File

src/BulkVariationsCreator.php, line 109

Class

BulkVariationsCreator
Default implementation of the BulkVariationsCreatorInterface.

Namespace

Drupal\commerce_bulk

Code

public function getProductVariation(Product $product) {
  $variations = $product
    ->getVariations();
  $variation = end($variations);
  $timestamp = time();
  if (!$variation instanceof ProductVariation) {
    $product_type = $this->entityTypeManager
      ->getStorage('commerce_product_type')
      ->load($product
      ->bundle());
    $variation = $this->entityTypeManager
      ->getStorage('commerce_product_variation')
      ->create([
      'type' => $product_type
        ->getVariationTypeId(),
      'product_id' => $product
        ->id(),
      'created' => $timestamp,
      'changed' => $timestamp,
    ]);
  }
  return $variation;
}