According to the PHP documentation, ProductVariationStorage::loadFromContext always returns a ProductVariationInterface, it returns the variation requested in the URL if possible and falls back to the product default variation if none is found or accessible.

According to the PHP documentation Product::getDefaultVariation may return NULL, so it would be logical to update ProductVariationStorage::loadFromContext to also allow a NULL return and adapt CommerceProductThemeHooks::preprocessCommerceProductVariation accordingly.

Issue fork commerce-3579667

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

aerzas created an issue. See original summary.

aerzas’s picture

Status: Active » Needs review
jsacksick’s picture

Status: Needs review » Needs work

The updated comment is incorrect, loadFromContext() doesn't necessarily load the default variation.

The default variation is the fallback, but if there is a "?v" query parameter present, it'll return the corresponding variation.

  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->access('view')) {
          return $variation;
        }
      }
    }
    return $product->getDefaultVariation();
  }

Also I think:

    $variables['is_active'] = isset($active_variation) && (int) $active_variation->id() === (int) $product_variation->id();

can be changed to:

    $variables['is_active'] = (int) $active_variation?->id() === (int) $product_variation->id();

tbkot made their first commit to this issue’s fork.

tbkot’s picture

The MR is updated.

jsacksick’s picture

Version: 3.3.3 » 3.x-dev
Status: Needs work » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.