PHPwoocommerceadvanced

Apply Discount Based on User Role

Automatically apply percentage discounts to WooCommerce products based on user roles

#woocommerce#discount#user-roles#pricing#wholesale
Share this snippet:

Code

php
1// Apply role-based discount to product prices
2function apply_role_based_discount($price, $product) {
3 // Only for logged-in users
4 if (!is_user_logged_in()) {
5 return $price;
6 }
7
8 // Define discount percentages for each role
9 $role_discounts = array(
10 'wholesale_customer' => 25, // 25% discount
11 'vip_customer' => 15, // 15% discount
12 'subscriber' => 10, // 10% discount
13 );
14
15 // Get current user
16 $user = wp_get_current_user();
17
18 // Check user role and apply discount
19 foreach ($role_discounts as $role => $discount) {
20 if (in_array($role, $user->roles)) {
21 $discount_amount = ($price * $discount) / 100;
22 return $price - $discount_amount;
23 }
24 }
25
26 return $price;
27}
28add_filter('woocommerce_get_price_html', 'apply_role_based_discount', 10, 2);
29add_filter('woocommerce_product_get_price', 'apply_role_based_discount', 10, 2);
30add_filter('woocommerce_product_get_regular_price', 'apply_role_based_discount', 10, 2);
31add_filter('woocommerce_product_variation_get_price', 'apply_role_based_discount', 10, 2);
32add_filter('woocommerce_product_variation_get_regular_price', 'apply_role_based_discount', 10, 2);
33
34// Display discount badge on product page
35function display_role_discount_badge() {
36 if (!is_user_logged_in()) {
37 return;
38 }
39
40 $role_discounts = array(
41 'wholesale_customer' => 25,
42 'vip_customer' => 15,
43 'subscriber' => 10,
44 );
45
46 $user = wp_get_current_user();
47
48 foreach ($role_discounts as $role => $discount) {
49 if (in_array($role, $user->roles)) {
50 echo '<div class="role-discount-badge">';
51 echo '<span class="discount-label">' . esc_html__('Your Discount:', 'woocommerce') . '</span> ';
52 echo '<span class="discount-amount">' . $discount . '%</span>';
53 echo '</div>';
54 break;
55 }
56 }
57}
58add_action('woocommerce_before_add_to_cart_button', 'display_role_discount_badge');
59
60// Show discount info in cart
61function add_role_discount_cart_message() {
62 if (!is_user_logged_in()) {
63 return;
64 }
65
66 $role_discounts = array(
67 'wholesale_customer' => 25,
68 'vip_customer' => 15,
69 'subscriber' => 10,
70 );
71
72 $user = wp_get_current_user();
73
74 foreach ($role_discounts as $role => $discount) {
75 if (in_array($role, $user->roles)) {
76 wc_print_notice(
77 sprintf(
78 __('You are receiving a %s%% discount as a %s!', 'woocommerce'),
79 $discount,
80 ucwords(str_replace('_', ' ', $role))
81 ),
82 'success'
83 );
84 break;
85 }
86 }
87}
88add_action('woocommerce_before_cart', 'add_role_discount_cart_message');
89
90// Add discount column in order details (Admin)
91function add_discount_info_to_order_admin($order) {
92 $user_id = $order->get_user_id();
93
94 if (!$user_id) {
95 return;
96 }
97
98 $user = get_userdata($user_id);
99 $role_discounts = array(
100 'wholesale_customer' => 25,
101 'vip_customer' => 15,
102 'subscriber' => 10,
103 );
104
105 foreach ($role_discounts as $role => $discount) {
106 if (in_array($role, $user->roles)) {
107 echo '<p class="form-field form-field-wide">';
108 echo '<strong>' . __('Role Discount Applied:', 'woocommerce') . '</strong><br>';
109 echo ucwords(str_replace('_', ' ', $role)) . ' - ' . $discount . '%';
110 echo '</p>';
111 break;
112 }
113 }
114}
115add_action('woocommerce_admin_order_data_after_billing_address', 'add_discount_info_to_order_admin');
116
117// Exclude sale items from role discount (optional)
118function exclude_sale_items_from_role_discount($price, $product) {
119 if ($product->is_on_sale()) {
120 return $product->get_sale_price(); // Return sale price without additional discount
121 }
122
123 return apply_role_based_discount($price, $product);
124}
125// Uncomment to activate:
126// add_filter('woocommerce_product_get_price', 'exclude_sale_items_from_role_discount', 10, 2);

Apply Discount Based on User Role

This snippet automatically applies percentage-based discounts to products based on the logged-in user's role. Perfect for wholesale stores, VIP customers, or membership sites.

