Cookies 在Ajax交易期间更改购物车数据
在Ajax期间,我试图用保存的购物车替换当前的WooCommerce购物车 这是我正在使用的代码:Cookies 在Ajax交易期间更改购物车数据,cookies,woocommerce,Cookies,Woocommerce,在Ajax期间,我试图用保存的购物车替换当前的WooCommerce购物车 这是我正在使用的代码: function restore() { $current_user_id = get_current_user_id(); if ($current_user_id > 0) { if (function_exists('get_user_attribute')) { $buy_now_persistent_cart = get_u
function restore()
{
$current_user_id = get_current_user_id();
if ($current_user_id > 0) {
if (function_exists('get_user_attribute')) {
$buy_now_persistent_cart = get_user_meta($current_user_id, '_buy_now_persistent_cart', true);
error_log(print_r($buy_now_persistent_cart, 1));
if (!empty($buy_now_persistent_cart['cart'])) {
WC()->session->cart = $buy_now_persistent_cart['cart'];
WC()->session->applied_coupons = $buy_now_persistent_cart['applied_coupons'];
if (function_exists('delete_user_attribute')) {
//delete_user_attribute( $current_user_id, '_buy_now_persistent_cart' );
}
//delete_user_meta( $current_user_id, '_buy_now_persistent_cart' );
}
}
}
这是$buy\u now\u persistent\u购物车输出:
Array
(
[cart] => Array
(
[3430dcf4efe8aa0c418434656773a73a] => Array
(
[key] => 3430dcf4efe8aa0c418434656773a73a
[product_id] => 126096
[variation_id] => 0
[variation] => Array
(
)
[quantity] => 1
[data_hash] => b5c1d5ca8bae6d4896cf1807cdf763f0
[line_tax_data] => Array
(
[subtotal] => Array
(
)
[total] => Array
(
)
)
[line_subtotal] => 2.4
[line_subtotal_tax] => 0
[line_total] => 2.4
[line_tax] => 0
)
)
[applied_coupons] => Array
(
)
)
当我在设置新数据后输出WC()->session->cart时,它似乎已正确更改,但当Ajax完成时,更新的cart不在那里
我想这是关于保存购物车和/或设置购物车哈希和cookies
我就是搞不懂
任何帮助都将不胜感激。我将以不同的方式处理。首先清空用户购物车,然后添加所有产品。 比如:
if (!empty($buy_now_persistent_cart['cart'])) {
WC()->cart->empty_cart();
foreach($buy_now_persistent_cart['cart'] as $item){
WC()->cart->add_to_cart($item['product_id'], $item['quantity']);
}
}
我没有测试它,但总的来说,我认为这是一种比强制进行会话更好的方法谢谢。使用您的代码,我在Ajax期间将购物车内容打印到错误日志中,购物车也随之更新。但是在Ajax返回后,当我使用Ajax添加其他产品或重新加载页面时,购物车将清空。有什么建议吗?听起来像是与你正在使用的另一个代码相结合,试着看看你还有哪些地方是空的。如果没有其他地方,可以尝试删除我添加的空_cart()行,只是为了测试是。你是对的。Stripe的插件在调用我使用的钩子后清除购物车。我切换到一个稍后的钩子,你的代码和我的代码都在工作。非常感谢你!不客气,您随时可以选择此答案作为正确答案:)thx