Posted on 2 Comments

Display the original and discounted prices

If you are using Woocommerce Groups Pricing, and you need to display the original and the discounted prices, you can add this code in your theme functions.php file.

If you are using Woocommerce 2.5.x


add_filter( 'woocommerce_get_price_html', 'your_own_price_html', 100, 2 );
function your_own_price_html( $price, $product ){
  $result = $price;

  if ( get_option( "wgp-baseprice", "regular" ) == "sale" ) {
    $price_key = '_price';
  } else {
    $price_key = '_regular_price';
  }

  if ( $product->product_type == 'variable' ) {

    $children = $product->get_children(true);

    $original_prices = array();
    $commission = 0;
    foreach ( $children as $child ) {
      $original_prices[] = get_post_meta( $child, $price_key, true );
      if ( $commission == 0 ) {
        $commission = WGP_Variations_Admin::get_commission( $product, $child );
      }
    }

    if ( $commission > 0 ) {
      $min_price = min( $original_prices );
      $max_price = max( $original_prices );
      $original_price = $min_price !== $max_price ? sprintf( _x( '%1$s–%2$s', 'Price range: from-to', 'woocommerce' ), wc_price( $min_price ), wc_price( $max_price ) ) : wc_price( $min_price );
    }
  } else {
    $commission = WooGroupPricing::get_commission( $product );
    if ( $commission ) {
      $original_price = woocommerce_price(get_post_meta( $product->id, $price_key, true ));
    }
  }

  if ( $commission ) {
    $result =  'Was:' . $original_price . " Now:" . $price;
  }
  return $result;
}

If you are using Woocommerce < 2.5.0


add_filter( 'woocommerce_get_price_html', 'your_own_price_html', 100, 2 );
function your_own_price_html( $price, $product ){
  $result = $price;
  if ( $product->is_type( 'variation' ) ) {
     $commission = WGP_Variations_Admin::get_commission( $product, $product->variation_id );
  } else {
     $commission = WooGroupPricing::get_commission( $product );
  }
  if ( $commission ) {
     $result =  'Was:' . woocommerce_price(get_post_meta( $product->id, '_price', true )) . " Now:" . $price;
  }
  return $result;
}

2 thoughts on “Display the original and discounted prices

  1. Hi
    I am using this plugin and was wondering if I could suggest 2 features? Or you could help me get it done in the codes?

    I have set the plugin up to Show: “Was and Now” prices but the group “Registered” users don’t have a special price? So the same price shows twice?
    Most get confused.

    Can this be fixed so a group with NO discount – Don’t get to see that there is a special price – JUST THE NORMAL PRICE 🙂 – Like before

    Also maybe fields to enter the text / translation I want instead of “Was” and “Now”?

    Best regards

    Peter

    1. Hi Peter,
      You can use the ‘wwo_group_pricing_display_original_prices’ filter, ex.
      add_filter( 'wwo_group_pricing_display_original_prices', 'eggemplo_wwo_group_pricing_display_original_prices', 10, 3 );
      function eggemplo_wwo_group_pricing_display_original_prices ( $displayed, $original, $discounted ) {
      $result = $displayed;
      if ( $original == $discounted ) {
      $result = $original;
      } else {
      $result = "Normal price: " . $original . " Discounted price: " . $discounted;
      }
      return $result;
      }

      But display the same price twice is not a good idea. I will change it in the next update, thanks for the suggestion.
      Kind Regards,
      Antonio B.

Leave a Reply

Your email address will not be published. Required fields are marked *