admin管理员组文章数量:1417661
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
}
本文标签: pluginsAdd Products to user39s ID Woocommerce
版权声明:本文标题:plugins - Add Products to user's ID Woocommerce 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745279973a2651377.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