处理非空车时遇到一些问题。
我的目标是在用户登录时清空购物车。
我创建了一个看起来与WP登录挂钩的插件。
它似乎完全按照购物车查询命令的输出执行了我想要的操作,但是用户WC购物车中仍然有内容。
这是我的插件代码:
FileNotFoundError: [Errno 2] No such file or directory: "/Users/user/'store_name'{}"
脚本输出
function WPLoginAction() {
$myfile = fopen("/home3/fitnevq7/public_html/scriptlogs/wploginnew", "w");
$txt = "wp login start \n";
fwrite($myfile, $txt);
$ISEmpty = WC()->cart->is_empty();
$txt = "is Cart empty before = " . $ISEmpty . "\n";
fwrite($myfile, $txt);
$count= WC()->cart->get_cart_contents_count();
$txt = "var count = " . $count . "\n";
fwrite($myfile, $txt);
global $woocommerce;
$var = $woocommerce->cart->empty_cart(true);
$txt = "var result = " . $var . "\n";
fwrite($myfile, $txt);
$count= WC()->cart->get_cart_contents_count();
$txt = "var count after delete = " . $count . "\n";
fwrite($myfile, $txt);
$ISEmpty = WC()->cart->is_empty();
$txt = "is Cart empty after = " . $ISEmpty . "\n";
fwrite($myfile, $txt);
}
add_action('login_enqueue_scripts', 'WPLoginAction');
}
这表明脚本正在运行,但是WC购物车仍通过GUI显示一个项目。
请注意,我还使用下面的两行代码创建了一个WP页面,并且该页面运行正常。
wp login start
is Cart empty before =
var count = 1
var result =
var count after delete = 0
is Cart empty after = 1