WooCommerce 购物车 REST Api 无法将购物车关联到特定用户
WooCommerce Cart REST Api Cannot relate cart to specific user
我正在使用 PHP laravel 开发 WooCommerce 购物车 REST Api。以下是此类 api 的代码片段。当我调用 add_to_cart 函数时,它确实将产品添加到购物车中,我可以看到 wp_woocommerce_sessions 中存储了一条记录 table 具有任意会话密钥值。但我不知道如何将该记录与特定用户相关联,以便每个用户都有自己的购物车记录,当我调用 get_cart 函数或其他两个函数(即 remove_item 或 clear_cart) 使用特定的用户 ID 获取与该用户相关的记录,而不是任意记录.现在正在发生什么,例如,如果我使用给定的用户 ID 调用 add_to_cart 函数,例如 ID 为 2,那么如果我调用get_cart 函数与另一个用户 id,例如 id 1,我仍然得到了以前存储的记录,该记录是为用户 2 准备的。我希望我解释了我的问题好...
我尝试在代码的顶部添加私有函数,set_current_user,我认为它可能有助于解决问题,但是,所有这些它所做的是将 wp_woocommerce_sessions 处的会话密钥值更改为与用户 ID 相同,而不是任意值。此外,我尝试查看 woocommerce rest api 文档。和 wc_cart class 文档。最后尝试google解决
private function set_current_user(int $user_id)
{
$curr_user = wp_set_current_user( $user_id, '' );
wp_set_auth_cookie( $user_id);
do_action( 'wp_login',...array($curr_user->user_login, $curr_user));
}
/**
* Get cart.
*/
public function get_cart(int $user_id)
{
try {
if (!isset($user_id)) {
throw new \Exception("You must include user id in your request.", 400);
}
$this->set_current_user($user_id);
$cart = WC()->cart->get_cart();
if (WC()->cart->get_cart_contents_count() <= 0) {
return response()->json('There are no items in the cart!', 200);
}
foreach ($cart as $item_key => $cart_item) {
$_product = apply_filters('wc_cart_rest_api_cart_item_product', $cart_item['data'], $cart_item, $item_key);
// Adds the product name as a new variable.
$cart[$item_key]['product_name'] = $_product->get_name();
}
return response()->json($cart, 200);
} catch (\Exception $e) {
return response()->json(
array('status' => $e->getCode(), 'message' => $e->getMessage()), $e->getCode());
}
}
/**
* Add to Cart.
*/
public function add_to_cart(Request $request, int $user_id)
{
try {
if (!isset($user_id)) {
throw new \Exception("You must include user id in your request.", 400);
}
$this->set_current_user($user_id);
$product_id = !isset($request['product_id']) ? 0 : absint($request['product_id']);
$quantity = !isset($request['quantity']) ? 1 : absint($request['quantity']);
$cart_item_data = !isset($request['cart_item_data']) ? array() : $request['cart_item_data'];
$this->validate_product($product_id, $quantity);
$product_data = wc_get_product($product_id);
if (!$product_data || 'trash' === $product_data->get_status()) {
throw new \Exception('Warning: This product does not exist!', 400);
}
$product_cart_id = WC()->cart->generate_cart_id($product_data->get_id());
$found_in_cart = WC()->cart->find_product_in_cart($product_cart_id);
// check for existing item in cart.
if ($found_in_cart) {
throw new \Exception(sprintf('You cannot add another "%s" to your cart.', $product_data->get_name()), 400);
}
// Add item to cart.
$item_key = WC()->cart->add_to_cart($product_id, $quantity, 0, array(), $cart_item_data);
// Return response to added item to cart or return error.
if ($item_key) {
$data = WC()->cart->get_cart_item($item_key);
do_action('wc_cart_rest_add_to_cart', $item_key, $data);
if (is_array($data)) {
return response()->json($data, 200);
}
} else {
throw new \Exception(sprintf('You cannot add "%s" to your cart.', $product_data->get_name()), 400);
}
} catch (\Exception $e) {
return response()->json(
array('status' => $e->getCode(), 'message' => $e->getMessage()), $e->getCode());
}
}
/**
* Validate product before it is added to the cart.
*/
protected function validate_product($product_id = null, $quantity = 1)
{
$this->validate_product_id($product_id);
$this->validate_quantity($quantity);
}
/**
* Validate the product id argument.
*/
protected function validate_product_id($product_id)
{
if ($product_id <= 0) {
throw new \Exception('Product ID number is required!', 400);
}
if (!is_numeric($product_id)) {
throw new \Exception('Product ID must be numeric!', 400);
}
}
/**
* Validate the product quantity argument.
*/
protected function validate_quantity($quantity)
{
if ($quantity <= 0) {
throw new \Exception('Quantity can not be zero!', 400);
}
if (!is_numeric($quantity)) {
throw new \Exception('Quantity must be numeric!', 400);
}
}
/**
* Remove Item in Cart.
*/
public function remove_item(Request $request, int $user_id)
{
try {
if (!isset($user_id)) {
throw new \Exception("You must include user id in your request.", 400);
}
$this->set_current_user($user_id);
$cart_item_key = !isset($request['cart_item_key']) ? '0' : wc_clean($request['cart_item_key']);
if ($cart_item_key != '0') {
if (WC()->cart->remove_cart_item($cart_item_key)) {
return response()->json(
array('status' => 200, 'message' => 'Item has been removed from cart.'), 200);
} else {
throw new \Exception('Unable to remove item from cart.', 400);
}
} else {
throw new \Exception('Cart item key is required!', 400);
}
} catch (\Exception $e) {
return response()->json(
array('status' => $e->getCode(), 'message' => $e->getMessage()), $e->getCode());
}
}
/**
* Clear cart.
*/
public function clear_cart(int $user_id)
{
try {
if (!isset($user_id)) {
throw new \Exception("You must include user id in your request.", 400);
}
$this->set_current_user($user_id);
WC()->cart->empty_cart();
WC()->session->set('cart', array()); // Empty the session cart data
if (WC()->cart->is_empty()) {
return response()->json(array('status' => 200, 'message' => 'Cart is cleared.'), 200);
} else {
throw new \Exception('Clearing the cart failed!', 400);
}
} catch (\Exception $e) {
return response()->json(
array('status' => $e->getCode(), 'message' => $e->getMessage()), $e->getCode());
}
}
我希望有一个基于用户 ID 的特定用户的购物车,当我 adding/retrieving/removing/clearing 购物车时,这些操作适用于该特定用户的购物车,但是,当
我发现我应该使用当前用户 ID 从用户元 table 中读取元数据购物车条目并将其加载到当前会话,在内存购物车对象中完成后我应该更新用户元条目...
我正在使用 PHP laravel 开发 WooCommerce 购物车 REST Api。以下是此类 api 的代码片段。当我调用 add_to_cart 函数时,它确实将产品添加到购物车中,我可以看到 wp_woocommerce_sessions 中存储了一条记录 table 具有任意会话密钥值。但我不知道如何将该记录与特定用户相关联,以便每个用户都有自己的购物车记录,当我调用 get_cart 函数或其他两个函数(即 remove_item 或 clear_cart) 使用特定的用户 ID 获取与该用户相关的记录,而不是任意记录.现在正在发生什么,例如,如果我使用给定的用户 ID 调用 add_to_cart 函数,例如 ID 为 2,那么如果我调用get_cart 函数与另一个用户 id,例如 id 1,我仍然得到了以前存储的记录,该记录是为用户 2 准备的。我希望我解释了我的问题好...
我尝试在代码的顶部添加私有函数,set_current_user,我认为它可能有助于解决问题,但是,所有这些它所做的是将 wp_woocommerce_sessions 处的会话密钥值更改为与用户 ID 相同,而不是任意值。此外,我尝试查看 woocommerce rest api 文档。和 wc_cart class 文档。最后尝试google解决
private function set_current_user(int $user_id)
{
$curr_user = wp_set_current_user( $user_id, '' );
wp_set_auth_cookie( $user_id);
do_action( 'wp_login',...array($curr_user->user_login, $curr_user));
}
/**
* Get cart.
*/
public function get_cart(int $user_id)
{
try {
if (!isset($user_id)) {
throw new \Exception("You must include user id in your request.", 400);
}
$this->set_current_user($user_id);
$cart = WC()->cart->get_cart();
if (WC()->cart->get_cart_contents_count() <= 0) {
return response()->json('There are no items in the cart!', 200);
}
foreach ($cart as $item_key => $cart_item) {
$_product = apply_filters('wc_cart_rest_api_cart_item_product', $cart_item['data'], $cart_item, $item_key);
// Adds the product name as a new variable.
$cart[$item_key]['product_name'] = $_product->get_name();
}
return response()->json($cart, 200);
} catch (\Exception $e) {
return response()->json(
array('status' => $e->getCode(), 'message' => $e->getMessage()), $e->getCode());
}
}
/**
* Add to Cart.
*/
public function add_to_cart(Request $request, int $user_id)
{
try {
if (!isset($user_id)) {
throw new \Exception("You must include user id in your request.", 400);
}
$this->set_current_user($user_id);
$product_id = !isset($request['product_id']) ? 0 : absint($request['product_id']);
$quantity = !isset($request['quantity']) ? 1 : absint($request['quantity']);
$cart_item_data = !isset($request['cart_item_data']) ? array() : $request['cart_item_data'];
$this->validate_product($product_id, $quantity);
$product_data = wc_get_product($product_id);
if (!$product_data || 'trash' === $product_data->get_status()) {
throw new \Exception('Warning: This product does not exist!', 400);
}
$product_cart_id = WC()->cart->generate_cart_id($product_data->get_id());
$found_in_cart = WC()->cart->find_product_in_cart($product_cart_id);
// check for existing item in cart.
if ($found_in_cart) {
throw new \Exception(sprintf('You cannot add another "%s" to your cart.', $product_data->get_name()), 400);
}
// Add item to cart.
$item_key = WC()->cart->add_to_cart($product_id, $quantity, 0, array(), $cart_item_data);
// Return response to added item to cart or return error.
if ($item_key) {
$data = WC()->cart->get_cart_item($item_key);
do_action('wc_cart_rest_add_to_cart', $item_key, $data);
if (is_array($data)) {
return response()->json($data, 200);
}
} else {
throw new \Exception(sprintf('You cannot add "%s" to your cart.', $product_data->get_name()), 400);
}
} catch (\Exception $e) {
return response()->json(
array('status' => $e->getCode(), 'message' => $e->getMessage()), $e->getCode());
}
}
/**
* Validate product before it is added to the cart.
*/
protected function validate_product($product_id = null, $quantity = 1)
{
$this->validate_product_id($product_id);
$this->validate_quantity($quantity);
}
/**
* Validate the product id argument.
*/
protected function validate_product_id($product_id)
{
if ($product_id <= 0) {
throw new \Exception('Product ID number is required!', 400);
}
if (!is_numeric($product_id)) {
throw new \Exception('Product ID must be numeric!', 400);
}
}
/**
* Validate the product quantity argument.
*/
protected function validate_quantity($quantity)
{
if ($quantity <= 0) {
throw new \Exception('Quantity can not be zero!', 400);
}
if (!is_numeric($quantity)) {
throw new \Exception('Quantity must be numeric!', 400);
}
}
/**
* Remove Item in Cart.
*/
public function remove_item(Request $request, int $user_id)
{
try {
if (!isset($user_id)) {
throw new \Exception("You must include user id in your request.", 400);
}
$this->set_current_user($user_id);
$cart_item_key = !isset($request['cart_item_key']) ? '0' : wc_clean($request['cart_item_key']);
if ($cart_item_key != '0') {
if (WC()->cart->remove_cart_item($cart_item_key)) {
return response()->json(
array('status' => 200, 'message' => 'Item has been removed from cart.'), 200);
} else {
throw new \Exception('Unable to remove item from cart.', 400);
}
} else {
throw new \Exception('Cart item key is required!', 400);
}
} catch (\Exception $e) {
return response()->json(
array('status' => $e->getCode(), 'message' => $e->getMessage()), $e->getCode());
}
}
/**
* Clear cart.
*/
public function clear_cart(int $user_id)
{
try {
if (!isset($user_id)) {
throw new \Exception("You must include user id in your request.", 400);
}
$this->set_current_user($user_id);
WC()->cart->empty_cart();
WC()->session->set('cart', array()); // Empty the session cart data
if (WC()->cart->is_empty()) {
return response()->json(array('status' => 200, 'message' => 'Cart is cleared.'), 200);
} else {
throw new \Exception('Clearing the cart failed!', 400);
}
} catch (\Exception $e) {
return response()->json(
array('status' => $e->getCode(), 'message' => $e->getMessage()), $e->getCode());
}
}
我希望有一个基于用户 ID 的特定用户的购物车,当我 adding/retrieving/removing/clearing 购物车时,这些操作适用于该特定用户的购物车,但是,当
我发现我应该使用当前用户 ID 从用户元 table 中读取元数据购物车条目并将其加载到当前会话,在内存购物车对象中完成后我应该更新用户元条目...