View Categories

WooCommerce:编辑添加到购物车最小,最大和步进数量

< 1 min read

仿牌独立站出海专家

PHP代码段1:设置最小值,最大值,增量和起始值添加到购物车数量@ WooCommerce单个产品页面和购物车页面(简单产品)


add_filter( 'woocommerce_quantity_input_args', 'bloomer_woocommerce_quantity_changes', 10, 2 );
  
function bloomer_woocommerce_quantity_changes( $args, $product ) {
  
   if ( ! is_cart() ) {
 
      $args['input_value'] = 4; // Start from this value (default = 1) 
      $args['max_value'] = 10; // Max quantity (default = -1)
      $args['min_value'] = 4; // Min quantity (default = 0)
      $args['step'] = 2; // Increment/decrement by this value (default = 1)
 
   } else {
 
      // Cart's "min_value" is already 0 and we don't need "input_value"
      $args['max_value'] = 10; // Max quantity (default = -1)
      $args['step'] = 2; // Increment/decrement by this value (default = 0)
      // COMMENT OUT FOLLOWING IF STEP < MIN_VALUE
      // $args['min_value'] = 4; // Min quantity (default = 0)
 
   }
  
   return $args;
  
}

PHP代码段2:设置最小值添加到购物车数量@ WooCommerce单个产品页面(可变产品->单个变体)


add_filter( 'woocommerce_available_variation', 'bloomer_woocommerce_quantity_min_variation', 9999, 3 );

function bloomer_woocommerce_quantity_min_variation( $args, $product, $variation ) {
	$args['min_qty'] = 5;
	return $args;
}