WordPress WooCommerce Add to Cart only adds one product

I’m quite new to WooCommerce and I can not figure out this one.
I’m creating a custom API (based on user requirement) and I’m letting user login in Laravel using a post request like this:

public function login (Request $req)
{
    global $woocommerce;
    $v = Validator::make($req->all(), [
        'username' => 'required|max:255',
        'password' => 'required|max:255',
    ]);
    if ($v->passes())
    {
        $user = wp_signon(['user_login'=>$req->input("username"),"user_password"=>$req->input("password")],true);
        if (is_wp_error($user) || !is_user_logged_in())
        {
            return response(['success' => false, 'message' => "Invalid Username or Password."], 401);
        }
        $key = md5($user->ID . $user->user_login. (time() + 7200) . $user->email);
        $cookie = wp_generate_auth_cookie($user->ID, (time() + 7200), "auth", $key);
        $store = [
            'user_login'      => $user->data->user_login,
            'user_id'         => $user->data->ID,
            'user_nicename'   => $user->data->user_nicename,
            'user_email'      => $user->data->user_email,
            'user_url'        => $user->data->user_url,
            'user_registered' => $user->data->user_registered,
            'display_name'    => $user->data->display_name,
        ];
        Session::push($cookie, $store);
        WC()->cart->set_session();
        return response(['auth_token' => $cookie, 'success' => true, 'cart'=>WC()->cart->get_cart_for_session(), 'timeout' => 7200], 200);
    }
    return response(['success' => false, 'message' => "Required field(s) missing."], 401);
}

Now, whenever I add a product to cart, it only replaces the previous value, and only one item is added to the cart. When I login to my WordPress site and visit the cart, it shows error “Undefined index”, while the cart is empty. Here’s the code for adding item to cart:

Read More
public function add($productId)
{
    //WC()->session->set_customer_session_cookie(true);
    $cart_id = WC()->cart->generate_cart_id( $productId );
    $prod_in_cart = WC()->cart->find_product_in_cart( $cart_id );
    // Add the product only if it's not in the cart already
    if( ! $prod_in_cart ) 
    {
        WC()->cart->add_to_cart($productId,1,0,null,null);
    }
    return response([
        'user'=>WC()->session->get_customer_id(),
        'cart'=>WC()->cart,
    ]);
}

I know I’m doing something wrong but don’t know what. Any help will be greatly appreciated.


Edit

If it helps I’m using WooCommerce version 2.3

Related posts

2 comments

  1. You have hardcoded the quantity as 1. Here is the function :

     WC()->cart->add_to_cart($productId,1,0,null,null);
    

    In this function, second parameter is the quantity. So raise the quantity if you want.

    $qty = 2;
    WC()->cart->add_to_cart($productId,$qty,0,null,null);
    
  2. As far as I’ve seen your code you’re using two different sessions

    WC()->cart->set_session(); & WC()->session->set_customer_session_cookie(true)

    I.E Once the user logged in and the second one is while it adds product to the cart. Did you check whether if things are working perfectly without logging in? Other possible problems would be theme integration or either way you’ve to turn your debug mode ON would help you where the problem is.

Comments are closed.