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:
- If I adding product from website I can get user's cart items in both side(Website & android view)
- 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:
- If I adding product from website I can get user's cart items in both side(Website & android view)
- 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 | Show 1 more comment1 Answer
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
user_id
. – Pratik Patel Commented Apr 19, 2019 at 6:21WC()->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:36WC()
function is by default take current login user id by usingget_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