I have a PHP script on a WooCommerce webserver that interfaces with an Android app I am developing. I mainly use it for logging into WordPress accounts and adding/removing products to the WooCommerce cart.

These functions work just fine:

function add_to_cart(){
    return WC()->cart->add_to_cart(
        $_POST['product_id'], 
        $_POST['quantity'],
        $_POST['variation_id'],
        null, 
        null);
}

function remove_from_cart(){
    if(empty($_POST['cart_item_key'])){
        return 'Please specify cart_item_key';
    }
    WC()->cart->remove_cart_item($_POST['cart_item_key']);
    return 'Product removed from cart!';
}

This function also works, but it return an empty array…

function get_cart_contents(){
    return WC()->cart->get_cart();
}

However I know that the cart is not empty because when I add a product to the cart on the app it shows in the cart on the website. Is there a different function I should be calling? Am I forgetting to call something?

I’m on a learn-implement basis with PHP, constructive criticism welcome

Read more here: WC()->cart->get_cart() shows empty but not on cart page


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: