If multiple products are displayed in a views listing, the actions for all add to cart forms are the same (the current page, as no #action is set on the form).

Not to be confused with #2640486: Multiple cart forms are broken on /cart, which dealt with views forms having the same id, whereas this issue is about product (add to cart) forms having the same #action property (the current view page).

CommentFileSizeAuthor
#9 2831895-9.diff1.09 KBsteveoliver

Comments

steveoliver created an issue. See original summary.

steveoliver’s picture

See https://github.com/drupalcommerce/commerce/pull/578 - currently submits add to cart forms to purchasable entities' pages. Not ideal as it directs you to another page and cannot continue adding products to cart from the view. :(

steveoliver’s picture

steveoliver’s picture

Title: Allow multiple products (add to cart forms) to be on the same page (View) » Allow multiple products (add to cart forms) to be on the same page
Issue tags: -views

Weird -- with the currently committed code (not this patch - which doesn't actually help anything), what happens is if you have a list of 10 products, and you click to add the second one to your cart, the second from last is added to the cart. If you click the third from the top, the third from the bottom is actually added. WTF?

This is not an issue specific to views, so removing tag and views from title.

mglaman’s picture

Does this have to do with the static counter added in #2827721: AddToCartForm should support all purchasable entities instead of when it was based on the product ID? Not quite sure, but would be worth checking out.

drugan’s picture

@steveoliver
Try applying #575 and see if that will work for you.

mglaman’s picture

drugan the issue is the form ID. Using a static counter was not working because it didn't increment properly.

This ensures a unique add to cart form ID.

-    $form_id .= '_' . self::$formInstanceId;
 +    $form_id .= '_' . $this->entity->getPurchasedEntity()->id();
drugan’s picture

@mglaman

It didn't increment properly because it should be changed like this:

    if ($this->operation != 'default') {
      $form_id = $form_id . '_' . $this->operation;
    }
-    $form_id .= '_' . self::$formInstanceId;
+    $form_id .= '_' . static::$formInstanceId;

But anyway, if we'll get right incrementing the bug indicating by steveoliver #4 is still there. As I think the issue not only on the add to cart form ID, but also with not properly detected $selected_variation.

See ProductVariationAttributesWidget.php

    if (!empty($user_input)) {
      $selected_variation = $this->selectVariationFromUserInput($variations, $user_input);
    }

For me #575 has resolved the steveoliver's issue. Still the ajax wrapper is not working properly because when you choose an attribute option, let's say on the seventh add to cart form in the view then you thrown up to the first add to cart form in the view instead of staying at the seventh form.

steveoliver’s picture

Title: Allow multiple products (add to cart forms) to be on the same page » AddToCartForm should support multiple instances
Status: Active » Needs review
Parent issue: » #2827721: AddToCartForm should support all purchasable entities
StatusFileSize
new1.09 KB

I'm marking this issue as a child of the static counter issue as a follow-up fix to that -- with this issue, we just use the purchasable entity id as a unique identifier for the form id. The Forms API/HTML::getId will increment the form id if the same product add to cart form ends up more than once on a page, and will keep the same functionality/approach, but with support for all purchasable entity types, not just products.

The https://github.com/drupalcommerce/commerce/pull/578 PR (diff also attached) fixes this issue.

@drugan, You may be experiencing a different issue which I noticed while testing this issue -- that is: if you have the same purchasable entity add to cart form twice on a page, the current ajax attribute selection/changing does not work correctly -- I will open a separate issue for the #ajax class targeting / field re-rendering issue.

steveoliver’s picture

bojanz’s picture

Status: Needs review » Closed (duplicate)

Let's go back to #2827721: AddToCartForm should support all purchasable entities since that's where the regression originated.