【发布时间】:2020-12-10 00:59:00
【问题描述】:
我有一种情况,用户不能从特定类别将超过 1 个产品添加到购物车。我尝试使用woocommerce_add_to_cart_validation 过滤钩。
此代码适用于所有商品,并将最后添加的商品保存在购物车中:
// before add to cart, only allow 1 item in a cart
add_filter( 'woocommerce_add_to_cart_validation', 'woo_custom_add_to_cart_before' );
function woo_custom_add_to_cart_before( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return true;
}
此代码不起作用:
add_filter( 'woocommerce_add_to_cart_validation', 'woo_custom_add_to_cart_before' );
function woo_custom_add_to_cart_before( $cart_item_data ) {
if( is_product_category( 'test' ) ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return true;
}
}
有什么帮助吗?
【问题讨论】:
-
在工作版本中,无论如何您都返回 true - 在第二个版本中,您现在仅在类别为测试时才这样做。
标签: php wordpress woocommerce cart taxonomy-terms