我有 5 个使用 Woocommerce 和 Gravity 产品插件的产品。我已将库存设置设置为“仅允许在一个订单中购买此商品中的一件”。我面临的问题是,如果用户将商品添加到购物车,然后他们返回并再次添加相同的商品,表单会显示默认确认消息“感谢您与我们联系!我们会尽快与您联系”。我尝试在表单设置中更改此消息,但没有任何区别。但这无论如何都不是我想要的解决方案。我找到了一些给我一个可接受的解决方案的东西(我需要取消勾选“只允许在一个订单中购买这个项目中的一个”才能使其工作)但仅适用于一个产品 ID。我的问题是如何使这个工作适用于多个 ID?
add_filter( 'woocommerce_add_to_cart_validation', 'limit_cart_items_from_category', 10, 3 );
function limit_cart_items_from_category ( $passed, $product_id, $quantity ){
// HERE define your product ID
$targeted_product_id = 37;
// Check quantity and display notice
if( $quantity > 1 && $targeted_product_id == $product_id ){
wc_add_notice( __('Only one item quantity allowed for this product', 'woocommerce' ), 'error' );
return false;
}
// Loop through cart items checking if the product is already in cart
foreach ( WC()->cart->get_cart() as $cart_item ){
if( $targeted_product_id == $product_id && $cart_item['data']->get_id() == $targeted_product_id ) {
wc_add_notice( __('This product is already in cart (only one item is allowed).', 'woocommerce' ), 'error' );
return false;
}
}
return $passed;
}
// Checking and removing quantity field for a specific product
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
function custom_quantity_input_args( $args, $product ) {
// HERE define your product ID
$targeted_product_id = 37;
if( $targeted_product_id == $product->get_id() )
$args['min_value'] = $args['max_value'] = $args['input_value'] = 1;
return $args;
}
我尝试更改
的两个实例$targeted_product_id = 37;
到
$targeted_product_ids = array( 2060, 1996, 2120, 2069, 2094 );
那没有用。请问有人可以帮忙吗?我有很多东西要学:) 谢谢。