您使用的代码非常陈旧且过时。下面的代码将检查应具有最小订单量的 Wholesale 用户角色的购物车项目:
// Cart and checkout validation
add_action( 'woocommerce_check_cart_items', 'minimal_total_required' ); // Cart and Checkout
add_action( 'woocommerce_checkout_process', 'minimal_total_required' ); // Checkout (optional)
function minimal_total_required() {
$user = wp_get_current_user();
## -- YOUR SETTINGS BELOW -- ##
$min_amount = 50; // Minimal order amount
$targeted_role = 'wholesale_buyer'; // User role
// Exit for non logged users or when minimal order amout is reached
if( $user->ID == 0 || WC()->cart->subtotal >= $min_amount )
return;
// Display an error notice for Wholesale user role
if ( in_array( $targeted_role, $user->roles ) )
wc_add_notice( sprintf( __("As a Wholesale user you must have a minimal order total of %s.") , wc_price($min_amount) ), 'error' );
}
代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。
对于两个用户角色和两个最小数量,您将使用以下内容:
// Cart and checkout validation
add_action( 'woocommerce_check_cart_items', 'minimal_total_required' ); // Cart and Checkout
add_action( 'woocommerce_checkout_process', 'minimal_total_required' ); // Checkout (optional)
function minimal_total_required() {
$user = wp_get_current_user();
// Exit for non logged users
if( $user->ID == 0 ) return;
## -- YOUR SETTINGS BELOW (For 2 user roles and 2 minimal amounts) -- ##
$min_amount = array( 50, 40 ); // Minimal order amounts
$targeted_role = array('wholesale_buyer', 'customer'); // Targetted User roles
$cart_subtotal = WC()->cart->subtotal;
// Wholesale user
if ( in_array( $targeted_role[0], $user->roles ) && $cart_subtotal < $min_amount[0]){
$text = sprintf( __('As a Wholesale user you must have a minimal order total amount of %s.'), wc_price($min_amount[0]) );
}
// Customer user
elseif ( in_array( $targeted_role[1], $user->roles ) && $cart_subtotal < $min_amount[1]){
$text = sprintf( __('You must have a minimal order total amount of %s.'), wc_price($min_amount[1]) );
}
// Display an error notice for Wholesale user role
if( isset($text) )
wc_add_notice( $text, 'error' );
}
代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。
它将为每个用户角色显示不同的通知。