User Role-Based Pricing in WooCommerce Using ACF

ACF ACF Guide ACF Option Page ACF Tutorials PHP Woocommerce WooCommerce Customization WooCommerce Development Woocommerce Hooks WooCommerce Tips Wordpress WordPress Development WordPress Hacks WP Best Practices

User Role-Based Pricing in WooCommerce Using ACF Tutorial/Guide

User Role-Based Pricing in WooCommerce Using ACF

  Offering special pricing to specific user roles can help increase loyalty and conversions in your WooCommerce store. This guide shows how to configure role-based pricing using Advanced Custom Fields (ACF).

Step 1: Activate the ACF Plugin

Begin by installing and activating the Advanced Custom Fields plugin. It allows you to create flexible input fields, including role-specific pricing fields.

Step 2: Add a Repeater Field for Role Pricing

Go to Custom Fields > Add New and create a new Field Group. Within this group, add a Repeater Field named `custom_prices`, with sub-fields for `user_role` and `price`.

Step 3: Set Custom Prices per Role

Now edit your WooCommerce products. Under the ACF section, add price entries for each user role like wholesaler, retailer, or premium customer.

Step 4: Filter Product Price Based on User Role

Use the `woocommerce_product_get_price` filter to display a dynamic price based on the logged-in user's role. Here's the code:

function custom_price_by_user_role( $price, $product ) {
    $user = wp_get_current_user();
    $roles = $user->roles;
    $role_prices = get_field( 'custom_prices', $product->get_id() );

    if ( $role_prices ) {
        foreach ( $role_prices as $entry ) {
            if ( in_array( $entry['user_role'], $roles ) ) {
                return (float) $entry['price'];
            }
        }
    }

    return $price;
}
add_filter( 'woocommerce_product_get_price', 'custom_price_by_user_role', 10, 2 );

This function checks the user's role and returns the matching custom price if available.

Start Offering Dynamic Prices!

By applying these changes, your WooCommerce store will support personalized pricing based on user roles, offering tailored experiences for different customer groups.  

💡 Have a Coding Problem?

Search our archives or reach out to our team for solutions and expert advice.