我在WooCommerce中为产品类别设置了最小数量。就我而言,我将最小数量设置为5件。我使用的代码工作正常,但我想为客户添加两个错误消息:
1)如果客户尝试通过单击“-”符号将数量更改为小于最小数量,我希望得到以下内容:“此产品的最小数量为5,请添加至少5个项目到篮子里
2)如果客户单击“添加到购物篮”按钮,我想输入以下内容:“此产品的最小数量为5。请检查您的购物篮”
可以在我的实际代码中添加一些代码吗?
add_filter( 'woocommerce_quantity_input_args', 'min_qty_filter_callback', 20, 2 );
function min_qty_filter_callback( $args, $product ) {
$category = 'Noten'; // The targeted product category
$min_qty = 5; // The minimum product quantity
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
if( has_term( $category, 'product_cat', $product_id ) ){
$args['min_value'] = $min_qty;
}
return $args;
}
答案 0 :(得分:0)
要添加到购物车验证,请添加以下代码段以验证您的商品数量-
function add_to_cart_validation( $flag, $product_id, $qunatity ) {
if( $qunatity < 5 ) { // if quantity less than 5
wc_add_notice( __( 'There is a minimum quantity of 5 items for this product. Please check your basket', 'text-domain' ), 'error' );
$flag = false;
}
return $flag;
}
add_filter( 'woocommerce_add_to_cart_validation', 'add_to_cart_validation', 10, 3 );