wp-e-commerce / WP-e-Commerce

WP eCommerce - The most popular independent eCommerce platform for WordPress
https://wpecommerce.org
GNU General Public License v2.0
215 stars 217 forks source link

Sync cart model when the quantity is updated. #2343

Closed jbeales closed 6 years ago

jbeales commented 6 years ago

Possible fix for #2342.

Calls model.sync() after updating the quantity of an item in the cart. This isn't needed when calling collection.create() because it happens automatically.

Possible issues: Nothing's being passed as the 3rd parameter to the sync() method, (the third param should be an ojbect of options). I'm hoping that Backbone will use sane defaults.

See http://backbonejs.org/#Model-sync for more on the Sync method.

mihaijoldis commented 6 years ago

So with the code change i see the new ajax requests and items showing up correctly in the cart but im confused on something.

So i had 4 items in the cart (same item 4 times) and i added it one more time but i noticed the request has the quantity=5 param Request URL:http://www.localhost.com/wpec-master/wp-json/wpsc/v1/cart/17?quantity=5&_wp_nonce=88f4a06257

Wondered if thats the "right" behaviour or not. Again even so the correct item is added to cart and quantity for my cart item is now 5 as in the request.

Again the code works with the change im just confused on the quantity request.

jbeales commented 6 years ago

My understanding of how Backbone.js works is that it stores the entire state of the shopping cart client-side, then syncs the desired state of the object to the server, so in your ajax request with 5, it's saying "there should be 5 of this product in the cart" not "add 5 of this product to the cart".

What exactly is synced depends on what the sync() method is called on - in this case it's called on the product, so the state of that single product is sent to the server, not the entire cart.