1
votes

In Woocommerce I would like to show a minimum price for a simple product and variable products in front of the catalog.

Based on "Set a min unit displayed price for variable products in Woocommerce" answer code, where I have made light changes to the last function as follow:

// 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( __( '%1$s <span class="amount"><div  style="font-size:11px;color: #666;text-align: center;">Bulk purchasing</div></span>', 'woocommerce' ), $min_price_html );
    }

    return $price;
}

I would like to adapt this solution to simple products as well. Any help is appreciated.

1

1 Answers

0
votes

From product additional price setting field (see the screenshots below), a minimal unit price replace the displayed product price in Woocommerce shop, archives and single product pages.

Based on "Set a min unit displayed price for variable products in Woocommerce" answer code, the following will work for simple and variable products to be used with Woocommerce 3 and above.

The code (completely revisited and enhanced):

// Backend: Add and display a custom field for simple and variable products
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_price_field_to_general_product_data' );
function add_custom_price_field_to_general_product_data() {
    global $product_object;

    echo '<div class="options_group hide_if_external">';

    woocommerce_wp_text_input(array(
        'id'          => '_min_unit_price',
        'label'       => __('Min Unit price', 'woocommerce') . ' (' . get_woocommerce_currency_symbol() . ')',
        'description' => __('Enter the minimum unit price here.', 'woocommerce'),
        'desc_tip'    => 'true',
        'value'       => str_replace('.', ',', $product_object->get_meta('_min_unit_price') ),
        '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 variable products: Display the min price with "From" prefix label
add_filter( 'woocommerce_variable_price_html', 'custom_min_unit_variable_price_html', 10, 2 );
function custom_min_unit_variable_price_html( $price, $product ) {
    if( $min_unit_price = $product->get_meta('_min_unit_price') ){
        $price  = wc_price( wc_get_price_to_display( $product, array( 'price' => $min_unit_price ) ) );
        $price .= ' <span class="prefix" style="font-size:11px;color: #666;text-align: center;">'.__('Bulk purchasing').'</span>';
    }
    return $price;
}

// Frontend simple 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 ) {
    if( $product->is_type('simple') && $min_unit_price = $product->get_meta('_min_unit_price') ){
        $price  = wc_price( wc_get_price_to_display( $product, array( 'price' => $min_unit_price ) ) );
        $price .= ' <span class="prefix" style="font-size:11px;color: #666;text-align: center;">'.__('Bulk purchasing').'</span>';
    }
    return $price;
}

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


In backend, for simple and variables products:

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