// Apply role-based discount to product prices
function apply_role_based_discount($price, $product) {
    // Only for logged-in users
    if (!is_user_logged_in()) {
        return $price;
    }

    // Define discount percentages for each role
    $role_discounts = array(
        'wholesale_customer' => 25,  // 25% discount
        'vip_customer'       => 15,  // 15% discount
        'subscriber'         => 10,  // 10% discount
    );

    // Get current user
    $user = wp_get_current_user();

    // Check user role and apply discount
    foreach ($role_discounts as $role => $discount) {
        if (in_array($role, $user->roles)) {
            $discount_amount = ($price * $discount) / 100;
            return $price - $discount_amount;
        }
    }

    return $price;
}
add_filter('woocommerce_get_price_html', 'apply_role_based_discount', 10, 2);
add_filter('woocommerce_product_get_price', 'apply_role_based_discount', 10, 2);
add_filter('woocommerce_product_get_regular_price', 'apply_role_based_discount', 10, 2);
add_filter('woocommerce_product_variation_get_price', 'apply_role_based_discount', 10, 2);
add_filter('woocommerce_product_variation_get_regular_price', 'apply_role_based_discount', 10, 2);

// Display discount badge on product page
function display_role_discount_badge() {
    if (!is_user_logged_in()) {
        return;
    }

    $role_discounts = array(
        'wholesale_customer' => 25,
        'vip_customer'       => 15,
        'subscriber'         => 10,
    );

    $user = wp_get_current_user();

    foreach ($role_discounts as $role => $discount) {
        if (in_array($role, $user->roles)) {
            echo '<div class="role-discount-badge">';
            echo '<span class="discount-label">' . esc_html__('Your Discount:', 'woocommerce') . '</span> ';
            echo '<span class="discount-amount">' . $discount . '%</span>';
            echo '</div>';
            break;
        }
    }
}
add_action('woocommerce_before_add_to_cart_button', 'display_role_discount_badge');

// Show discount info in cart
function add_role_discount_cart_message() {
    if (!is_user_logged_in()) {
        return;
    }

    $role_discounts = array(
        'wholesale_customer' => 25,
        'vip_customer'       => 15,
        'subscriber'         => 10,
    );

    $user = wp_get_current_user();

    foreach ($role_discounts as $role => $discount) {
        if (in_array($role, $user->roles)) {
            wc_print_notice(
                sprintf(
                    __('You are receiving a %s%% discount as a %s!', 'woocommerce'),
                    $discount,
                    ucwords(str_replace('_', ' ', $role))
                ),
                'success'
            );
            break;
        }
    }
}
add_action('woocommerce_before_cart', 'add_role_discount_cart_message');

// Add discount column in order details (Admin)
function add_discount_info_to_order_admin($order) {
    $user_id = $order->get_user_id();

    if (!$user_id) {
        return;
    }

    $user = get_userdata($user_id);
    $role_discounts = array(
        'wholesale_customer' => 25,
        'vip_customer'       => 15,
        'subscriber'         => 10,
    );

    foreach ($role_discounts as $role => $discount) {
        if (in_array($role, $user->roles)) {
            echo '<p class="form-field form-field-wide">';
            echo '<strong>' . __('Role Discount Applied:', 'woocommerce') . '</strong><br>';
            echo ucwords(str_replace('_', ' ', $role)) . ' - ' . $discount . '%';
            echo '</p>';
            break;
        }
    }
}
add_action('woocommerce_admin_order_data_after_billing_address', 'add_discount_info_to_order_admin');

// Exclude sale items from role discount (optional)
function exclude_sale_items_from_role_discount($price, $product) {
    if ($product->is_on_sale()) {
        return $product->get_sale_price(); // Return sale price without additional discount
    }

    return apply_role_based_discount($price, $product);
}
// Uncomment to activate:
// add_filter('woocommerce_product_get_price', 'exclude_sale_items_from_role_discount', 10, 2);

Creating Custom User Roles

To create the custom roles used in this snippet:

// Run once to create roles (use a plugin or run in functions.php then remove)
function create_wholesale_customer_role() {
    add_role(
        'wholesale_customer',
        __('Wholesale Customer'),
        array(
            'read'         => true,
            'edit_posts'   => false,
            'delete_posts' => false,
        )
    );
}
add_action('init', 'create_wholesale_customer_role');

Advanced: Category-Specific Discounts

// Apply different discounts for specific categories
if (has_term('electronics', 'product_cat', $product->get_id())) {
    $discount = 20; // 20% off electronics for wholesale
} else {
    $discount = 25; // 25% off everything else
}

CSS for Discount Badge

.role-discount-badge {
    background: #2ea44f;
    color: white;
    padding: 10px 15px;
    border-radius: 4px;
    margin-bottom: 15px;
    display: inline-block;
}

.discount-amount {
    font-weight: bold;
    font-size: 1.2em;
}

Dependencies

  • WooCommerce

Related Snippets