1

I have made an REST API for synchronize user's cart item to web view to android view. I have used CoCart WP plugin Here are some scenarios:

  1. If I adding product from website I can get user's cart items in both side(Website & android view)
  2. Now if i'm adding product from android api it doesn't show in web view.

Here are details I'm using -

https://co-cart.github.io/co-cart-docs/#add-to-cart

POST /wp-json/wc/v2/cart/add

curl -X POST https://example.com/wp-json/wc/v2/cart/add \
    -H "Content-Type: application/json" \
  -d '{
    "product_id": 1722,
    "quantity": 1
  }'

So I need solutions that can synchronize user's cart item to web view to android.

What I'm thinking is it possible to add products to cart by particular user's ID? It makes my work more easy!!

  • You should try add to cart product manually. Using custom query to particular user using user_id. – Pratik Patel Apr 19 at 6:21
  • Thanks pratik for reply, I have tried WC()->cart->add_to_cart( $product_id ) that doesn't give any opt to add user id. Can you please give me further info. for same? – Parth Shah Apr 19 at 6:36
  • Actually WC() function is by default take current login user id by using get_current_user_id() but in api this same function is not working so we have to manually add product with custom query and i have done same thing in custom way so please check my answer – Pratik Patel Apr 19 at 6:44
  • Can you share link of your answer? – Parth Shah Apr 19 at 6:45
  • I just want to know how you have done same thing in custom way! – Parth Shah Apr 19 at 6:46
1
/* Add to cart product api */
add_action( 'rest_api_init', function () {
    register_rest_route( 'wp/v2', 'add_to_cart_product', array(
        'methods' => array('GET','POST'),
        'callback' => 'add_to_cart_product',
    ) );
} );

function add_to_cart_product(){

    //wp_set_current_user($_POST['user_id']);
    /*wp_set_auth_cookie($_POST['user_id']);*/

    /* Required Parameters
    $_POST['user_id']
    $_POST['product_id'] */

    global $woocommerce,$wpdb;

    $array = $wpdb->get_results("select meta_value from ".$wpdb->prefix."usermeta where meta_key='_woocommerce_persistent_cart_1' and user_id = ".$_POST['user_id']);
    $data =$array[0]->meta_value;
    $cart_data=unserialize($data);

    $flag = 0;
    foreach($cart_data['cart'] as $key => $val) {
        //$_product = $val['data'];
        if($val['product_id'] != $_POST['product_id']){
            $flag = 0;
        }
        elseif($val['product_id'] == $_POST['product_id']) {
            $flag = 2;

        }
    }
    if($flag == 2){
        $cart_data['cart'][$key]['quantity']++;
    }
    else{
        $string = $woocommerce->cart->generate_cart_id( $_POST['product_id'], 0, array(), $cart_data['cart'] );
        $product = wc_get_product( $_POST['product_id'] );
        $cart_data['cart'][$string] = array(
            'key' => $string,
            'product_id' => $_POST['product_id'],
            'variation_id' => 0,
            'variation' => array(),
            'quantity' => 1,
            'line_tax_data' => array(
                'subtotal' => array(),
                'total' => array()
            ),
            'line_subtotal' => $product->get_price(),
            'line_subtotal_tax' => 0,
            'line_total' => $product->get_price(),
            'line_tax' => 0,
        );



    //echo "<pre>";
    //print_r($cart_data);
    //exit;
    //$serialize_data = serialize($cart_data);
    //$woocommerce->cart->add_to_cart( $_POST['product_id'] );
    update_user_meta($_POST['user_id'],'_woocommerce_persistent_cart_1',$cart_data);
    return cart_items(); // API response whatever you want
}
  • Awesome man! It's working for me as I want just bit suggestion that code needs some minor updation that makes it standard. :) – Parth Shah Apr 19 at 7:28
  • Happy to help you man :) – Pratik Patel Apr 19 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.