plugins - Add Products to user's ID Woocommerce

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

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 -

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

curl -X POST  \
    -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!!

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/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!!

Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked Apr 19, 2019 at 6:11 Parth ShahParth Shah 881 silver badge11 bronze badges 6
  • You should try add to cart product manually. Using custom query to particular user using user_id. – Pratik Patel Commented Apr 19, 2019 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 Commented Apr 19, 2019 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 Commented Apr 19, 2019 at 6:44
  • Can you share link of your answer? – Parth Shah Commented Apr 19, 2019 at 6:45
  • I just want to know how you have done same thing in custom way! – Parth Shah Commented Apr 19, 2019 at 6:46
 |  Show 1 more comment

1 Answer 1

Reset to default 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
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745279973a4620232.html

相关推荐

  • plugins - Add Products to user&#39;s ID Woocommerce

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

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信