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 child 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' );

PHP – 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' );

Note

All modifications to a theme or plugin should be made by creating a child theme and placing the changes there. Changes made to the parent theme will be overwritten the next time it updates.


WordPress Notes:

  • All modifications to a theme or plugin should be made by creating a child theme and placing the changes there; changes made to the parent theme will be overwritten the next time it updates

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.