WooCommerce 3.0 – woocommerce_free_price_html – No Longer Working
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!
Yours was the only recent discussion of the issue I found online, and it helped me immensely. Thank you for posting.