Pawan Kumawat

Add Custom Price 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:

 

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 );

 

This code will display all the quantity and price combinations in an unordered list on the product single page.

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:

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.

With these steps, you should now 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.

Categories

Related Blogs

Add same product to cart twice instead of changing quantity in Cart

By default, WooCommerce allows customers to change the quantity of a product in the cart by updating the quantity input field on the cart page. If the quantity is increased, the product will be added to the cart only once with the updated quantity. However, if you want to allow customers to add the same product multiple times as separate items in the cart, you’ll need to customize the cart behavior.

How to add custom order status in WooCommerce

To add a custom order status and customize its email template using `register_post_status`, `wc_order_statuses`, `woocommerce_email_actions`, and `woocommerce_email_classes`, you can follow these steps: