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.

Categories

Related Blogs

How To Add New Post Type And Custom Taxonomy

In WordPress, `register_post_type` and `register_taxonomy` are essential functions that allow you to create custom post types and custom taxonomies, respectively. Custom post types and taxonomies give you the flexibility to organize and display different types of content beyond the default posts and pages.

Add Custom Taxonomy Filter on Products Dashboard.

How To Filter Products by Taxonomies in the Dashboard?

WooCommerce provide Many Product filters on admin screen , such as “Select a category”, “Filter by product type”, “Filter by stock status”.

Hide Products Based On Custom Field / ACF Value : WooCommerce

To hide products in WooCommerce based on a custom field or Advanced Custom Fields (ACF) value, you’ll need to use some custom code. Specifically, you’ll have to hook into the WooCommerce product query and modify it to include your custom field or ACF value as a condition for hiding products.

Request A Quote