How to Add Custom Prices on Bulk Purchase in WooCommerce Using ACF

How to Add Custom Prices on Bulk Purchase in WooCommerce Using ACF

To achieve this functionality, you’ll need to create a custom ACF repeater field for quantity and price in the product admin panel and then display all the prices on the product single page. Additionally, you’ll have to calculate the price according to the selected quantity when a product is added to the cart using WordPress hooks. Here’s a step-by-step guide:

Step 1: Install and Activate ACF Plugin

Make sure you have the ACF plugin installed and activated on your WordPress site.

Step 2: Create ACF Repeater Field for Quantity and Price

In the WordPress admin panel, go to Custom Fields > Add New and create a new Field Group with your desired settings. Add a Repeater field for quantity and price. Make sure you set the field name and field type appropriately.

Step 3: Display All Prices on Product Single Page

To display all the prices on the product single page, you’ll need to hook into the single product page template and fetch the prices from the ACF repeater field. Add the following code to your theme’s functions.php file or a custom plugin:

html
Copy code

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 ( $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>' . $quantity . ' - ' . wc_price( $price ) . '</li>';
        }
        echo '</ul>';
    }
}
add_action( 'woocommerce_single_product_summary', 'display_all_prices_on_product_page', 25 );

Step 4: Calculate Price on Adding to Cart

To calculate the price according to the selected quantity when a product is added to the cart, you can use the woocommerce_before_calculate_totals hook. Add the following code to your theme’s functions.php file or a custom plugin:

html
Copy code

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

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $product = $cart_item['data'];

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

        // Calculate price based on selected quantity
        if ( $quantity_and_prices ) {
            $quantity = (int) $cart_item['quantity'];
            $calculated_price = null;

            foreach ( $quantity_and_prices as $item ) {
                $item_quantity = (int) $item['quantity'];
                $item_price = (float) $item['price'];

                if ( $quantity === $item_quantity ) {
                    $calculated_price = $item_price;
                    break;
                }
            }

            // If the calculated price is found, set it as the product price
            if ( $calculated_price ) {
                $product->set_price( $calculated_price );
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_price_based_on_quantity', 10, 1 );

This code will calculate the product price based on the selected quantity and set it accordingly before the cart totals are calculated.

By following these steps, you’ll have a custom ACF repeater field for quantity and price in the product admin panel, display all the prices on the product single page, and calculate the price based on the selected quantity when adding a product to the cart.

Related Blogs

Calculate distance between 2 address using latitude and longitude

To calculate the distance between two locations using their latitude and longitude coordinates, you can use the Haversine formula. The Haversine formula is commonly used to calculate the great-circle distance between two points on the surface of a sphere (such as the Earth) given their latitude and longitude.

Shortcodes included with WooCommerce

WooCommerce comes with several shortcodes that you can use to display various elements and functionalities on your WordPress website. These shortcodes allow you to customize the appearance and layout of your WooCommerce store. Here are some of the essential shortcodes included with WooCommerce:

How to Remove Product Tabs on Product Page

To remove product tabs on the WooCommerce product page, you can use the `woocommerce_product_tabs` filter. This filter allows you to modify or remove the default product tabs provided by WooCommerce. By removing the tabs you don’t want, you can customize the product page layout to suit your needs.

Request A Quote

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.