Set a min unit displayed price for variable produc

2019-07-18 03:49发布

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!

1条回答
Fickle 薄情
2楼-- · 2019-07-18 04:15

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

  • per unit
  • per box
  • per pallet

1) In backend: for variable products only, we add a custom setting field, for the "Min unit price" to be displayed.

enter image description here

2) In frontend: for variable products only, we display that custom "Min unit price" in shop, archives and single product pages.

enter image description here

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 hide_if_simple hide_if_external">';

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

    echo '</div>';
}

// Backend: Save the custom field value for variable products
add_action('woocommerce_process_product_meta', 'save_custom_product_general_field');
function save_custom_product_general_field($post_id)
{
    if (isset($_POST['_min_unit_price'])){
        $min_unit_price = sanitize_text_field($_POST['_min_unit_price']);
        // Cleaning the min unit price for float numbers in PHP
        $min_unit_price = str_replace(array(',', ' '), array('.',''), $min_unit_price);
        // Save
        update_post_meta($post_id, '_min_unit_price', $min_unit_price);
    }
}

// Frontend: Display the min price with "From" prefix label for variable products
add_filter( 'woocommerce_variable_price_html', 'custom_min_unit_variable_price_html', 30, 2 );
function custom_min_unit_variable_price_html( $price, $product ) {
    $min_unit_price = get_post_meta( $product->get_id(), '_min_unit_price', true );

    if( $min_unit_price > 0 ){
        $min_price_html = wc_price( wc_get_price_to_display( $product, array( 'price' => $min_unit_price ) ) );
        $price = sprintf( __( 'From %1$s', 'woocommerce' ), $min_price_html );
    }

    return $price;
}

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

查看更多
登录 后发表回答