We want to apply a 2% for only specific users with specific role, so we use this code in functions.php to solve that:
function tl_user_role_percent_discount( $cart ) {
global $woocommerce;
$percent = $discount = 0;
if (is_user_logged_in() ) {
$user = wp_get_current_user();
$user_meta = get_userdata( $user->ID );
$user_roles = $user_meta->roles;
if ( in_array( 'cliente_vip', $user_roles, true ) ) {
$percent = 2;
$discount = $cart->get_subtotal() * $percent / 100;
$woocommerce->cart->add_fee( 'Discount (2%)', -$discount);
}
}
}
add_action( 'woocommerce_cart_calculate_fees', 'tl_user_role_percent_discount', 99, 1 );
How to exclude the Shipping cost to not discount anything?
Thanks
你可以这样做
$discount = ( $cart->get_subtotal() - $cart->get_shipping_total() ) * $percent / 100;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.