Skip to content

Instantly share code, notes, and snippets.

@kloon
Last active October 16, 2022 16:46
  • Star 16 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kloon/8981075 to your computer and use it in GitHub Desktop.
WooCommerce 2.1 variation price, revert to 2.0 format
/**
* Use WC 2.0 variable price format, now include sale price strikeout
*
* @param string $price
* @param object $product
* @return string
*/
function wc_wc20_variation_price_format( $price, $product ) {
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
if ( $price !== $saleprice ) {
$price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
}
return $price;
}
add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );
@stevenTieulie
Copy link

Works great, but still show From: on items with multiple variations but same price. (IE clothing sizes) The edited code below fixes that:

get_variation_price( 'min', true ); $max_price = $product->get_variation_price( 'max', true ); if ($min_price != $max_price){ $price = sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $min_price ) ); return $price; } else { $price = sprintf( __( '%1$s', 'woocommerce' ), wc_price( $min_price ) ); return $price; } } ?>

@kloon
Copy link
Author

kloon commented Feb 19, 2014

Thanks for the update @stevenTieulle, I updated the snippet to reflect your changes :)

Copy link

ghost commented Feb 20, 2014

thanks guys. any idea why this might not work with product categories? in my case any of the codes does change the display format. thanks

@kloon
Copy link
Author

kloon commented Feb 20, 2014

Just updated the code again to make it exactly like 2.0, so should support sale prices now as well.

Copy link

ghost commented Feb 20, 2014

yeah. i tried everything - i wonder why i doesnt work in my case...

@josedebaires
Copy link

Hi, where's functions.php? in woocomerce? can not find it
Thanks for your help

@samuelaguilera
Copy link

@josedebaires functions.php is in your theme, not in WooCommerce plugin.

Copy link

ghost commented Feb 27, 2014

is there a solution for the group product too? i would have it back to the WC 2.0 too. thanks

Copy link

ghost commented Feb 27, 2014

@alexkappel
Copy link

If you want this to reflect on Composite Products you can edit this row in class-wc-product-bto.php

change:
if ( ! $woocommerce_bto_helpers->is_wc_21() )
return $this->get_price_html_20();

to:
if ( $woocommerce_bto_helpers->is_wc_21() )
return $this->get_price_html_20();

I don't know how to make it in my child theme functions.php but if anyone got more skills than me, please update me.

@abalu
Copy link

abalu commented Mar 18, 2014

Hello, i still have the price ranges for composite Porducts - can someone help me out?

@funkshen
Copy link

Hi, I've added this code to my function.php file but after I added this code whenever I clear my cache or update/post a new product, page, post, etc, wordpress/woocommerce will update or complete the action I am trying to do but it then always takes me to a blank white page not to the page it should. eg. I click publish and public a new product but I am taken to a blank white screen, not back to the updated product page of my dashboard.

Is anyone else having this issue? thanks.

@ayottepl
Copy link

ayottepl commented Apr 8, 2014

i'd lost "from" with the update... so many thanks! With this, it's back :D

@sam-webdev
Copy link

When I add this to the functions.php it works for the individual product pages, but when I try to view the store/shop page it states:

Warning: Cannot modify header information - headers already sent by (output started at XXXX/functions.php:262) in XXX/wp-includes/pluggable.php on line 1121

I've already checked the wp-config file and it does not have additional spaces. I have no plugins enabled except for woocommerce. The pluggable.php file is missing the " >? " (without quotes) When I add the " >? " to pluggable.php, it fixes the store/shop page, but my checkout page still shows the aforementioned error.

Any idea what causes this and how to fix it?

Thanks!

@sam-webdev
Copy link

Never mind, I fixed it.

I removed this code's " <?php " and the " >? " (without quotes) before adding it to the functions.php file. It now works.

@YogY11
Copy link

YogY11 commented Jul 28, 2014

Works great, thx for that!
One minor bug: the suffix you can set in WooCommerce/Settings/Tax/Price display suffix is no longer displayed for variable products.

I unsuccessfully tried to implement the below from the woo themes docs:
http://docs.woothemes.com/wc-apidocs/source-class-WC_Product.html

 /**
  * Get the suffix to display after prices > 0
  * @return string
  */
 public function get_price_suffix() {
     $price_display_suffix  = get_option( 'woocommerce_price_display_suffix' );

     if ( $price_display_suffix ) {
         $price_display_suffix = ' <small class="woocommerce-price-suffix">' . $price_display_suffix . '</small>';

         $find = array(
             '{price_including_tax}',
             '{price_excluding_tax}'
         );

         $replace = array(
             wc_price( $this->get_price_including_tax() ),
             wc_price( $this->get_price_excluding_tax() )
         );

         $price_display_suffix = str_replace( $find, $replace, $price_display_suffix );
     }

     return apply_filters( 'woocommerce_get_price_suffix', $price_display_suffix, $this );
}

Any thoughts?

@bavington
Copy link

Works a treat, thanks for sharing this!

@alemarengo
Copy link

Hi to you all!
I'm searching for a solution like the one YogY11 wrote above.
How can I add a suffix text like "VAT incl." after the lowest price?
Many thanks in advance!

_EDIT_
I simply added . 'some text'; after return $price

@groggy96
Copy link

groggy96 commented Sep 7, 2014

Is it possible to have a hook that would completely override the pricing and use text instead. I know its a strange request but Its something I need to do and I can't find an answer anywhere.

@eddallen
Copy link

eddallen commented Sep 9, 2014

Thanks for this! SO useful! - Has anyone worked out a way of setting it up with composite products as alexkappel mentioned above but keeping it safely stored in the child theme?

@groggy96 This does something similar to what you're after, it might take some editing to get it exactly right though?

@karidamato
Copy link

This worked great for me until I updated WooCommerce to 4.0 and my theme (The Retailer). Now, it causes my shopping cart to go wonky—instead of "Choose an option" the menu reads the first price option and the number in the middle of my " - " and " + " "Add to Cart" disappears! Any ideas on how to revert the price to the "FROM" format without the other issues?

@amouratoglou
Copy link

Hi people,

1st of all, Thanks for sharing...

It was working perfectly, but somehow it's not working anymore , but breaking the site!

I have a problem with the snippet, getting Server internal error 500 after implementing in functions.php

My Ajax cart is working and then the error 500 appears and the white screen...

seems to be in conflict with this YITH Ajax plugin...

also I got this error message: anybody knows what can be wrong??

PHP Warning: Cannot modify header information - headers already sent by (output started at /home/organicb/public_html/wp-content/themes/flatsome-child/functions.php:20) in /home/organicb/public_html/wp-includes/pluggable.php on line 1121

@kelerby
Copy link

kelerby commented Nov 11, 2014

The recent update for this is still showing the "FROM:" pricing. Does anyone have any suggestion or workarounds for this issue?

@nr-s
Copy link

nr-s commented Feb 16, 2015

Many thanks..

@Al5ki
Copy link

Al5ki commented Mar 16, 2015

Hi guys,

I would really appreciate some help on this issue, the above code works great, so thanks for everyone who collaborated on it.

However,I need to show the price including tax, I have tried reverse engineering other codes and merging to no avail.

Can anyone help?

Also, I would ideally like to show the term From: on every product, not just the ones with different prices, if that's possible somehow?

Many thanks in advance,
Al5ki

@mario312
Copy link

Thanks. this worked like a charm.

@swohlert
Copy link

Have been using this for quite a while now, but I'm in the progress of adding a language to my webshop. Does anyone know a fix for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment