1 回答
TA贡献1770条经验 获得超3个赞
如下所示:
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 );
}
}
代码位于活动子主题(或活动主题)的functions.php 文件中。它应该有效。
对于多个用户角色,您将使用wp_get_current_user()来获取当前WP_User对象,然后您可以获得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;
经过:
// 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 关注
- 115 浏览
添加回答
举报