WooCommerce 3.0 – woocommerce_free_price_html – No Longer Working


About The Author
Cabe Nolan is the founder of WP Cover where he shares his insight into WordPress, development, & entrepreneurship. Outside of WPCover, Cabe continues to run a successful WordPress development firm, Bold City Design as well as a few high profile websites, Arrivala, Two Way Resume, Dock Skipper, and a successful outdoors brand, DolfinPack.







WooCommerce 3.0 – woocommerce_free_price_html – No Longer Working

WooCommerce 3.0 has arrived!  While many are jumping for joy at new features & improved code, there’s some of us that have had to devote some time to putting out fires.  WooCommerce 3.0 depreciated, added, and modified some important filters & API requests that were working in 2.0 causing client bugs.  One issue we’ve recently come across is with the following filters:

  • woocommerce_free_price_html
  • woocommerce_variable_free_price_html
  • woocommerce_variation_free_price_html

 
Our previous function as shown below, was utilized with Advanced Custom Fields to manipulate the text that was displayed when the product price was set to $0.00:

 

add_filter( 'woocommerce_variable_free_price_html','wpcover_free_price_text' );
add_filter( 'woocommerce_free_price_html','wpcover_free_price_text' );
add_filter( 'woocommerce_variation_free_price_html','wpcover_free_price_text' );

function wpcover_free_price_text( $price ) {
	global $post;
	if(get_field('wpc_price_headline', $post->ID)) {
		return get_field('wpc_price_headline', $post->ID);
	} else {
		return '$10 / Per Team';
	}
}

As you can see from the above snippet, the code would check if the price of a simple or variable product had a regular price of $0.00. If so, it checked to see if the product had a valve in the Advanced Custom Field of wpc_price_headline, if not it returned some standard text.

What Happened & The Fix

Information on why exactly this filter stop working was hard to come by. From what I did find within the WooCommerce site, it was depreciated due to localization issues. However, the fix was pretty straight-forward and worked nicely, here is what I did:

add_filter( 'woocommerce_get_price_html','wpcover_free_price_text' );

function wpcover_free_price_text( $price, $product ) {
	global $post;
	global $product;
	$pprice = $product->get_price();
	if($pprice == '0.00') {
		if(get_field('wpc_price_headline', $post->ID)) {
			return get_field('wpc_price_headline', $post->ID);
		} else {
			return '$10 / Per Team';
		}
	}
}

There’s your fix!


Comments:

  1. Hajii Gomeshi says:

    Yours was the only recent discussion of the issue I found online, and it helped me immensely. Thank you for posting.

Share Your Thoughts

Leave a Reply

Your email address will not be published.


Related Stuff You Might Like







WordPress News, Tips, & Code Snippets

Join the WP Cover mailing list and get wordpress news, tips, code snippets, security warnings, and more delivered right to your inbox.  We won't flood your inbox, newsletters typically go out every 1-2 weeks unless it involves an important security release.

You have Successfully Subscribed!