skip to Main Content

In WooCommerce is possible to display only price including tax or excluding tax in products loop and in single product page. Also if one product is on sale, Woocommerce display regular price like crossed out price and sale price. This is by default.

I found many working snippet to customized price for my needs, but some override also what I like to preserve: for example, I loose regular price crossed out if sale price is defined.
I’m looking for a clear code that allow me to:

  1. Show both regular and sale price, for simple and variable products
  2. Show price with tax and without tax with additional custom text (incl. tax; excl. tax)

This for products loop and single product page as well.

Thanks for any suggestions.

2

Answers


  1. Chosen as BEST ANSWER

    I find a solution perfect to my needs. This code does what I asked for in my original request, like in Druvi answer (thanks Druvi).

    Additionals features:

    • add custom text before sale price.

    • if the product has a "noleggio" (rental) tag it shows custom text, if it has a "vendita" (purchase) tag it shows other text.

    • if the product on sale has date sale from/to, it shows the expiration date in products loop and single product page.

    Hope can help someone else.

    Add text for regular price, sale price and for specific tags:

    function ckde_sale_price_html( $price, $product ) {
    $excl_tax = sprintf( __("escl. IVA") );
    if ( $product->is_on_sale() ) {
    
    if ( has_term( 'noleggio', 'product_tag' )) {
    $has_sale_text = array(
    '<ins>' => '<br>Prezzo in offerta: &nbsp;</ins>'
    );
    
    $return_string = str_replace(array_keys( $has_sale_text ), array_values( $has_sale_text ), $price . '<br><span class="woocommerce-price-suffix exc-vat-price">' . $day_excl_tax .'</span>');
    
    } elseif ( has_term( 'vendita', 'product_tag' ) ) {
    $has_sale_text = array(
    '<ins>' => '<br>Prezzo in offerta: &nbsp;</ins>'
    );
    
    $return_string = str_replace(array_keys( $has_sale_text ), array_values( $has_sale_text ), $price . '<br><span class="woocommerce-price-suffix exc-vat-price">' . $excl_tax .'</span>');
    
    } else {
    
    $return_string = $price . '<br><span class="woocommerce-price-suffix exc-vat-price">' . $day_excl_tax .'</span>';
    }
    } else {
    if ( has_term( 'noleggio', 'product_tag' )) {
    $return_string = $price . '<br><span class="woocommerce-price-suffix exc-vat-price">' . $day_excl_tax .'</span>';
    
    } elseif ( has_term( 'vendita', 'product_tag' ) ) {
    
    $return_string = $price . '<br><span class="woocommerce-price-suffix exc-vat-price">' . $excl_tax .'</span>';
    }
    }
    
    return $return_string;
    
    }
    
    add_filter( 'woocommerce_get_price_html', 'ckde_sale_price_html', 100, 2 );
    

    Add custom text with sale dates after sale price in Woocommerce Single product:

    add_filter( 'woocommerce_sale_flash', 'ckde_sale_end_date', 9999, 3 );
         
    function ckde_sale_end_date( $html, $post, $product ) {
    if ( $product->get_date_on_sale_to() ) return $html . ' <span class="onsale-date">(In offerta fino a: ' . gmdate( 'd M Y', $product->get_date_on_sale_to()->getTimestamp() ) . ')</span>';
    
    return $html;
    
    }
    

  2. This code should be added to your theme’s functions.php file

    add_filter('woocommerce_get_price_html', 'display_both_prices', 10, 2);
    function display_both_prices($price, $product) {
        if ($product->is_type('variable')) {
            $min_price = $product->get_variation_price('min', true);
            $max_price = $product->get_variation_price('max', true);
    
            if ($min_price !== $max_price) {
                $price = wc_format_price($min_price) . ' - ' . wc_format_price($max_price);
            } else {
                $price = wc_price($min_price);
            }
        } else {
            if ($product->is_on_sale()) {
                $regular_price = wc_price($product->get_regular_price());
                $sale_price = wc_price($product->get_sale_price());
                $price = '<del>' . $regular_price . '</del> <ins>' . $sale_price . '</ins>';
            } else {
                $price = wc_price($product->get_price());
            }
        }
        return $price;
    }
    

    And Display prices with and without tax along with custom text:

    add_filter('woocommerce_get_price_html', 'display_price_with_tax', 10, 2);
    function display_price_with_tax($price, $product) {
        $price_incl_tax = wc_price($product->get_price_including_tax());
        $price_excl_tax = wc_price($product->get_price_excluding_tax());
        $custom_text = ' (incl. tax: ' . $price_incl_tax . ', excl. tax: ' . $price_excl_tax . ')';
        return $price . $custom_text;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search