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
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
to
Another way would be to hook into the price suffix function:
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):
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.
I have revisited code and simplified it (for single products only):
Code goes in function.php file of your active child theme (or theme). Tested and works.
Related: