Comments (23)
Sorry.
It works without error when I'm not connected. (with or without persistent cart)
from co-cart.
@yaroldlord Are you able to remove it using Postman?
from co-cart.
I don't know how to use postman.
I use a simple javascript call
jQuery.each( [ "put", "delete" ], function( i, method ) {
jQuery[ method ] = function( url, data, callback, type ) {
if ( jQuery.isFunction( data ) ) {
type = type || callback;
callback = data;
data = undefined;
}
return jQuery.ajax({
url: url,
type: method,
dataType: type,
data: data,
success: callback
});
};
});
function remove_clicked(cart_item_key) {
return function () {
// noinspection JSUnresolvedFunction
$.delete("/wp-json/wc/v2/cart/cart-item", {cart_item_key: cart_item_key}, function () {
updateCart();
});
}
I try also with a curl call. it is the same:
curl "http://xxxxxx/wp-json/wc/v2/cart/cart-item" -X DELETE -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0" -H "Accept: */*" -H "Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3" --compressed -H "Referer: http://xxxxx/yyyy/private/projects/view/20180716021" -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "X-Requested-With: XMLHttpRequest" -H "Cookie: wordpress_test_cookie=WP+Cookie+check; sid=xxxxxxx; wordpress_logged_in_xxxxx=xxxxx; wp-settings-1=libraryContent"%"3Dbrowse; wp-settings-time-1=1535320895; woocommerce_items_in_cart=1; woocommerce_cart_hash=xxxxxx; wp_woocommerce_session_xxxxxx=xxxxxx" -H "Connection: keep-alive" --data "cart_item_key=e8fd4a8a5bab2b3785d794ab51fef55c"
Update work fine but it is not possible to have a quantity of 0 so it cannot be a workaround.
from co-cart.
I just success to use postman. It is the same. I receive a http 200 with "Item has been removed from cart." but the product is still in the cart.
from co-cart.
I just tried in Postman and it works fine. Can you try without authorization applied?
from co-cart.
@yaroldlord Have you tried without authorization using Postman yet?
from co-cart.
I just try and I have a strange behavior: If I try just after refreshing the page in my browser, I get in postman "Item has been removed from cart". (but the article is not removed) !!! If I try again, I receive "Unable to remove item from cart".
I'm also trying to debug the code of WC_REST_Cart_Controller::remove_item but I do not understand where the problem comes from: The code runs without error, the article is removed from the cart and yet when I ask again the cart it still contains the Article !!
Another point I corrected the error that prevented the update from working with a quantity to 0 ($product_data is not defined in WC_REST_Cart_Controller :: update_item) and the behavior is the same as with remove_item: the request runs without error but the item is not removed from the cart.
What's even stranger is that adding article and changing quantity work without error !!!
from co-cart.
I found someone with the same problem as me:
I do not feel lonely anymore ;)
from co-cart.
I try with a new account (not logged as admin for the site) and the problem is still present!
from co-cart.
Here an other with the same probleme: https://wordpress.org/support/topic/sometimes-remove_cart_item-does-not-removeing-item
from co-cart.
And three! https://wordpress.org/support/topic/problem-with-removing-item-from-cart/
from co-cart.
And four! https://wordpress.org/support/topic/remove-cart-item-not-working
from co-cart.
I just try and I have a strange behaviour: If I try just after refreshing the page in my browser, I get in postman "Item has been removed from cart". (but the article is not removed) !!! If I try again, I receive "Unable to remove item from cart".
The browser and postman do not connect so viewing the items in cart via the browser and removing them via postman will return false results.
Second, the articles that you have shared are issues with WooCommerce core. Nothing to do with the REST API that I have built.
Is your site or web app live or in a staging area that I can quickly examine?
from co-cart.
After running the API the changes are not kept in the wp_woocommerce_sessions table but only in the php session in all case (delete, update, add).
This error has the effect of making the add or update of a product work, but not the delete: the get_cart_from_session function has this behavior when it retrieves the information from the table it takes the information of the session php when they exist otherwise it take that of the table.
The fix is not just a call to persistent_cart_update because the current user has been invalidated by rest_cookie_check_errors which checks for the presence of _wpnonce
from co-cart.
So what needs to change?
from co-cart.
It's possible to disable persistant cart.
If I add in cart_rest_api_init:
add_filter( 'woocommerce_persistent_cart_enabled', '__return_false' );
The bug diseapear: The delete work fine.
Of course I lose the persistence of the cart at the end of a session.
It's just a bad workaround.
It seems to me that cart-rest-api has a structural flaw complicate to fix.
from co-cart.
The issue you have is with WooCommerce, not the API. That filter is for the core of WooCommerce so something is wrong with your setup. I have already confirmed that the API works to remove items. I suggest that you also update your PHP version to fill the requirements WooCommerce needs.
from co-cart.
I'm not agree with you.
The mistake is not in wordpress or woocommerce.
It's cart-rest-api that has an error.
The filter rest_cookie_check_errors is call when a route registered by register_rest_route is present.
If you call wp-json/wc/v2 you will get all the registered route.
rest_cookie_check_errors is a general and standard protection against CSRF.
To avoid blocking a _wpnonce parameter must be inserted in the call parameters.
This attribute must contain a secret specific to the session.
https://codex.wordpress.org/WordPress_Nonces
from co-cart.
I am able to remove items without the need of using nonces. So this is puzzling.
from co-cart.
Some plugin disable persistent cart. Perhaps...
I work with a fresh install of wordpress, woocommerce and cart-rest-api .and nothing else.
from co-cart.
I will run more tests when I have more free time to spend on this project and make adjustments.
from co-cart.
Fr your information, i managed to use the API with persistant cart enabled by doing these operations:
- Add a Rest call to get nonce (get_nonce)
- Call get_nonce from my backend by copying the header of the customer call (ie acting as a proxy)
- Add the _wpnonce attribute in API calls
- Add a call to persistent_cart_update within the API
For the 1 I used the trick shown here:
https://wordpress.stackexchange.com/questions/295471/nonce-retrieved-from-the-rest-api-is-invalid-and-different-from-nonce-generated
<?php
/*
Plugin Name: get_nonce Plugin
*/
$get_nonce = 'invalid';
$secret = 'mySecret';
add_action('wp_loaded', function () {
global $get_nonce;
$get_nonce = wp_create_nonce('wp_rest');
});
add_action('rest_api_init', function () {
register_rest_route('get_nonce', '/call', array(
'methods' => 'GET',
'callback' => function ($data = array()) {
global $get_nonce, $secret;
$param_secret = !isset($data['secret']) ? '' : wc_clean($data['secret']);
if ($secret == $param_secret) {
return $get_nonce;
}
return 'erreur';
},
'args' => [
'secret'
],
));
});
from co-cart.
@yaroldlord You never did confirm if you had the same issue as a guest (not logged in).
from co-cart.
Related Issues (20)
- Totals calculations are not right when a custom price is set for the product HOT 6
- Tweak: New installation of CoCart should not run if WooCommerce is not installed HOT 2
- Undefined session functions due to CoCart not keeping function names the same as WooCommerce session handler HOT 4
- Yoast head field for products API HOT 3
- Products disappear when you restore cart HOT 3
- Bug: Unable to validate product data passed when updating item quantity. Product data returns null HOT 4
- [Enhancement]: Add product validation for when attempting to update an item's quantity should the product no longer exists HOT 4
- JWT Authentication not working anymore. HOT 4
- Incorrect subtotal price? HOT 17
- There is no way to apply taxes and coupons with CoCart Cart while frontend cart/checkout page. HOT 3
- [Vulnerability] : Any user can choose price for products on cart HOT 1
- Custom Price Ignored after Load Cart from Session HOT 3
- Guest users cart is cleared when increasing quantity from the cart. HOT 1
- [Enhancement]: Authenticate via phone number HOT 1
- Mixing carts between users HOT 1
- Sync repository labels action test
- Updating a cart or removing an item from a cart add random products to the cart HOT 1
- problem with multiple reqs in a row HOT 1
- cookies and subdomains HOT 2
- TODO: Update WordPress plugin directory banner
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from co-cart.