400 Bad Request deleting Cart lines
I've got a Rails integration for tracking abandoned cart activity in Spree, but lately, it seems carts are failing to update -- specifically when an item is removed from the user's cart.
We've noticed that every DELETE request sent to /ecommerce/stores/{store_id}/carts/{cart_id}/lines/{line_id} returns a 400 error with In order to delete this line item, delete the parent resource.
The implication here is that if a customer deletes an item from their cart, in order to track that in MailChimp, we need to delete their entire cart and start over, which seems problematic.
Is there a different process we should be using to sync line item deletes?
Did you manage to get this fixed?
More or less.
Ultimately, due to the above error, updating a cart with line item changes requires us to delete any previously saved MailChimp record and resubmit it in order to post the updates. Though that functionally gives us what we need, it seems less efficient at it requires additional API calls:
ON UPDATE:
- First, check to see if a cart already exists
- If it exists, delete it
- Post the complete cart record
The only other alternative I can see from the documentation would be to send patch/post/delete requests as needed for each line item individually, which I believe leads to more API calls than the above scenario.
Given how frequently a cart object changes during its lifetime, it seems like the Carts endpoint would benefit from create or update functionality
Hi @mrogers-5s
Thanks for that, it will save me battling on trying to get that delete line item to work.
Hi @mrogers-5s
I managed to get the delete line item to work in this fork, if you're interested.
https://github.com/MatthewKennedy/spree_mailchimp_ecommerce/blob/master/app/models/spree/line_item_decorator.rb
line 8 in the above file gets the info of the deleted line item then passes them to the delete_line_item method that organises and formats the data, and then passes the order number and line id to the job file below to execute the line item delete on the Mailchimp api:
https://github.com/MatthewKennedy/spree_mailchimp_ecommerce/blob/master/app/jobs/spree_mailchimp_ecommerce/delete_line_item_job.rb
This extension uses background jobs in Sidekiq, to create, update and destroy a Mailchimp cart, because of this to destroy and then re-create a cart for updating proved troublesome depending on how Sidekiq executed the jobs and if a retry was initiated.
@MatthewKennedy @mrogers-5s sorry to resurrect. But in case you still run into this, have you tried deleting a line item when it is not the last item in the cart? I just had this happen to me. If I have multiple line items, the request is successful. If it is the last one, it gives me this same error response. In a way it makes sense, there is no point tracking a cart in Mailchimp that has no items.