Git Product home page Git Product logo

Comments (23)

yaroldlord avatar yaroldlord commented on May 22, 2024 1

Sorry.
It works without error when I'm not connected. (with or without persistent cart)

from co-cart.

seb86 avatar seb86 commented on May 22, 2024

@yaroldlord Are you able to remove it using Postman?

from co-cart.

yaroldlord avatar yaroldlord commented on May 22, 2024

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.

yaroldlord avatar yaroldlord commented on May 22, 2024

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.

seb86 avatar seb86 commented on May 22, 2024

I just tried in Postman and it works fine. Can you try without authorization applied?

from co-cart.

seb86 avatar seb86 commented on May 22, 2024

@yaroldlord Have you tried without authorization using Postman yet?

from co-cart.

yaroldlord avatar yaroldlord commented on May 22, 2024

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.

yaroldlord avatar yaroldlord commented on May 22, 2024

I found someone with the same problem as me:
I do not feel lonely anymore ;)

https://stackoverflow.com/questions/52029721/woocommerce-remove-cart-item-not-removing-items-when-logged-in

from co-cart.

yaroldlord avatar yaroldlord commented on May 22, 2024

I try with a new account (not logged as admin for the site) and the problem is still present!

from co-cart.

yaroldlord avatar yaroldlord commented on May 22, 2024

Here an other with the same probleme: https://wordpress.org/support/topic/sometimes-remove_cart_item-does-not-removeing-item

from co-cart.

yaroldlord avatar yaroldlord commented on May 22, 2024

And three! https://wordpress.org/support/topic/problem-with-removing-item-from-cart/

from co-cart.

yaroldlord avatar yaroldlord commented on May 22, 2024

And four! https://wordpress.org/support/topic/remove-cart-item-not-working

from co-cart.

seb86 avatar seb86 commented on May 22, 2024

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.

yaroldlord avatar yaroldlord commented on May 22, 2024

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.

seb86 avatar seb86 commented on May 22, 2024

So what needs to change?

from co-cart.

yaroldlord avatar yaroldlord commented on May 22, 2024

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.

seb86 avatar seb86 commented on May 22, 2024

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.

yaroldlord avatar yaroldlord commented on May 22, 2024

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.

seb86 avatar seb86 commented on May 22, 2024

I am able to remove items without the need of using nonces. So this is puzzling.

from co-cart.

yaroldlord avatar yaroldlord commented on May 22, 2024

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.

seb86 avatar seb86 commented on May 22, 2024

I will run more tests when I have more free time to spend on this project and make adjustments.

from co-cart.

yaroldlord avatar yaroldlord commented on May 22, 2024

Fr your information, i managed to use the API with persistant cart enabled by doing these operations:

  1. Add a Rest call to get nonce (get_nonce)
  2. Call get_nonce from my backend by copying the header of the customer call (ie acting as a proxy)
  3. Add the _wpnonce attribute in API calls
  4. 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.

seb86 avatar seb86 commented on May 22, 2024

@yaroldlord You never did confirm if you had the same issue as a guest (not logged in).

from co-cart.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.