• Resolved ajouve

    (@ajouve)


    Hi,

    I am building an api to manage my users cart

    First I had to create a function to enable the cart management

    private function loadCart()
        {
            WC()->frontend_includes();
            WC()->session = new WC_Session_Handler();
            WC()->session->init();
            WC()->customer = new WC_Customer( get_current_user_id(), true );
            WC()->cart = new WC_Cart();
        }

    Then I have a function to add produts to the cart

        public function updateCart($request)
        {
            $this->loadCart();
            foreach ($request->get_json_params() as $item) {
                WC()->cart->add_to_cart($item['id'], $item['quantity'], $item['variationId']);
            }
    
            return new WP_REST_Response(WC()->cart->get_cart());
        }

    This is not adding any product, WC()->cart->add_to_cart($item['id'], $item['quantity'], $item['variationId']) is returning me a cart key but the new item is not returned by WC()->cart->get_cart()

    If I am running WC()->cart->empty_cart(); just before my loop, all the new items are added to the cart but the old one are removed

    Thanks for the help

Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add or update cart quantity threw api’ is closed to new replies.