My example may be a bit convoluted, however it does strike me as a bug so I thought I'd report it.

I have a display node for a product, and then blocks that also show on the same page provided by Views / Context.

One of these blocks holds the add to cart form for the product which the display node references.

If I try and add to cart using the form in the View, no matter what Quantity I specify, only 1 is added to the cart.

If I remove the Add To Cart form from the display node (hide it in the content type) however, then the Views form works as expected.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kitkat13’s picture

Title: Add To Cart form provided by a View only adds 1 quantity if displayed on the same page as the display node » A second Add To Cart form provided by a View only adds 1 quantity if displayed on the same page with form without imput quantity
Version: 7.x-1.2 » 7.x-1.x-dev
FileSize
4.46 KB

Hello,
i have the same bug.

To reproduce it , just install (and dependencies):
- Drupal v. 7.14
- commerce v. 7.x-1.x-dev
- views v. 7.x-3.3
- eva v. 7.x-1.x-dev

Now:
- add a new product (product_commerce type)
- create a product-display (name : 'product' if you want to use my view) with a field 'product_ref'
- set manage display for field 'product_ref'
** in default => Format 'add to cart'
settings:
Quantity widget: Disabled
Default quantity: 1
Combine like items: Enabled
** in teaser => teaser => Format 'add to cart'
settings:
Quantity widget: Enabled
Default quantity: 1
Combine like items: Enabled

- add a new content : product with a ref on the product create before
- import the view in views.txt
- go to admin/structure/block and set to 'View: add to cart bis' in region 'content' BUT UNDER 'Main page content'

Now, go to the display node or /test.
Try to add 10 product into your cart... only 1 is added to the cart.

vasike’s picture

@kitkat13 : i follow your instructions using the view you provided.
i can't reproduce the issue. all it works as should be. it adds the right number of products to the cart.

i used the last dev http://ftp.drupal.org/files/projects/commerce-7.x-1.x-dev.tar.gz

vasike’s picture

Status: Active » Postponed (maintainer needs more info)

i did a second test and indeed i could reproduce the issue.

first test: i test for a product display with several product references (different attributes), and all it works as it should be

second test: for a product display with a single product reference and i got this issue, only one product added to the cart, no matter of quantity.

third test: deleted the fields for attributes, and made test for both product display
result: it works only for multiple product references. for product displays with single product referenced it seems we have this issue.

vasike’s picture

Status: Postponed (maintainer needs more info) » Active

i think i found the cause of the issue : duplicate of the form_id in the case of duplicates of the products referenced.
'form_id' => commerce_cart_add_to_cart_form_id($product_ids),
this value should be somehow unique.

vasike’s picture

Status: Active » Needs review
FileSize
1.03 KB

here is a patch for this, adds an incremental number to the form_id.

i also noticed there some issues, having duplicate of product displays in a page with multiple products referenced.
the change of the product (or attributes) selection goes to to change for all product displays.
but i think this is another issue. i'll take a look if is not already open.

checker’s picture

Thanks yor this patch vasike. I'm in the situation "having duplicate of product displays in a page with multiple products referenced" and therefore your patch does not work. The form ids are unique but I still cannot add to cart.

rszrama’s picture

Title: A second Add To Cart form provided by a View only adds 1 quantity if displayed on the same page with form without imput quantity » Use a separate form ID pattern for Views field based Add to Cart forms
Component: Cart » Views integration
Category: bug » feature
Status: Needs review » Active

Honestly, I'm a bit hesitant to commit the patch in #5. I think that function is the wrong place to work around a limitation of Drupal's forms API. I'm not sure we should even be responsible for ensuring uniqueness of form IDs if multiple instances of the same form are placed on the page. As a concession, we could use an alternate form ID pattern for Views field based Add to Cart forms, but we would need to make it optional to preserve backwards compatibility for folks who may already be altering those forms.

Generally, though, I'd also recommend not having multiple instances of the same Add to Cart form on the page, but if you must it appears you just won't be able to have one use the quantity textfield and the other not (I don't see how this makes sense anyways) until this feature lands.

rszrama’s picture

Want to add to this that in the course of debugging #1925646: Combine does not work with different line-item types and custom comparison properties I discovered another scenario where you can end up with duplicate form IDs: using two Add to Cart forms on the same page that add the product using different line item types.

ellastelter’s picture

Issue summary: View changes

Has this been solved? I believe I am having a similar issue.

While I only see one instance of "add to cart" on my page, I am using panelizer. No matter what quantity is listed in the cart it only adds one.

Would this patch work for me, or does this sound like a different issue?

Pierre.G’s picture

Hi,

I'm in the exact situation described by rszrama on #8.
I have a few add to cart forms on the same page, which have the same products attached to them.

The id generated through commerce_cart_add_to_cart_form_id() is the same for every form on the page.
When a user select another product variation in the first add to cart form, it also updates the next forms (wrong behaviour). When an other form is changed, it doesn't trigger any ajax (wrong too).

I tried the patch, it's almost working with it!
But the id and the class are different inside a form :
<form class="generated_id-2" id="generated_id-1">
<form class="generated_id-4" id="generated_id-3">
<form class="generated_id-6" id="generated_id-5">
I believe that the id get generated first and then the function is called again to generate the class.

So maybe that using static isn't the best option here.

What about using another thing than the list of products to generate the id ? Maybe we can add the product display id to reduce the scenarios ?

AaronBauman’s picture

After applying this patch, the ajax for product dependencies on my commerce forms doesn't work anymore.