WordPress
HTML
CSS
JavaScript
jQuery
SQL
PHP
WordPress Snippets

Code Snippet

The WooCommerce product grid shows the pricing for each product by default. But that can be changed on a per-product basis to deliver a custom message.

PHP

Place the following code anywhere in your theme’s functions.php document.

In the following, we change the pricing to instead say “Contact Us for Pricing” (see line 9). This may be applicable for certain products where you don’t sell them online, but have them shown as a catalog item whereas the intent is for the user to inquire into the pricing.

On line 7, we set an array that contains the IDs of the specific products that are to receive this special messaging.

				
					// contact us for pricing
function custom_price_message($price) {

	global $post;

	$product_id = $post->ID;
	$my_product_array = array( 511,1029,9999 );
	if (in_array($product_id, $my_product_array)) {
	    $addedtext = 'Contact Us for Pricing';
	    return '<span class="price-note">' . $addedtext . '</span>';
	}

	else {
	    return $price;
	}
}
add_filter( 'woocommerce_get_price_html', 'custom_price_message' );
				
			

Add Text to the Price

In the following, we want to keep the pricing in place, but also want some additional text that may be helpful to the user.

In this case, on line nine, we have text added to the pricing – “Starting at “. And on line 10 we define the positioning of the text to come before the variable $price.

				
					// use the pricing but add some text before the price
function custom_price_message($price) {

	global $post;

	$product_id = $post->ID;
	$my_product_array = array( 9794,9927,9930 );
	if (in_array($product_id, $my_product_array)) {
	    $addedtext = 'Starting at ';
	    return '<span class="price-note">' . $addedtext . $price . '</span>';
	}

	else {
	    return $price;
	}
}
add_filter( 'woocommerce_get_price_html', 'custom_price_message' );
				
			

We could reverse the two variables ($addtext and $price) to have the text come after the price (e.g., $100 for a limited time).

				
					// use the pricing but add some text after the price
function custom_price_message($price) {

	global $post;

	$product_id = $post->ID;
	$my_product_array = array( 9794,9927,9930 );
	if (in_array($product_id, $my_product_array)) {
	    $addedtext = ' for a limited time';
	    return '<span class="price-note">' . $price . $addedtext . '</span>';
	}

	else {
	    return $price;
	}
}
add_filter( 'woocommerce_get_price_html', 'custom_price_message' );
				
			

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.

WordPress
HTML
CSS
JavaScript
jQuery
SQL
PHP

Why 1SMARTchicken?

This site was built and is maintained to benefit my autistic son.
See More →

My Son's Name is Johnny

He was diagnosed as autistic quite late, at age four...
His story

Buy Me a Coffee

Thanks for your support!

Feedback

If you see an error on the page or the code itself is incorrect or incomplete, or just plain wrong, please let us know. We’re always learning. NOTE: we do not sell your information and will not send you spam emails.