Price Label Field per Product ("per m²", "per month")
Adds a field to each product for a short label shown after its price, such as "per m²", "per person" or "per night".
Opens WPIDE on
Code
add_action( 'woocommerce_product_options_pricing', function () {
global $product_object;
woocommerce_wp_text_input( array(
'id' => '_wpide_price_label',
'label' => 'Price label',
'placeholder' => 'per m²',
'value' => $product_object instanceof WC_Product ? $product_object->get_meta( '_wpide_price_label' ) : '',
) );
} );
add_action( 'woocommerce_admin_process_product_object', function ( $product ) {
// phpcs:ignore WordPress.Security.NonceVerification -- WooCommerce verifies the product save nonce.
if ( isset( $_POST['_wpide_price_label'] ) ) {
$product->update_meta_data( '_wpide_price_label', sanitize_text_field( wp_unslash( $_POST['_wpide_price_label'] ) ) );
}
} );
add_filter( 'woocommerce_get_price_html', function ( $html, $product ) {
if ( '' === $html ) {
return $html;
}
$id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id();
$label = get_post_meta( $id, '_wpide_price_label', true );
return '' === $label ? $html : $html . ' <span class="wpide-price-label">' . esc_html( $label ) . '</span>';
}, 30, 2 );
Notes
The label appears wherever the price is shown, and variations use their parent product's label.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.