Sometimes you need a short line right under the WooCommerce product price: “Ships within 24 hours”, “Incl. VAT”, “Price per kilo”. This snippet adds text after the price in WooCommerce, on the product page and in the shop lists, for every product or only for the products you choose.
The snippet
<?php
/**
* Add a short text after the WooCommerce product price.
*/
// ===== Config =====
function dpc_price_note_config() {
$config = array(
// The text under the price. Plain text; it is escaped.
'text' => __( 'Ships within 24 hours', 'dpc' ),
// Only for these product IDs, e.g. array( 12, 34 ). Empty array() = every product.
'product_ids' => array(),
// true = only on the product's own page, not in the shop, categories or related products.
'single_only' => false,
);
// ===== End of config =====
return (array) apply_filters( 'dpc_price_note_config', $config );
}
add_filter( 'woocommerce_get_price_html', 'dpc_price_note', 10, 2 );
function dpc_price_note( $price, $product ) {
// Not in wp-admin product lists, not for single variations (their price shows under the main price).
if ( '' === $price || ( is_admin() && ! wp_doing_ajax() ) || ! $product instanceof WC_Product || $product->is_type( 'variation' ) ) {
return $price;
}
$config = dpc_price_note_config();
$text = trim( (string) ( $config['text'] ?? '' ) );
$ids = array_map( 'absint', (array) ( $config['product_ids'] ?? array() ) );
if ( '' === $text || ( $ids && ! in_array( $product->get_id(), $ids, true ) ) ) {
return $price;
}
if ( ! empty( $config['single_only'] ) && ! ( is_singular( 'product' ) && get_queried_object_id() === $product->get_id() ) ) {
return $price;
}
return $price . ' <span class="dpc-price-note" style="display:block;font-size:0.8em;">' . esc_html( $text ) . '</span>';
}
Add it with Scripts Organizer
In Scripts Organizer go to Code Blocks → Add New and give the block a name. Set Trigger location to Everywhere, tick PHP under Script location, paste the code into the PHP editor, edit the config and publish. Leave Action/Hook empty.
The code only adds a filter, so it can load before WooCommerce. Prices are also rendered in AJAX and REST requests and in product blocks, which Everywhere covers.
The same code also works unchanged in a small plugin or an mu-plugin.
How it works
woocommerce_get_price_htmlfilters the price HTML wherever WooCommerce prints it withget_price_html(): the single product page, shop and category pages, related products, product shortcodes and the Product Price block.- The filter passes the product as its second argument, so the ID check uses that product. The older version read the global
$post, which on a product page is the main product, so related products got the text of the page’s product. - The note is a
<span>withdisplay:block, because the price sits inside a<p>, and a<div>there is invalid HTML that browsers break apart. The text goes throughesc_html(). - Prices in wp-admin (the Products list) and the separate price of a selected variation are left as they are, so the note appears once per product.
single_onlycompares the product with the page being viewed, so related products and upsells on a product page do not get the note.
Customise it
- Your text: change
text. It is translatable with thedpctext domain. - Only some products:
'product_ids' => array( 12, 34 ). The ID is shown when you hover a product in Products. - Only on the product page:
'single_only' => true. - Style it in your theme’s stylesheet with
.dpc-price-noteand remove the inlinestyleattribute. - A different note per product, or links in the note: filter
dpc_price_note_config, or replaceesc_html()withwp_kses_post()if you need markup you trust.
Gotchas
- Cart, checkout and mini cart show line prices with other functions, so the note does not appear there. That is usually what you want.
- Products without a price (empty price HTML) get no note.
- Full-page caches keep the old price HTML until they are cleared, so purge the cache after changing the text.
The older version of this post had no <?php tag, offered two versions with the same function name (pasting both is a fatal error), printed a <div> inside the price paragraph and also changed prices in wp-admin.
In the Scripts Organizer library Import it with one click from Scripts Organizer.