phpwordpresswoocommerceproduct

Set a min unit displayed price for variable products in Woocommerce


I'm building a webshop in WooCommerce for selling Medical Gloves. They are being sold by unit, by box or by palet. When you buy by box or palet, you get a lower unit price.

I've been playing around for a while but I can't seem to get the configuration just how I want.

Let me give an example first.
Product A:
Price per unit: €1,90.
Price per unit when buying a box: €1,76 (120 units).
Price per unit when buying a pallet: €1,63 (2880 units).

What i would like is the following:
- on the archive page it should show: From €1,63.
- on the product page, users can select buy per unit / box / pallet.
- depending on the selection, the price should calculate automatically. So if user chooses 1 pallet, the price should be 2880*1,63=4694,40. Or if user chooses 2 pallets, the price should be (2880*1,63)*2

I've been experimenting with minimum maximum quantities. I've entered the unit prices in the variation tabs and added minimum quantities and steps. For example the pallet minimum of 2880 units and 2880 steps. Basically it works but... I think it would be confusing for the client if they see 2880 as a quantity in the order form, instead of just 1 pallet.

Other possibility is if I just directly add the total prices to the variation tabs, so €4694,40 for a pallet. This also works but... on the archive page it shows From €1,90. So they don't see directly they can get a unit from 1,63 if they buy per pallet.

I thought about using Measurement Price Calculator but those plugins only work with measurements like volume and weight, not quantity.

Anyone has experience and a possible solution with this issue? Any help would be highly appreciated. Thanks!


Solution

  • Update: better handle inputed price and handles simple products too.

    You should use variable products and set 3 variations for each product:

    The code:

    // Backend: Add and display a custom field for variable products
    add_action('woocommerce_product_options_general_product_data', 'add_custom_product_general_field');
    function add_custom_product_general_field()
    {
        global $post;
    
        echo '<div class="options_group show_if_simple show_if_variable">';
    
        woocommerce_wp_text_input(array(
            'id'          => '_min_unit_price',
            'label'       => __('Min Unit price', 'woocommerce') ,
            'placeholder' => '',
            'description' => __('Enter the minimum unit price here.', 'woocommerce'),
            'desc_tip'    => 'true',
            'data_type'   => 'price',
        ));
    
        echo '</div>';
    }
        
    // Backend: Save the custom field value for simple and variable products
    add_action( 'woocommerce_admin_process_product_object', 'save_product_custom_price_field', 10, 1 );
    function save_product_custom_price_field( $product ) {
        if ( isset($_POST['_min_unit_price']) ) {
            $product->update_meta_data( '_min_unit_price', wc_clean( wp_unslash( str_replace( ',', '.', $_POST['_min_unit_price'] ) ) ) );
        }
    }
    
    // Frontend simple and variable products display the min price with "From" prefix label
    add_filter( 'woocommerce_get_price_html', 'custom_min_unit_product_price_html', 10, 2 );
    function custom_min_unit_product_price_html( $price, $product ) {
        $min_unit_price = $product->get_meta('_min_unit_price');
    
        if ( $min_unit_price > 0 && in_array( $product->get_type(), array('simple', 'variable') ) ){
            $min_price_html = wc_price( wc_get_price_to_display( $product, array( 'price' => $min_unit_price ) ) );
            return sprintf( __( 'From %s', 'woocommerce' ), $min_price_html );
        }
        return $price;
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and works.

    In backend, for simple and variables products, you will get:

    enter image description here

    enter image description here

    In frontend, for simple and variables products (in shop, archives and single product pages)

    enter image description here