php - 如果商品已在购物车中,Woocommerce 和 Gravity 在添加到购物车时会收到默认确认消息
问题描述
我有 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 );
那没有用。有人可以帮忙吗?我有很多东西要学:) 谢谢。
解决方案
推荐阅读
- javascript - 是否可以在 JavaScript 中导出返回的属性/方法?
- django - NoReverseMatch at / Reverse for 'profile' with arguments '('',)' 未找到。尝试了 1 种模式:['profile/(?P
[^/]+)/$'] - django - 向我的 Django 应用程序添加评论部分表单
- r - 使用完整和填写 dplyr 将月薪数据转换为周薪
- firebase - 两次调用 Firebase initializeApp() 会产生负面影响吗?
- html - Django表单小部件Textarea直接设置为隐藏类型但需要它可见
- rxjs - 在switchMap中操作时保持状态
- python - 使用python selenium单击类中的span元素
- c++ - 如何引用模板化基类的嵌套类型
- javascript - React 组件不会在第一次状态更新时重新渲染