Search code examples
phpwordpresswoocommerceproduct

Display prefixed price and additional unit price in Woocommerce simple products


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.


Solution

  • 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: