I have this code which works perfectly and applies a 10% discount to users in the 'subscriber' category. Now I want to modify the code so that the discount is only applied to products that are in specific categories.
For example, I have three categories of products: 'products', 'exclusive-products', and 'subscriptions'. The discount should apply only to products that are in the first two categories, and not apply to the products within the 'subscriptions' category.
Can somebody help me modify this function to meet those requirements?
// Applying conditionally a discount for a specific user role
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 'subscriber' user role
if ( ! current_user_can('subscriber') )
return; // Exit
// HERE define the percentage discount
$percentage = 10;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
// Applying discount
$cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
}