Add product to user’s Woocommerce Cart from Android Application

I am trying to use a php script hosted on the same webserver as a woocommerce site to act as an interface between my Android (and soon to be iOS) application.

I have implemented sessions by extracting the cookie from the response header and I also generate an authorization token that is sent in each response as well. However I do not know how to tell Woocommerce which user’s cart to add to. I figured wp_set_current_user() would work but it doesn’t look like it does.

Here is the relevant portion of the php script.

function authenticate($username, $password){
    global $authorized;
    global $user;
    $user = get_user_by('login', $username);

    if(wp_check_password($password, $user->data->user_pass, $user->ID)){
        wp_set_current_user($user->ID, $username);
        $authorized = true;
    } else{
        $authorized = false;
    }
}

function add_to_cart(){
    global $woocommerce;
    $cart = $woocommerce->cart;


    $worked = $cart->add_to_cart($_POST['product_id'], $_POST['quantity'], 
        $_POST['variation_id'], array(), 
        array());

    echo $worked; //Just for testing, currently the response is empty
}

If anyone could tell me how to let Woocommerce know which user the session is associated with it would be greatly appreciated.

Read more here: Add product to user’s Woocommerce Cart from Android Application

Leave a Reply

Your email address will not be published. Required fields are marked *