Custom Pricing for User Roles in WooCommerce with ACF
Offering personalized pricing for user roles can significantly improve customer satisfaction in WooCommerce. Learn how to set up custom pricing for specific user types using Advanced Custom Fields (ACF) with this step-by-step guide.
Step 1: Install and Activate ACF Plugin
First, install and activate the Advanced Custom Fields (ACF) plugin on your WooCommerce site. ACF enables you to add custom fields effortlessly, including custom pricing fields based on user roles.
Step 2: Create an ACF Field Group for Custom Pricing
Navigate to Custom Fields > Add New in the WordPress admin panel and create a new Field Group. Set up a Repeater Field within this group to capture custom prices. Within the Repeater, define fields for user roles (e.g., wholesaler, retailer, or member) and corresponding prices.
Step 3: Assign Custom Prices to User Roles
After creating the ACF field group, edit your WooCommerce products and specify custom prices for different user roles within the Repeater field. This approach allows you to easily adjust pricing for various roles.
Step 4: Apply Custom Prices with WooCommerce Filter
To make these custom prices active, hook into the woocommerce_product_get_price
filter, which enables dynamic pricing based on the current user’s role. Add the following code to your theme’s functions.php file or a custom plugin:
function custom_product_price_based_on_user_role( $price, $product ) { $current_user = wp_get_current_user(); $user_roles = $current_user->roles; $custom_prices = get_field( 'custom_prices', $product->get_id() ); if ( $custom_prices ) { foreach ( $custom_prices as $item ) { $role = $item['user_role']; $custom_price = (float) $item['price']; if ( in_array( $role, $user_roles ) ) { return $custom_price; } } } return $price; } add_filter( 'woocommerce_product_get_price', 'custom_product_price_based_on_user_role', 10, 2 );
This code snippet adjusts the price displayed to customers based on their role. Ensure that field names and values match the settings in your ACF field group.
Start Offering Custom Pricing Today!
Following these steps, you can seamlessly implement custom pricing for user roles in WooCommerce, enhancing the shopping experience by personalizing product prices for wholesalers, retailers, and other customer groups.