Given I'm on the cart product page
And I have a product in my cart with quantity 1
When I change the value of the quantity input field to 2
And press return (enter)
Then I expect the quantity for the line item to be updated

Instead the line item is deleted.
This is caused by the fact that the return submits the line item delete form instead of the cart update form.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Kingdutch created an issue. See original summary.

bojanz’s picture

Ouch, agreed.

vasike’s picture

Status: Active » Needs review
FileSize
2.07 KB

here is patch for this.
Used the D7 approach - extra JS for edit quantity Views handler - to make sure the Update cart is submitted on press enter key.

sorabh.v6’s picture

Assigned: Unassigned » sorabh.v6
sorabh.v6’s picture

Assigned: sorabh.v6 » Unassigned
Status: Needs review » Reviewed & tested by the community

Hi All,

I confirm that the patch in #3 is working fine for me code is also looking fine. Setting it to RTBC.

Thanks All

MegaChriz’s picture

Would this still work when #2906345: Replace the cart page view with a Twig template lands?

I see a reference to a views class here:

+++ b/modules/cart/js/commerce_cart_edit_quantity.js
@@ -0,0 +1,25 @@
+      $('form .views-field-edit-quantity input.form-number', context).keydown(function (event) {
edurenye’s picture

RTBC +1

littlepixiez’s picture

#3 works perfectly for me. It's the first thing I did when playing with the cart! Great patch.

@MegaChriz It works using 'form input[id^="edit-edit-quantity"]' but I'm guessing the ID might change depending on what generates the form?

spacetaxi’s picture

#3 works for me with a minor change: Add a colon (:) in front of input in the selector. See https://api.jquery.com/input-selector/

$('input#edit-submit', $(this).parents('form')).click();

to

$(':input#edit-submit', $(this).parents('form')).click();
abramm’s picture

Status: Reviewed & tested by the community » Needs work

The JS code is missing context handling and once().
Running Drupal.attachBehaviors() multiple time will cause the event handler to run multiple times.

abramm’s picture

Ahh the context handling is there. That's just once() missing.

Here's the patch against latest dev.
Added once() and changed input to :input as per #9.

Interdiff included.

abramm’s picture

Status: Needs work » Needs review
Berdir’s picture

Status: Needs review » Reviewed & tested by the community

Don't know too much about JS, but this was RTBC before, looks fine to me and I tested it.

Interestingly, after noticing this problem. I quickly tried to reproduce on https://commerceplus.acromedia.com/cart and it doesn't happen there, so I first thought its a problem with our theme, but I guess this patch is already applied there...

bojanz’s picture

Status: Reviewed & tested by the community » Needs work

Thanks everyone!

+edit_quantity:
+  version: VERSION
+  js:
+    js/commerce_cart_edit_quantity.js: {}
+  dependencies:
+    - core/jquery
+    - core/drupal

Let's call this cart_form. That parallels cart_block above it, and allows us to keep the file even for a Twig version of the cart form, if it ends up existing. The JS file should then be commerce_cart.form.js (not a fan of prefixing every JS/CSS file with the module name, but that's what we've done so far).

+        .keydown(function (event) {
+        if (event.keyCode === 13) {
+          // Prevent browser's default submit from being clicked.
+          event.preventDefault();
+          $(':input#edit-submit', $(this).parents('form')).click();
+        }
+      });

Indentation is wrong here.

Wanted to run a full ESLint, but it's ignoring Drupal's coding standard somehow, telling me to replace single quotes with double quotes, which makes no sense.

  • bojanz committed bc5c177 on 8.x-2.x authored by vasike
    Issue #2878155 by abramm, vasike, bojanz, Berdir: When I press return to...
bojanz’s picture

Status: Needs work » Fixed

Thanks, everyone!

mmjvb’s picture

Status: Fixed » Closed (fixed)

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

eiriksm’s picture

FileSize
1.2 KB

Not sure if we should call this an edge case, but the selector in the jQuery code will only work if you actually use the view provided by commerce core with little customization. This means one has to duplicate this code to be able to fix the same issue when the selectors are different.

In my opinion we should add a class to the actual input instead, and use this as the selector. This way, the code would work regardless of how you customize the cart form. Well except if you actively remove this class, but that seems way less likely than someone doing some customization that ends up changing wrapper selectors.

Here is a patch with that approach. If you want a followup instead, please let me know :)

eiriksm’s picture