WooCommerce Bulk Price Setup with ACF Repeater Fields

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.

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.