1 回答

TA貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊
如下所示:
add_action( 'woocommerce_cart_calculate_fees', 'progressive_discount_based_on_cart_total', 10, 1 );
function progressive_discount_based_on_cart_total( $cart ) {
? ? // HERE we target other user roles than 'distributor_prices' (allowing guests)
? ? if ( current_user_can('distributor_prices') && is_user_logged_in() )
? ? ? ? return;
? ? if ( is_admin() && ! defined( 'DOING_AJAX' ) )
? ? ? ? return;
? ? $cart_total = $cart->get_cart_contents_total(); // Cart total
? ? if ( $cart_total >= 3000.00 && $cart_total < 5000.00 )
? ? ? ? $percent = 15; // 15%
? ? elseif ( $cart_total >= 1500.00 && $cart_total < 3000.00 )
? ? ? ? $percent = 10; // 10%
? ? elseif ( $cart_total >= 750.00 && $cart_total < 1500.00 )
? ? ? ? $percent =? 5; // 5%
? ? else
? ? ? ? $percent = 0;
? ? if ( $percent > 0 ) {
? ? ? ? $discount? ?= $cart_total * $percent / 100;
? ? ? ? $label_text = sprintf( __("Bulk Order Discount %s"), '('.$percent.'%)' );
? ? ? ? $cart->add_fee( $label_text, -$discount, true );
? ? }
}
代碼位于活動(dòng)子主題(或活動(dòng)主題)的functions.php 文件中。它應(yīng)該有效。
對(duì)于多個(gè)用戶角色,您將使用wp_get_current_user()來(lái)獲取當(dāng)前WP_User對(duì)象,然后您可以獲得roles如下屬性:
$user? ? ? ?= wp_get_current_user();
$user_roles = $user->roles;? // An array of the user roles
然后你將在代碼中替換:
? ? // HERE we target other user roles than 'distributor_prices' (allowing guests)
? ? if ( current_user_can('distributor_prices') && is_user_logged_in() )
? ? ? ? return;
經(jīng)過(guò):
? ? // HERE we target "wholesale_prices" and "wholesale_vat_exc" user roles (allowing guests)
? ? if ( ! array_intersect( wp_get_current_user()->roles, array('wholesale_prices', 'wholesale_vat_exc') && is_user_logged_in() )
? ? ? ? return;
或者
? ? // HERE we target "wholesale_prices" and "wholesale_vat_exc" user roles (allowing guests)
? ? if ( ! ( current_user_can('wholesale_prices') || current_user_can('wholesale_vat_exc') ) && is_user_logged_in() )
? ? ? ? return;
- 1 回答
- 0 關(guān)注
- 143 瀏覽
添加回答
舉報(bào)