MyListing Club

Change $0 to Free

Building better MyListing websites. One code snippet at a time.

Overview

The Change $0 to Free code snippet allows for greater flexibility within a MyListing website.

Implementation

  1. Grab the code snippet.
  2. Follow the provided instructions.
  3. Save your changes.
				
					//// INSTRUCTIONS: 
//// Copy and paste the code snippet below into the child theme's functions.php file.
//// You can edit the functions.php file from your WordPress dashboard (Appearance > Theme Editor) or via FTP.

//// NOTES:
//// If you prefer to show the word "Free" instead of $0, for your pricing tables for example, this snippet will do that.
//// Now that you have this snippet, you can change $0 to "Free", "No Cost", or whatever wording you would like to use.

--------------------------- CODE SNIPPET IS BELOW THIS LINE --------------------------- 

// CHANGE $0 TO FREE
function my_wc_custom_get_price_html( $price, $product ) {

    if ( $product->get_price() == 0 ) {
        if ( $product->is_on_sale() && $product->get_regular_price() ) {
            $regular_price = wc_get_price_to_display( $product, array( 'qty' => 1, 'price' => $product->get_regular_price() ) );
            $price = wc_format_price_range( $regular_price, __( 'Free!', 'woocommerce' ) );
        } else {
            $price = '<span class="amount">' . __( 'FREE', 'woocommerce' ) . '</span>';
        }
    }
    return $price;
}

add_filter( 'woocommerce_get_price_html', 'my_wc_custom_get_price_html', 10, 2 );