Custom Bulk Pricing in WooCommerce Using ACF Fields

Custom Bulk Pricing in WooCommerce Using ACF Fields

 

To implement this feature, you’ll need to add a repeater field in ACF for defining quantity and pricing tiers on the product admin page. These prices will then be shown on the product page and used to calculate the total price based on selected quantity during checkout. Follow the steps below:

Step 1: Activate ACF Plugin

Ensure the ACF plugin is installed and activated on your site.

Step 2: Add ACF Repeater Field for Quantity & Price

In your WordPress dashboard, navigate to Custom Fields > Add New. Create a field group with a Repeater for quantity and price, and set their field types accordingly.

Step 3: Display Bulk Prices on Product Page

Insert the following code in your theme’s `functions.php` to pull and display repeater values on the single product page:

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: Dynamic Pricing When Adding to Cart

To adjust the price based on selected quantity, use this hook:

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

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

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

By implementing the steps above, you’ll enable WooCommerce to support dynamic pricing based on selected quantity using ACF.

Leave a Comment

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

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.