0

I am facing the issue to update the persistent cart in my WooCommerce cart API when I am update product into the cart by rest api. it is not displayed in my website . and when I am adding the product from the website in the API I will get the product but when adding the product from API the website cart is overridden by the API cart products

my rest api code :

if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
    $cart = WC()->instance()->cart;
    if ( woo_in_cart( $product_id ) ) {
        $cart_id      = $cart->generate_cart_id( $product_id );
        $cart_item_id = $cart->find_product_in_cart( $cart_id );
        $cart->set_quantity( $cart_item_id, $quantity );
    } else {
        $cart->add_to_cart( $product_id, $quantity );
    }

} else {
    WC()->cart->add_to_cart( $product_id, $quantity );
}
if ( ! empty( $user_id ) ) {
    $full_user_meta['cart'] = WC()->cart->get_cart();;
    update_user_meta( $user_id, '_woocommerce_persistent_cart_1', $full_user_meta );
}
Ajay Ghaghretiya
  • 784
  • 1
  • 6
  • 30

1 Answers1

1

Updating woocommerce_persistent_cart_1 is not enough, you have to replace/update the session cart data present in wp_woocommerce_sessions table specific to user id too.

MakeWebBetter
  • 463
  • 3
  • 5