Dynamic Content Bug for On-Sale Pricing with Variable Products

I’m trying to display the original price as crossed out when an item is on sale. It works find if it is a Simple Product, but if it is a Variable Product, both the crossed-out price and the sale price are the same. This issue was reported by a previous user, but I was unable to implement the fix they suggested as the instructions weren’t very clear. I’ve linked that discussion below.

Here is a screenshot of what it looks like with a Simple Product vs a Variable Product. The simple product is circled in blue pen. You’ll notice the price difference shows up with that one, vs the other two highlighted products that are variable.

I’m fine with a workaround like the one reference in the above chat, but I would need advice on how to implement it. Otherwise, has this bug been fixed and I just haven’t seen the solution anywhere?

1 Like

Here is the code I added to my functions.php file in my child theme. Please note that this price was formatted for Norwegian currency - so you may need to make changes in the “array” part where formatting is addressed:

// Fix problem where regular price shows as sale price
// Use shortcode: [product_regular_price] 
// instead of {{dc:woocommerce:product_regular_price}}
function my_shortcode_product_regular_price() {
    $html = '';

    global $product;

    $price = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_regular_price() ) );

    $args = array(
            'ex_tax_label'       => false,
            'decimal_separator'  => ',',
            'thousand_separator' => '',
            'decimals'           => 0,
            'price_format'       => '%2$s ',
        );

    $html = wc_price( $price, $args );

    return $html;
 }
 add_shortcode( 'product_regular_price', 'my_shortcode_product_regular_price' );
1 Like

I think the issue you are coming across with the other examples is that those examples only work for variable products and not also Simple Products. I appreciate the examples though as it demonstrates what we’ll do to fix it. We’ll change up how {{dc:woocomerce:product_regular_price}} works in 6.4 to better support Variable products so you don’t need any of these too.

<?php

// Fix problem where regular price shows as sale price
// Use shortcode: [product_regular_price]
// instead of {{dc:woocommerce:product_regular_price}}
function my_shortcode_product_regular_price() {
    $html = '';

    global $product;

    // Charlie update
    // If has method get_variation_regular_price use that
    // if not use get_regular_price
    $price = method_exists($product, 'get_variation_regular_price')
      ? $product->get_variation_regular_price()
      : $product->get_regular_price();

    $price = wc_get_price_to_display( $product, array( 'price' => $price ) );

    $args = array(
            'ex_tax_label'       => false,
            'decimal_separator'  => ',',
            'thousand_separator' => '',
            'decimals'           => 0,
            'price_format'       => '%2$s&nbsp;',
        );

    $html = wc_price( $price, $args );

    return $html;
 }
 add_shortcode( 'product_regular_price', 'my_shortcode_product_regular_price' );
1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.