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

[5.x]: LineItems aren't updated with update cart action cart/update-cart #3895

Open
roguesherlock opened this issue Feb 13, 2025 · 1 comment

Comments

@roguesherlock
Copy link

What happened?

Description

When updating a cart, the line item is expected to update based on the id provided in the payload. However, the update fails because the code appears to use the array index instead of the provided id, preventing the correct line item from being updated.

Steps to reproduce

  1. Send a request to update the cart with the following JSON payload:
   {
       "lineItems": [{
           "id": 129,
           "qty": 10
       }]
   }
  1. Check the cart to see if the line item with id 129 has been updated.

Expected behavior

The cart should update the line item with the provided id (in this case, 129), so that the line item reflects the new quantity. In this case, the updated line item should show a quantity of 10.

Actual behavior

The cart message says cart updated but the line item is not updated.

I dug through the code a bit I see that it attempts to use the array index (0) as the line item ID instead of using the id field (129) from the line item object, causing the wrong item to be fetched or no item to be found.

foreach ($lineItems as $key => $lineItem) {
$lineItem = $this->_getCartLineItemById($key);

if I send the line Items as an object of key (id) /value (lineItem) pairs like the code expects it seems to work

   {
      "lineItems": {
        "129": {
            "id": 129,
            "qty": 10
        }
      }
   }

However the documentation states

Array of one or more of the cart's line items to update. Each must have an id key-value pair, and may include options, note, and qty key-value pairs.

I'm not sure if it's the expected behaviour and the documentation is incorrect (or I misunderstood it) or it's actually a bug.

Craft CMS version

Craft Pro 5.6.4

Craft Commerce version

5.3.1

PHP version

8.3.14

Operating system and version

Darwin 24.3.0

Database type and version

MySQL 8.4.4

Image driver and version

Imagick 3.7.0 (ImageMagick 7.1.1-41)

Installed plugins and versions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant