in the online store, I want to introduce a % discount for a specific group of customers.
I have already created a new group and customers and added the code below, which gives a discount to the cart.
/* discount biblioteka */
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_user_role', 20, 1 );
function discount_based_on_user_role( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return; // Exit
// only for 'biblioteka' role
if ( ! current_user_can('biblioteka') )
return; // Exit
// Kortings percentage
$percentage = 40;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
// Applying discount
$cart->add_fee( sprintf( __("biblioteka discount (%s)", "woocommerce"), $percentage . '%'), - $discount, true );
}
I would like this discount to also be displayed in the store as a promotional price and I added the code below, which made the website stop working.
/* Custom prices by user role */
add_filter('woocommerce_product_get_price', 'custom_price_assign', 10, 2);
add_filter('woocommerce_product_variation_get_price', 'custom_price_assign', 10, 2); // For product variations (optional)
function custom_price_assign( $price, $product ) {
// Check if the user has a role of wholesaler
if ( current_user_can('biblioteka') ){
return $price * 0.40;
}
return $price;
}
What’s the simplest way I can display the regular price and the 40% off price as a promotional price for a special group of users?
2
Answers
In the end, I only stuck with the following
I have a question about how I can limit the promotional price. Since some of the products are on sale, users with the "Library" role have a double discount.
I would like a user with the "Library" role to be provided with a permanent 40% discount on the regular price, regardless of whether the product is in support.
Try the following revised code instead, that will display the discounted product price in shop and single product for :
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.