Custom Pricing for User Roles in WooCommerce with ACF

Custom Pricing for User Roles in WooCommerce with ACF

Learn how to set up custom pricing based on user roles in WooCommerce using Advanced Custom Fields (ACF). Follow these simple steps to offer tailored pricing for different user types.

Step 1: Install and Activate ACF Plugin

Begin by installing and activating the ACF plugin on your WordPress website. ACF provides powerful tools for adding custom fields to your site’s content.

Step 2: Create ACF Field Group for Custom Pricing

In the WordPress admin panel, navigate to Custom Fields > Add New. Create a new Field Group for custom pricing, including a Repeater field. Within the Repeater, define fields for user roles and their corresponding prices.

Step 3: Set Custom Prices for User Roles

Edit your products and enter custom prices for different user roles using the ACF Repeater field you created. This allows you to tailor pricing based on user roles such as wholesalers, retailers, or members.

Step 4: Apply Custom Prices Based on User Role

Hook into the woocommerce_product_get_price filter to dynamically adjust product prices based on the user’s role. Use the provided code snippet in your theme’s functions.php file or a custom plugin to implement this functionality.

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 dynamically adjusts product prices based on the user’s role. Customize field names and values to match your ACF settings.

With these steps, you can implement custom pricing for user roles in WooCommerce using ACF, providing a personalized shopping experience for your customers.

Related Blogs

How to hide particular category product on shop

To hide a particular category on the shop page using the `woocommerce_product_query` hook, you can modify the query parameters to exclude the category you want to hide. This approach allows you to customize the product query directly without modifying the main query.

Request A Quote

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.