[英]Clear Woocommerce cart when sending multiple items by url
我需要清理 Woocommerce 购物车,以防我通过 url 向它发送四个以上的项目。
我想出了这个代码,但它只在购物车中已经有五个项目时才清理它。
//in functions.php
add_filter( 'woocommerce_add_to_cart_validation', 'remove_cart_item_before_add_to_cart', 20, 3 );
function remove_cart_item_before_add_to_cart( $passed, $product_id, $quantity ) {
if( WC()->cart->get_cart_contents_count() >= 5 )
WC()->cart->empty_cart();
return $passed;
}
另一个问题是,即使上面对我没有用的代码,如果我通过 url 添加多个课程也不起作用,如下所示:
https://exemple.net/cart?fill_cart=100,101,102,103,104,105,106
也就是说, functions.php
的代码只能在网站上工作,而不是通过 url。
我只需要在通过 url发送超过 4 件商品时清除购物车。
我更喜欢 PHP 中的解决方案,但 JS 解决方案也可以。 谢谢谁能帮帮我。
我找到了一个使用免费插件Cart links for WooCommerce的解决方案,并对其代码进行了一些更改。
我将在下面描述我所做的:
1) 要开始安装插件,这将允许您使用 url 中的fill_cart
参数添加产品(如我的问题中所述)
2)编辑插件文件soft79-cart-links-for-woocommerce.php
找到: yoursite/wp-content/plugins/soft79-cart-links-for-woocommerce/
3) 转到第 141 行或找到方法fill_cart
的声明
在fill_cart
方法中,取消注释(或添加)清理购物车的代码:
public function fill_cart( $fill_string ) {
$original_notices = wc_get_notices();
wc_clear_notices();
////Clear the cart
//WC()->cart->empty_cart(); <-- uncomment here!
保存文件。 现在任何通过 url 到达的新项目都必须先清理购物车。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.