在woocommerce中,我想为购物车中的特定产品设定最低数量。如果值较小,则会出现错误。
例如:只有当购物车中的产品a,b,c数量为2或更多时,客户才能结帐。
否则会出现错误 - "每件商品的最低订单数量为2"。
此论坛中有类似的主题,我认为此代码可能很有用。
add_filter( 'woocommerce_quantity_input_min', 'woocommerce_quantity_input_min',10, 2 );
function woocommerce_quantity_input_min( $min, $product ){
if ( $product-> ) {
$min = 2;
} else {
$min = 3;
}
return $min;
}
有人可以做这个吗?
我真正的需求:在我的商店,我卖了很多手表。共有6个产品类别:
我需要一个条件,客户只能检查购物车页面中的每个产品数量是否至少为2,无论它是哪个类别。
请注意,我们提供添加产品表单存档页面和单个产品页面。如果从存档页面添加,则默认值为1.
答案 0 :(得分:4)
更新2 (现在,对于存档页面和购物车中的产品变体也有效)。
以下代码将为特定产品类别设置最小数量:
// On single product pages
add_filter( 'woocommerce_quantity_input_args', 'min_qty_input_args', 20, 2 );
function min_qty_input_args( $args, $product ) {
## ---- Your settings ---- ##
$product_categories = array('Burberry', 'Daniel Wellington',
'Diesel', 'Emporio Armani', 'Marc Jacobs', 'Michael Kors');
$quantity = 2;
## ---- The code: set minimun quantity for specific product categories ---- ##
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
if( has_term( $product_categories, 'product_cat', $product_id ) ){
$args['min_value'] = $quantity;
}
return $args;
}
// On archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'min_qty_loop_add_to_cart_button', 50, 2 );
function min_qty_loop_add_to_cart_button( $button, $product ) {
// Only for non variable products
if( $product->is_type( 'variable' ) ) return $button; // Exit
## ---- Your settings ---- ##
$product_categories = array('Burberry', 'Daniel Wellington',
'Diesel', 'Emporio Armani', 'Marc Jacobs', 'Michael Kors');
$quantity = 2;
## ---- The code: set minimun quantity for specific product categories ---- ##
if( has_term( $product_categories, 'product_cat', $product->get_id() ) ){
$class = implode( ' ', array_filter( array(
'button',
'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
) ) );
$button = sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
);
}
return $button;
}
代码放在活动子主题(或活动主题)的function.php文件中。经过测试和工作。
对于产品标记,您只需更改&#39; product_cat&#39;通过&#39; product_tag&#39;在代码中。
ADDITION:对于产品IDS(ID数组)
要使其适用于产品ID而改为使用:
// On single product pages
add_filter( 'woocommerce_quantity_input_args', 'min_qty_input_args', 20, 2 );
function min_qty_input_args( $args, $product ) {
## ---- Your settings ---- ##
$product_ids = array('23', '52', '75', '87', '90', '102');
$quantity = 2;
## ---- The code: set minimun quantity for specific product Ids ---- ##
if( in_array( $product->get_id(), $product_ids ) || ( $product->is_type('variation') && in_array( $product->get_parent_id(), $product_ids ) ) ){
$args['min_value'] = $quantity;
}
return $args;
}
// On archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'min_qty_loop_add_to_cart_button', 50, 2 );
function min_qty_loop_add_to_cart_button( $button, $product ) {
// Only for non variable products
if( $product->is_type( 'variable' ) ) return $button; // Exit
## ---- Your settings ---- ##
$product_ids = array('23', '52', '75', '87', '90', '102');
$quantity = 2;
## ---- The code: set minimun quantity for specific product IDs ---- ##
if( in_array( $product->get_id(), $product_ids ) ){
$class = implode( ' ', array_filter( array(
'button',
'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
) ) );
$button = sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
);
}
return $button;
}
代码放在活动子主题(或活动主题)的function.php文件中。经过测试和工作。