简体   繁体   English

Woocommerce-将商品添加到购物车-将其添加到全球所有会话中

[英]Woocommerce - Adding an item to cart - adds it across all sessions globally

Working on a WP Woocommerce, 在WP Woocommerce上工作,

If I add an item using https://url/?add-to-cart=1561&quantity=4 I see the item in every cart in every other session on the site, regardless if I was logged in or not. 如果我使用https:// url /?add-to-cart = 1561&quantity = 4添加商品,则无论我是否登录,我都会在网站上其他每个会话的每个购物车中看到该商品。

For example: I add the item in browser (not logged in) and I see the product on another browser. 例如:我在浏览器中添加了该项目(未登录),并且在另一个浏览器中看到了该产品。

I've tried, 我试过了,

  • Turning off all plugins except woocommerce and max menu 关闭除woocommerce和max菜单外的所有插件
  • Switching to default theme 切换到默认主题
  • Calling the url in the browser instead of a click event 在浏览器中调用网址,而不是单击事件

All result in a 'globally shared' cart across everyone visiting the site. 所有这些都会导致访问该网站的每个人都拥有一个“全球共享”的购物车。

Any help or suggestions would be appreciated. 任何帮助或建议,将不胜感激。

I hacked together a solution that has seemed to solve my issues. 我一起破解了一个似乎可以解决我的问题的解决方案。

It looks like my WC session was always setting a unique customer ID, the issue had something to do with the cart itself being used for multiple customer IDs. 看来我的WC会话总是在设置唯一的客户ID,问题与购物车本身用于多个客户ID有关。

My solution: 我的解决方案:

I set a session variable with a key of the customer id then assign all cart items to the variable. 我使用客户ID的键设置了会话变量,然后将所有购物车商品分配给该变量。

 $id = WC()->session->get_customer_id();
 WC()->session->set($id, $my_cart);

Every time the cart has an item added to it, ( Ex: /?add-to-cart=1561 ) I added that item to the $my_cart variable via action hook. 每次购物车中添加了商品时,(例如:/?add-to-cart = 1561),我都会通过操作钩子将该商品添加到$ my_cart变量中。

 function add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {}
 add_action('woocommerce_add_to_cart', 'add_to_cart', 10, 6);

I essentially wrote my own functions to add/remove/update $my_cart which hooked into the woocommerce action hooks. 我本质上是写了自己的函数来添加/删除/更新$ my_cart,该函数挂接到woocommerce动作挂钩中。

Action Hooks Used: 使用的动作挂钩:

add_action('woocommerce_add_to_cart', 'add_to_cart', 10, 6);
add_action('woocommerce_remove_cart_item', 'remove_from_cart', 10, 1);
add_action('woocommerce_after_cart_item_quantity_update', 'update_cart', 10, 3);

Then on the cart page I added all the items from $my_cart to the 'official' woocommerce cart (making sure not to 're add' them to $my_cart) to get my desired result. 然后,在购物车页面上,我将所有商品从$ my_cart添加到“官方” woocommerce购物车(确保不要将其“重新添加”至$ my_cart)以得到我想要的结果。

Closing: 收盘:

Again, not exactly sure what issues caused this cart bug in the first place (my last thoughts were some sort of caching on the hosting platform) but this might be a good example for anyone who wants to store particular data in a woocommerce session via action hooks. 再次,不确定是什么原因首先导致了该购物车错误(我的最后想法是在托管平台上进行某种缓存),但这对于想要通过操作在woocommerce会话中存储特定数据的人来说可能是一个很好的例子。挂钩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM