Product variant pages generated using ProductVariation->toUrl() don't have the right cache contexts. Having dynamic_page_cache enabled, always the same product variant is being displayed.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Lukas von Blarer created an issue. See original summary.

Lukas von Blarer’s picture

Issue summary: View changes
bradjones1’s picture

Status: Active » Needs review
FileSize
838 bytes

So in my testing the issue seems to be that the render array's cache metadata does not include the 'v' query string key, and so the fields relating to the current product variation from the context are cached regardless of that value. This patch adds the cache context to the build array.

Not sure if test coverage in Commerce exists/needs to change to cover this?

mglaman’s picture

That is the correct approach. I thought there was an open issue for this already. Good patch. We can write tests for it ensure cache busting.

bojanz’s picture

Status: Needs review » Needs work

Let's add a test and wrap this up.

mglaman’s picture

+++ b/modules/product/src/ProductViewBuilder.php
@@ -61,6 +61,7 @@ class ProductViewBuilder extends EntityViewBuilder {
     $product_type = $product_type_storage->load($entity->bundle());
     if ($product_type->shouldInjectVariationFields() && $entity->getDefaultVariation()) {
+      $build['#cache']['contexts'][]  = 'url.query_args:v';

Actually, I'm pretty sure we can define this on the product class itself.

mglaman’s picture

This doesn't add tests, but it puts the cache context on the product entity so it is always available and bubbles up.

  • bojanz committed 0e5a3fb on 8.x-2.x authored by mglaman
    Issue #2884083 by mglaman, bradjones1: Product variant pages generated...
bojanz’s picture

Title: Product variant pages generated using ProductVariation->toUrl() don't have the right cache contexts » Expand tests for the Product url.query_args.v cache context
Status: Needs work » Active

Committed #7 since it's release day, renaming the issue so that it receives the test.

bojanz’s picture

Title: Expand tests for the Product url.query_args.v cache context » Product variant pages generated using ProductVariation->toUrl() don't have the right cache contexts
Status: Active » Fixed

Reverting the title, having a test for this one-liner no longer feels like good use of our time.

Status: Fixed » Closed (fixed)

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