Create Quantity-Based Bulk Discounts with ACF and PHP

Bulk pricing encourages larger orders by offering discounts as the quantity increases. This tutorial shows you how to build a custom bulk pricing system using an ACF repeater field to define "Quantity" and "Price" pairs. We cover how to display these pricing tiers on the product page and, more importantly, how to use the woocommerce_before_calculate_totals hook to recalculate the cart item price based on the quantity added. This ensures a seamless checkout experience where users see their savings instantly as they add more items.

WooCommerce Bulk Price Setup with ACF Repeater Fields

ACF ACF Option Page ACF Tutorials Advanced Guides Woocommerce WooCommerce Custom Code Woocommerce Hooks WooCommerce Tips Wordpress WordPress Code Snippets WordPress Development WordPress Functions

WooCommerce Bulk Price Setup with ACF Repeater Fields Tutorial/Guide

WooCommerce Bulk Price Setup with ACF Repeater Fields

  Want to offer different prices based on order quantity? Use ACF to define quantity-based pricing for WooCommerce products. You’ll also update the price dynamically when a user adds an item to their cart.

Step 1: Install ACF Plugin

Ensure Advanced Custom Fields (ACF) plugin is active on your WordPress site.

Step 2: Create Quantity-Based Price Fields

Navigate to Custom Fields > Add New. Add a Repeater with fields for `quantity` and `price`. Assign the group to WooCommerce products.

Step 3: Show Pricing Options on Product Page

Paste this code in your `functions.php` or plugin to display available prices:

function display_all_prices_on_product_page() {
    global $post;

    // Check if the current post is a product
    if ( 'product' !== get_post_type( $post ) ) {
        return;
    }

    // Get the ACF repeater field data
    $quantity_and_prices = get_field( 'quantity_and_prices', $post->ID );

    // Display the prices if available
    if ( $quantity_and_prices ) {
        echo '<h2>All Prices:</h2>';
        echo '<ul>';
        foreach ( $quantity_and_prices as $item ) {
            $quantity = (int) $item['quantity'];
            $price    = (float) $item['price'];
            echo '<li>' . esc_html( $quantity ) . ' - ' . wc_price( $price ) . '</li>';
        }
        echo '</ul>';
    }
}
add_action( 'woocommerce_single_product_summary', 'display_all_prices_on_product_page', 25 );

Step 4: Update Price Dynamically in Cart

To calculate final price dynamically:

function update_price_by_qty( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    foreach ( $cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];
        $pricing = get_field( 'quantity_and_prices', $product->get_id() );

        if ( $pricing ) {
            $qty = (int) $cart_item['quantity'];
            foreach ( $pricing as $entry ) {
                if ( $qty === (int) $entry['quantity'] ) {
                    $product->set_price( (float) $entry['price'] );
                    break;
                }
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'update_price_by_qty', 10 );

This solution empowers your WooCommerce store to adjust product pricing based on user-selected quantity dynamically.

๐Ÿ’ก Have a Coding Problem?

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