Search code examples
phpwordpresswoocommerceproduct

Display on shop pages the unit price and the wholesale price on product pages


In Woocommerce, I have set my products with a wholesale price. I'm trying to display the unit price of the products only in shop pages, which should be the wholesale price divided by 6.

Any clue?


Solution

  • You will keep your product wholesale price are they are (in single product pages) and on shop page the following function will display the unit price (the wholesale price divided by 6) prepended optionally with a text like "(unit)":

    add_filter( 'woocommerce_get_price_html', 'unit_product_price_on_archives', 10, 2 );
    function unit_product_price_on_archives( $price, $product ) {
        if ( is_shop() || is_product_category() || is_product_tag() ) {
            $unit_divider = 6;
            $suffix = ' '. __('(unit)', 'woocommerce');
    
            if( $product->is_type('variable') )
            {
                $unit_price_min  = $product->get_variation_price('min') / $unit_divider;
                $unit_price_fmin = wc_get_price_to_display( $product, array( 'price' => $unit_price_min ) );
                $unit_price_max  = $product->get_variation_price('max') / $unit_divider;
                $unit_price_fmax = wc_get_price_to_display( $product, array( 'price' => $unit_price_max ) );
    
                if( $unit_price_min != $unit_price_max )
                    $price = wc_format_price_range( $unit_price_fmin, $unit_price_fmax ) . $suffix;
                else
                    $price = wc_price($unit_price_fmin) . $suffix;
                if( $unit_price_max == 0 )
                    $price = '';
            }
            elseif( $product->is_type('grouped') )
            {
                $tax_mode     = get_option( 'woocommerce_tax_display_shop' );
                $children     = array_map( 'wc_get_product', $product->get_children() );
                $child_prices = array();
                foreach ( $children as $child ){
                    if ( '' !== $child->get_price() ) {
                        $child_prices[] = 'incl' === $tax_mode ? wc_get_price_including_tax( $child ) : wc_get_price_excluding_tax( $child );
                    }
                }
                if ( ! empty( $child_prices ) ) {
                    $min_price = min( $child_prices );
                    $max_price = max( $child_prices );
                } else {
                    $min_price = $max_price = '';
                }
                if ( '' !== $min_price ) {
                    $min_price /= $unit_divider;
                    $max_price /= $unit_divider;
                    $price = $min_price !== $max_price ? wc_format_price_range( $min_price, $max_price ) : wc_price( $min_price );
                } else {
                    $price = '';
                }
            }
            elseif( $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 ) );
                $sale_price_unit = $product->get_sale_price() / $unit_divider;
                $sale_price_unit = wc_get_price_to_display( $product, array( 'price' => $sale_price_unit ) );
    
                $price = wc_format_sale_price( $regular_price_unit, $sale_price_unit ) . $suffix;
            }
            else
            {
                $unit_price = $product->get_price() / $unit_divider;
                $unit_price = wc_get_price_to_display( $product, array( 'price' => $unit_price ) );
    
                $price = wc_price($unit_price) . $suffix;
            }
        }
        return $price;
    }
    

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

    Tested and works.