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.

Leave a Comment

Your email address will not be published. Required fields are marked *