Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使我的客户购物车在离开结账页面时清空?_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 如何使我的客户购物车在离开结账页面时清空?

Php 如何使我的客户购物车在离开结账页面时清空?,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,在我的电子商务商店中,单击“添加到购物车”按钮后,您将直接重定向到结帐页面。我想让它这样当你离开结帐页你的购物车是空的。有没有一个php代码可以使这项工作 我尝试过这个代码,但它有很多错误: add_action( 'wp_head', 'clear_cart' ); function clear_cart() { if ( wc_get_page_id( 'cart' ) == get_the_ID() || wc_get_page_id( 'checkout' ) == get_th

在我的电子商务商店中,单击“添加到购物车”按钮后,您将直接重定向到结帐页面。我想让它这样当你离开结帐页你的购物车是空的。有没有一个php代码可以使这项工作

我尝试过这个代码,但它有很多错误:

add_action( 'wp_head', 'clear_cart' );
function clear_cart() {
    if ( wc_get_page_id( 'cart' ) == get_the_ID() || wc_get_page_id( 'checkout' ) == get_the_ID() ) {
        return;
    }
    WC()->cart->empty_cart();
}

有时我的会话在结帐页面结束,或者我的购物车被随机清空。

我认为下面的内容可能会帮助您清空购物车。请添加
functions.php
文件

add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10,  3);
function wdm_empty_cart( $cart_item_data, $product_id, $variation_id ){
  global $woocommerce;
  $woocommerce->cart->empty_cart();
  // Do nothing with the data and return
  return $cart_item_data;
}

最近没有在woocommerce中工作过,但是看看这个代码正在检查的页面ID。这是非常有限的(购物车/结帐),这个功能是清除你的购物车,只要它不是这两个页面。我敢打赌那就是你得到随机清空的地方。不是一个完整的答案,但希望能帮助你写点什么/想点什么。看起来像是
WC()->cart->empty_cart()确实是您所需要的全部。找出如何在您的用例中使用它;)