craftcms / commerce

Fully integrated ecommerce for Craft CMS.
https://craftcms.com/commerce
Other
226 stars 170 forks source link

[4.x]: The update cart action (actionUpdateCart()) is not updating unchecked checkboxes #3228

Closed gigo6000 closed 1 year ago

gigo6000 commented 1 year ago

What happened?

Description

We have some custom fields of type Lightswitch and show them in the booking template as checkboxes:

    <input type="checkbox" value="1" name="fields[booleanField]" {% if editMode and cart.booleanField %} checked{% endif %}>
Screen Shot 2023-07-22 at 9 45 55 AM

If any of those checkboxes is changed to checked and saved once then the next time you save the form and you uncheck them the cart/order values will still be true/1. The unchecked checkboxes are not sent in the request which is standard in html forms, but the actionUpdateCart() is not updating this fields.

Steps to reproduce

  1. Create a custom field of type Lightswitch
  2. Add field to the template as an input of type checkbox
  3. Save the form first with the checkbox checked
  4. Remove the item from the cart and go back to the same form
  5. Uncheck the checkbox and save

Expected behavior

The value for the field should be false/0 (cart.booleanField == 0) after you uncheck the checkbox and save the form.

Actual behavior

The value of the field is true/1 (cart.booleanField == 1) even after saving the form with the checkbox unchecked.

Craft CMS version

4.4.15

Craft Commerce version

4.2.11

PHP version

8.1.16

Operating system and version

Debian

Database type and version

MariaDB 10.4.28

Image driver and version

No response

Installed plugins and versions

"craftcms/commerce": "4.2.11",
"craftcms/commerce-omnipay": "^4.0.0.1",
"craftcms/commerce-paypal-checkout": "2.1.2",
"craftcms/postmark": "3.0.0",
"craftcms/redactor": "3.0.4",
"nystudio107/craft-retour": "4.1.12",
"nystudio107/craft-seomatic": "4.0.28",
"omnipay/common": "~3.0",
"solspace/craft-freeform": "4.1.3",
"spicyweb/craft-neo": "3.8.0",
lukeholder commented 1 year ago

This is expected behaviour with HTML checkboxes. If they aren't clicked then the data is not submitted to the server. This is not a Craft CMS/Commerce behaviour but the normal HTML checkbox behaviour.

You would need to have a hidden input with the same name before the normal checkbox submitting a false value if you want a false submitted.

{{ hiddenInput('fields[booleanField]', 0) }}
{{ input('checkbox', 'fields[booleanField]', 1, {checked: cart.booleanField}) }}