I installed drupal commerce with composer, everything works fine. I created product- and variation-type. I created a product with several variations, each has it's own price and sku. On the single product page I can choose one of the variations from select box and price and title are updated (with ajax?).
Than i created a block-view for this product-type in views, showing the rendered entity of the product, which should be the same as on single product page, because I use the same display mode (standard). Well, it looks the same, but the variation title and price are not updated, when I change it.
Don't know, if it's a bug or if I make a mistake.

Comments

krabbe created an issue. See original summary.

mglaman’s picture

Component: Product pricing » Product

It sounds like some cache tags are not properly being invalidated when the variation is changed. The bug might be that it's not clearing the Views cache? Either way some cache tag isn't being invalidated somewhere.

krabbe’s picture

Thank you. How can I find out, where the cache problem is located?

mglaman’s picture

So, unfortunately it can be difficult. I'm not even sure how I'd debug it right now without going all out with xdebug.

The first thing is to check the ProducrVariation entity class. We should be adding the Product it belongs to cache tags to its invalidate cache tags call. Maybe even experiment with adding more stuff there, no idea.

It's either Views render cache, which should invalidate with entity save. Or the variation isn't actually invalidating the Products view cache.

So if you're new to D8s render cache and cache tag/context system, this is one hell of an intro/dive in issue.

I say don't be afraid and try it out :) you'll want to have it in your back pocket.

Next steps though is writing a test to verify this bug.

krabbe’s picture

Mmmmh... I'm not a full time drupalist, and while exploring D8 I have to learn so many new things. So I will put this one beside.
And yes, learning the drupal test system is one of the things.
But maybe, this issue will just "fly away" with one of next updates... ;)
Thanks a lot.

krabbe’s picture

It seems to be a bigger problem. On the page with the block of products listed with views not only the price for the variations don't update, but also only the first product is added to the cart, no matter which add to cart button I click.

There are three products in that block with two different product types, one with variations, the other one without.
The view is a product view, format unordered list and product as rendered entity.

mglaman’s picture

Okay, I guess it's time to create a test view and really get a test going for this. I assume that dynamic page cache and page cache are enabled. Is this for authenticated and anonymous users?

krabbe’s picture

Cache modules are enabled, yes. And there is no difference in behavior between authenticated and anonymous users.

agoradesign’s picture

The add to cart problem sounds familiar. I had a similar problem when I implemented and add to cart button directly for a variation view instead of the default one for product entities. When I used that to show 4 variations in a list,I had the same problem and I solved it

Unfortunately I don't have access to that repo atm before Sunday... I'm not sure but AFAIK it had to do with the form ID or similar

mglaman’s picture

krabbe, is that view generic enough you could paste the config YML here? Or possible send if in a PM? We need to make a test using a sample View, but need to make sure I built one properly.

drugan’s picture

StatusFileSize
new4.54 KB

I confirm the issue. Only the first product in a block-view works as expected. The others don't change their values (Title, SKU, Price) being chosen. Well, actually they change them but only on the the very first product, not on itself.

See the exported YML of the view.

mglaman’s picture

drugan, thanks!

krabbe’s picture

StatusFileSize
new6.56 KB

This is the block-view-YML-file I use...

agoradesign’s picture

CommerceAddToCartForm must provide an unique form ID. That's why always the same product is added to cart.

My quick and not too sexy workaround was to use a static counter variable and add this to the form ID

mglaman’s picture

Assigned: Unassigned » mglaman

In 1.x we made it based on the product's ID (well, variations actually.) I'll write a test which imports this view and does all those things. Sitting down with it now.

mglaman’s picture

Status: Active » Needs review
StatusFileSize
new548 bytes

This looks surprisingly simple.

Based on

    $form_id = $this->entity->getEntityTypeId();
    if ($this->entity->getEntityType()->hasKey('bundle')) {
      $form_id .= '_' . $this->entity->bundle();
    }
    if ($this->operation != 'default') {
      $form_id = $form_id . '_' . $this->operation;
    }
    return $form_id . '_form';

We just need a unique operation.

diff --git a/modules/product/src/ProductLazyBuilders.php b/modules/product/src/ProductLazyBuilders.php
index c14dd01..3cb1171 100644
--- a/modules/product/src/ProductLazyBuilders.php
+++ b/modules/product/src/ProductLazyBuilders.php
@@ -73,7 +73,7 @@ class ProductLazyBuilders {
         'combine' => $combine,
       ],
     ];
-    return $this->entityFormBuilder->getForm($line_item, 'add_to_cart', $form_state_additions);
+    return $this->entityFormBuilder->getForm($line_item, 'add_to_cart_' . $product_id, $form_state_additions);
   }

Can you try the patch? We still need tests

mglaman’s picture

Status: Needs review » Needs work
mglaman’s picture

Status: Needs work » Needs review
StatusFileSize
new9.07 KB

Here we go! Fix with test. PR here: https://github.com/drupalcommerce/commerce/pull/487

Test is not a FunctionalJavascript test for attribute changing. Merely that with 5 add to cart forms, clicking submit on third adds proper product. We can extend to test FunctionalJavascript if needed.

krabbe’s picture

Thank you!
The last patch fixed my problem. Price and title of product with variations are updating now and the correct product is added to cart.

drugan’s picture

For me also #18 works. Thank you!

niko-’s picture

Thanks,

works for me.
also #18 fix https://www.drupal.org/node/2786245

agoradesign’s picture

Shouldn't we include the operation in getBaseFormId() as well?

If you want to target all add to cart forms in a hook_form_BASE_FORM_ID_alter(), this would be much more convenient...

mglaman’s picture

StatusFileSize
new8.95 KB

Updated base form ID: commerce_line_item_add_to_cart_form. Then a form ID would be like commerce_line_item_product_variation_add_to_cart_PRODUCTID_form.

Tests still stupid on Travis.

mglaman’s picture

Attributing this work to ThinkBean.

mglaman’s picture

Updated PR to change view name and route to match "test multiple carts". Should be merged if tests still pass with change.

  • mglaman committed c80cc04 on 8.x-2.x
    Issue #2796669 by mglaman, krabbe, drugan: Price not updated for...
mglaman’s picture

Status: Needs review » Fixed

Thanks everyone for confirming! Merged.

Status: Fixed » Closed (fixed)

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