skip to Main Content

I am trying display a custom price range for my variable products.
I managed to insert a price range with regular (min and max) prices and sale (min and max) prices.

Here is my code attempt:

add_filter( 'woocommerce_get_price_html', 'custom_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'custom_price_format', 10, 2 );
function custom_price_format( $price, $product ) {

    // Main Price
    $regular_priceMin = $product->is_type('variable') ? $product->get_variation_regular_price( 'min', true ) : $product->get_regular_price();
    $regular_priceMax = $product->is_type('variable') ? $product->get_variation_regular_price( 'max', true ) : $product->get_regular_price();
    
    $sale_priceMin = $product->is_type('variable') ? $product->get_variation_sale_price( 'min', true ) : $product->get_sale_price();
    $sale_priceMax = $product->is_type('variable') ? $product->get_variation_sale_price( 'max', true ) : $product->get_sale_price();

    if ( $regular_priceMin !== $sale_priceMin && $product->is_on_sale()) {
        
        $price = '<p class="teste"><del>' . wc_price($regular_priceMin). 'a' . wc_price($regular_priceMax) . '</del></p> <ins>' . wc_price($sale_priceMin) . '</ins>';
    }
    return $price;
}

However some sale prices have the same values and the formatting is not correct.
It creates 3 lines:

  • One for the minimum price value,
  • another for the letter "a"
  • and another for the maximum price value.

How can I organize this correctly?

The tag <del> tag is not in the same line.

How can solved this? What I am doing wrong?

2

Answers


  1. Would do you not use standart $product->get_price_html() that is available for all type products?

    Login or Signup to reply.
  2. Try the following revisited code that will work everywhere on product loops except on single products for variable products custom price range:

    // For variable product
    add_filter( 'woocommerce_variable_price_html', 'variable_prices_range_formatted', 100, 2 );
    function variable_prices_range_formatted( $price, $product ){
        global $woocommerce_loop;
        // Not on single products
        if ( ( is_product() && isset($woocommerce_loop['name']) && ! empty($woocommerce_loop['name']) ) || ! is_product() )
        {
            // For on sale variable product
            if ( $product->is_on_sale()  )
            {
                $regular_price_min = $product->get_variation_regular_price( 'min', true );
                $regular_price_max = $product->get_variation_regular_price( 'max', true );
    
                $active_price_min = $product->get_variation_price( 'min', true );
                $active_price_max = $product->get_variation_price( 'max', true );
    
                if ( $regular_price_min !== $active_price_min || $regular_price_max !== $active_price_max )
                {
                    // Regular price range (for <del> html tag)
                    if( $regular_price_min !== $regular_price_max ) {
                        $regular_price_del_html = sprintf( '<del>%s a %s</del>', wc_price($regular_price_min), wc_price($regular_price_max) );
                    } else {
                        $regular_price_del_html = sprintf( '<del>%s</del>', wc_price($regular_price_min) );
                    }
    
                    // Active price range (for <ins> html tag)
                    if( $active_price_min !== $active_price_max ) {
                        $active_price_ins_html = sprintf( '<ins>%s a %s</ins>', wc_price($active_price_min), wc_price($active_price_max) );
                    } else {
                        $active_price_ins_html = sprintf( '<ins>%s</ins>', wc_price($active_price_min) );
                    }
    
                    $price = sprintf( '<p class="teste">%s %s</p>', $regular_price_del_html, $active_price_ins_html );
                }
            }
        }
        return $price;
    }
    

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

    A variable on sale product price:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search