使用Wordpress和WooCommerce,我需要帮助添加两个用户角色。我让它使用一个角色,但还需要将其应用于另一个角色。
下面我正在使用用户角色'批发',但此规则还需要应用于其他用户角色' us_wholesale'。
这是我在functions.php中的代码:
add_action( 'woocommerce_checkout_process', 'wdm_wu_minimum_order_amount' );
function wdm_wu_minimum_order_amount() {
$current_screen_user = wp_get_current_user();
if( in_array( 'wholesale', $current_screen_user->roles ) ) {
$minimum = 150;
if ( WC()->cart->subtotal < $minimum ) {
if( is_cart() ) {
wc_print_notice(
sprintf( 'You must have an order with a minimum of %s to place your order, your current order total is %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->subtotal )
), 'error'
);
} else {
wc_add_notice(
sprintf( 'You must have an order with a minimum of %s to place your order, your current order total is %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->subtotal )
), 'error'
);
} } } }
我在woocommerce cart-totals.php中的覆盖:
<?php
$current_screen_user = wp_get_current_user();
if( in_array( 'wholesale', $current_screen_user->roles ) ) {
$minimum = 150; // Set the minimum amt.
$cart_amt = WC()->cart->subtotal; // cart sub_total, this is actual total excluding discounts and shipping.
if ( $cart_amt < $minimum ) {
if( is_cart() ) {
//Added notices for cart page.
wc_print_notice(
sprintf( 'You must have an order with a minimum of %s to place your order, your current order total is %s.' ,
wc_price( $minimum ),
wc_price( $cart_amt )
), 'error'
);
} else {
//Added notice msg for checkout page.
wc_add_notice(
sprintf( 'You must have an order with a minimum of %s to place your order, your current order total is %s.' ,
wc_price( $minimum ),
wc_price( $cart_amt)
), 'error'
);
}
} else {
do_action( 'woocommerce_proceed_to_checkout' );
}
} else {
do_action( 'woocommerce_proceed_to_checkout' );
}
?>
答案 0 :(得分:0)
由于您只有两个角色需要检查,为什么不直接替换以下一行:
if( in_array( 'wholesale', $current_screen_user->roles ) ) {
与
if( in_array( 'wholesale', $current_screen_user->roles ) || in_array( 'us_wholesale', $current_screen_user->roles ) ){
这样,如果当前用户有wholesale
或us_wholesale
作为用户角色,您的当前(可能正在工作?)函数将会运行。