You are here

public function ProductVariationStorage::loadFromContext in Commerce Core 8.2

Loads the product variation from context.

Uses the variation specified in the URL (?v=) if it's active and belongs to the current product.

Note: The returned variation is not guaranteed to be enabled, the caller needs to check it against the list from loadEnabled().

Parameters

\Drupal\commerce_product\Entity\ProductInterface $product: The current product.

Return value

\Drupal\commerce_product\Entity\ProductVariationInterface The product variation.

Overrides ProductVariationStorageInterface::loadFromContext

File

modules/product/src/ProductVariationStorage.php, line 94

Class

ProductVariationStorage
Defines the product variation storage.

Namespace

Drupal\commerce_product

Code

public function loadFromContext(ProductInterface $product) {
  $current_request = $this->requestStack
    ->getCurrentRequest();
  if ($variation_id = $current_request->query
    ->get('v')) {
    if (in_array($variation_id, $product
      ->getVariationIds())) {

      /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */
      $variation = $this
        ->load($variation_id);
      if ($variation
        ->isPublished() && $variation
        ->access('view')) {
        return $variation;
      }
    }
  }
  return $product
    ->getDefaultVariation();
}