Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
gigo6000 opened this issue Jul 22, 2023 · 1 comment
Labels
bug commerce4 Issues related to Commerce v4

Comments

@gigo6000
Copy link

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",
@gigo6000 gigo6000 added commerce4 Issues related to Commerce v4 bug labels Jul 22, 2023
@lukeholder
Copy link
Member

lukeholder commented Jul 25, 2023

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}) }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug commerce4 Issues related to Commerce v4
Projects
None yet
Development

No branches or pull requests

2 participants