TransWikia.com

Display prefixed price and additional unit price in Woocommerce simple products

Stack Overflow Asked by Schalk Joubert on December 11, 2021

Using Woocommerce I sell wine per box with 6 bottles in a box.
So naturally I enter a price per box when setting up my product.
I do however want the user to see the price per bottle as well.

$120 / box

$20 / bottle

I tried using Display on shop pages the unit price and the wholesale price on product pages answer code. It does exactly what I want, except, it removes the original ( per box ) price.

The edited snippet below display both the original $price and the unit $price as well as the group/unit indicator (for simple products):

add_filter( 'woocommerce_get_price_html', 'unit_product_price_on_archives', 10, 2 );
function unit_product_price_on_archives( $price, $product ) {
    if ( is_product() || is_product_category() || is_product_tag() ) {
        $unit_divider = 6;
        $group_suffix = ' '. __('(per box)', 'woocommerce');
        $unit_suffix = ' '. __('(per bottle)', 'woocommerce');

        if( $product->is_on_sale() )
        {
            $regular_price_unit = $product->get_regular_price() / $unit_divider;
            $regular_price_unit = wc_get_price_to_display( $product, array( 'price' => $regular_price_unit ) );

            $regular_price_group = $product->get_regular_price();
            $regular_price_group = wc_get_price_to_display( $product, array( 'price' => $regular_price_group ) );
          
            $group_price_sale = $product->get_sale_price();
            $group_price_sale = wc_get_price_to_display( $product, array( 'price' => $group_price_sale ) );

            $group_price_sale = wc_format_sale_price( $regular_price_group, $group_price_sale ) . $group_suffix;


            $unit_price_sale = $product->get_sale_price() / $unit_divider;
            $unit_price_sale = wc_get_price_to_display( $product, array( 'price' => $unit_price_sale ) );
            $unit_price_sale = wc_format_sale_price( $regular_price_unit, $unit_price_sale ) . $unit_suffix;
         
            $price = $group_price_sale . '<br>' . $unit_price_sale;
        }
        else
        {
            $group_price = $price;
            $group_price = $group_price . $group_suffix;
            $unit_price = $product->get_price() / $unit_divider;
            $unit_price = wc_get_price_to_display( $product, array( 'price' => $unit_price ) );
            $unit_price = $price = wc_price($unit_price) . $unit_suffix;
            $price = $group_price . '<br>' . $unit_price;
        }
    }
    return $price;
}

I did however only cater for standard product with regular or sale price.

I did not extend this to grouped or variable products.

2 Answers

I have revisited code and simplified it (for single products only):

add_filter( 'woocommerce_get_price_html', 'custom_product_price_html', 10, 2 );
function custom_product_price_html( $price, $product ) {
    // For simple products only
    if ( $product->is_type('simple') ) {

        $unit_divider = 6;
        $suffix_box   = ' '. __('(per box)', 'woocommerce');
        $suffix_unit  = ' '. __('(per bottle)', 'woocommerce');

        $active_price_unit  = wc_get_price_to_display( $product, array( 'price' => ( $product->get_price() / $unit_divider ) ) );

        if($product->is_on_sale() )
        {
            $regular_price_unit   = wc_get_price_to_display( $product, array( 'price' => ( $product->get_regular_price() / $unit_divider ) ) );
            $formatted_price_unit = wc_format_sale_price( $regular_price_unit, $active_price_unit );

            $price .= $suffix_box . '<br><span class="unit-price">' . $formatted_price_unit . $suffix_unit . '</span>';
        }
        else
        {
            $price .= $suffix_box . '<br><span class="unit-price">' . wc_price($active_price_unit) . $suffix_unit . '</span>';
        }
    }
    return $price;
}

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


Related:

Answered by LoicTheAztec on December 11, 2021

There are several ways to achieve this. The one you posted above might work once you append the new price to the original string by changing these two lines

$price = $group_price_sale . '<br>' . $unit_price_sale;
$price = $group_price . '<br>' . $unit_price;

to

$price .= $group_price_sale . '<br>' . $unit_price_sale;
$price .= $group_price . '<br>' . $unit_price;

Another way would be to hook into the price suffix function:

function so1664798_add_bottle_price_suffix( $html, $product, $price, $qty ){
  $bottleprice = $price / 6;
  $html .= ' ('.$bottleprice .' per bottle)';
  return $html;
}
add_filter( 'woocommerce_get_price_suffix', 'so1664798_add_bottle_price_suffix', 99, 4 );

Or, if you want the price per bottle to show for example on the single product page, you can hook in there with this code (I use it for price per litre information):

function so1664798_add_bottle_price() {
    global $product;
    $price = $product->get_price();
    $price_per_bottle = $price / 6;
    echo '<span class="price-unit smaller">'.number_format((float)$price_per_bottle, 2, wc_get_price_decimal_separator(), '').get_woocommerce_currency_symbol().' / '.__('Bottle','txtdomain').',&nbsp;</span>';
}
add_action('woocommerce_single_product_summary','so1664798_add_bottle_price_summary',10);

The last option will be the safest one if you want to avoid your bottle price to be displayed at several other places (checkout, cart, ...) but only want it at the product page.

Answered by user127091 on December 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP