Cart API created shows a same product for all logged in users woocommerce

Question

We are creating Cart APIs for react APP.The cart shows the same product list for all logged-in users in woo-commerce. Tried with different plugins too.

function get_cart(){
   if (defined('WC_ABSPATH')) {
        // WC 3.6+ - Cart and notice functions are not included during a REST request.
        include_once WC_ABSPATH . 'includes/wc-cart-functions.php';
        include_once WC_ABSPATH . 'includes/wc-notice-functions.php';
    }

    if (null === WC()->session) {
        $session_class = apply_filters('woocommerce_session_handler', 'WC_Session_Handler');

        WC()->session = new $session_class();
        WC()->session->init();
    }

    if (null === WC()->customer) {
        WC()->customer = new WC_Customer(get_current_user_id(), true);
    }

    if (null === WC()->cart) {
        WC()->cart = new WC_Cart();

        // We need to force a refresh of the cart contents from session here (cart contents are normally refreshed on wp_loaded, which has already happened by this point).
    }
    print_r(WC()->cart->get_cart());
    if (null === WC()->session) {
        $session_class = apply_filters('woocommerce_session_handler', 'WC_Session_Handler');

        WC()->session = new $session_class();
        WC()->session->init();
    }

    if (null === WC()->customer) {
        WC()->customer = new WC_Customer(get_current_user_id(), true);
    }

    if (null === WC()->cart) {
        WC()->cart = new WC_Cart();

        // We need to force a refresh of the cart contents from session here (cart contents are normally refreshed on wp_loaded, which has already happened by this point).
    }
    return WC()->cart->get_cart();
}
0
, , , , Aparna Mathew 4 months 0 Answers 61 views 0

Leave an answer