休息 API 根据 woocommerce 中的用户 ID 将产品存储在购物车中

Rest API to store products in cart based on the user id in woocommerce

我发现只有少数人接受的相同的相关参考是下面提到的代码,但没有会话存储在选项 table 中,带有以下键 '_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);
}

在对 woo-commerce 使用持久购物车的方式进行大量研究后,我创建了一个可行的解决方案。

function woocomm_add_to_cart($param) {

global $wpdb;
$user_id = $param['user_id'];
$objProduct = new WC_Session_Handler();

$wc_session_data = $objProduct->get_session($user_id);

// Get the persistent cart may be _woocommerce_persistent_cart can be in your case check in user_meta table
$full_user_meta = get_user_meta($user_id,'_woocommerce_persistent_cart_1',true);

// create new Cart Object
$cartObj = new WC_Cart();

// Add old cart data to newly created cart object
if($full_user_meta['cart']) {
    foreach($full_user_meta['cart'] as $sinle_user_meta) {
        $cartObj->add_to_cart( $sinle_user_meta['product_id'], $sinle_user_meta['quantity']  );
    }
}

// Add product and quantities coming in request to the new cart object
if($param['products']){
    foreach($param['products'] as $prod) {
        $cartObj->add_to_cart( $prod['product_id'], $prod['quantity']  );
    }
}

$updatedCart = [];
foreach($cartObj->cart_contents as $key => $val) {
    unset($val['data']);
    $updatedCart[$key] = $val;
}

// If there is a current session cart, overwrite it with the new cart
if($wc_session_data) {
    $wc_session_data['cart'] = serialize($updatedCart);
    $serializedObj = maybe_serialize($wc_session_data);


    $table_name = 'wp_woocommerce_sessions';

    // Update the wp_session table with updated cart data
    $sql ="UPDATE $table_name SET 'session_value'= '".$serializedObj."', WHERE  'session_key' = '".$user_id."'";

    // Execute the query
    $rez = $wpdb->query($sql);
}

// Overwrite the persistent cart with the new cart data
$full_user_meta['cart'] = $updatedCart;
update_user_meta($user_id, '_woocommerce_persistent_cart_1', $full_user_meta);

$response = [
    'status' => true,
    'message' => 'Products successfully added to cart'
];

return rest_ensure_response($response);


}

这是其余 API 的请求 json 数据:

{"user_id": 15, "products" : [{"product_id":"81","quantity":"0.5"},{"product_id":"1817","quantity":"0.5"}]}

我对以前的代码做了一些更改,因为这段代码不能独立地与我一起工作。

  1. 我已经为我的 Vue.js 应用程序创建了 Rest API。

  2. 创建路由并创建函数并粘贴此代码用作添加到 购物车

  3. 这也可以在添加时更新web端用户的session 来自移动端

    if (checkloggedinuser()) {
    
        include_once WC_ABSPATH . 'includes/wc-cart-functions.php';
        include_once WC_ABSPATH . 'includes/wc-notice-functions.php';
        include_once WC_ABSPATH . 'includes/wc-template-hooks.php';
        global $wpdb;
        $user_id = get_current_user_id();
        WC()->session = new WC_Session_Handler();
        WC()->session->init();
    
        $wc_session_data = WC()->session->get_session($user_id);
    
        // Get the persistent cart may be _woocommerce_persistent_cart can be in your case check in user_meta table
        $full_user_meta = get_user_meta($user_id,'_woocommerce_persistent_cart_1',true);
        WC()->customer = new WC_Customer( get_current_user_id(), true );
        // create new Cart Object
        WC()->cart = new WC_Cart();
        // return $full_user_meta;
        // Add old cart data to newly created cart object
        if($full_user_meta['cart']) {
            foreach($full_user_meta['cart'] as $sinle_user_meta) {
                 WC()->cart->add_to_cart( $sinle_user_meta['product_id'], $sinle_user_meta['quantity']  );
            }
        }
    
        WC()->cart->add_to_cart( $request['product_id'], $request['quantity']  );
    
        $updatedCart = [];
    
    
        foreach(WC()->cart->cart_contents as $key => $val) {
            unset($val['data']);
            $updatedCart[$key] = $val;
            $updatedCart[$key]['file'] = "hello file herer";  
        }
    
        // If there is a current session cart, overwrite it with the new cart
        if($wc_session_data) {
            $wc_session_data['cart'] = serialize($updatedCart);
            $serializedObj = maybe_serialize($wc_session_data);
            $table_name = 'wp_woocommerce_sessions';
    
            // Update the wp_session table with updated cart data
            $sql ="UPDATE $table_name SET 'session_value'= '".$serializedObj."', WHERE  'session_key' = '".$user_id."'";
    
            // Execute the query
            $rez = $wpdb->query($sql);
        }
    
        // Overwrite the persistent cart with the new cart data
        $full_user_meta['cart'] = $updatedCart;
        update_user_meta($user_id, '_woocommerce_persistent_cart_1', $full_user_meta);
    
         return array(
            'success' => false,
            'responsecode' => 403,
            "message" => "Products successfully added to cart",
            "data" => [],
        );
    
    }else{
         return array(
            'success' => false,
            'responsecode' => 403,
            "message" => "Please Logged In to get Data",
            "data" => [],
        );
    }