Only related reference for the same I found with accepted by few people is the below mentioned code but there is no session stored in options table with the following key ‘_wc_session_’.$user_id
function add_products_programmatically($user_id) {
// Get the current session data and saved cart
$wc_session_data = get_option('_wc_session_'.$user_id);
// Get the persistent cart
$full_user_meta = get_user_meta($user_id,'_woocommerce_persistent_cart', true);
// Create a new WC_Cart instance and add products programmatically
$cart = get_new_cart_with_products();
// If there is a current session cart, overwrite it with the new cart
if($wc_session_data) {
$wc_session_data['cart'] = serialize($cart->cart_contents);
update_option('_wc_session_'.$user_id, $wc_session_data);
}
// Overwrite the persistent cart with the new cart data
$full_user_meta['cart'] = $cart->cart_contents;
update_user_meta($user_id, '_woocommerce_persistent_cart', $full_user_meta);
}
3
Answers
After a lot of research to the way woo-commerce uses persistent cart I have created a working solution.
Here is the Request json data for the Rest API:
I have done some changes in previous code as this code is not working with me independently.
I have created Rest API for my Vue.js application.
Create route and make function and paste this code use as add to
cart
This can also update the session of user on web side when you add
from mobile end
this is the simplest solution:
reference from the link:
https://barn2.com/managing-cart-rest-api-woocommerce-3-6/