Woocommerce add text to product price

  • Categoria dell'articolo:Tech / wordpress
Al momento stai visualizzando Woocommerce add text to product price

To add a text after the product price for a single product on WooCommerce, you can use the following code snippet:

add_filter( 'woocommerce_get_price_html', 'custom_price_message', 100, 2 );
function custom_price_message( $price, $product ){
    if( is_product() && $product->get_id() === YOUR_PRODUCT_ID ) {
        $price .= '<p>Your custom message here</p>';
    }
    return $price;
}


You need to replace YOUR_PRODUCT_ID with the ID of the specific product you want the custom message to appear for. You can get the product ID by going to the product’s Edit page on the WordPress dashboard, it’s listed on the top right corner.

You can also use the same approach to target multiple products by adding an array of IDs and using the in_array() function to check if the current product’s ID is in the array of IDs you want to target.

add_filter( 'woocommerce_get_price_html', 'custom_price_message', 100, 2 );
function custom_price_message( $price, $product ){
    $product_ids = array( YOUR_PRODUCT_ID1, YOUR_PRODUCT_ID2);
    if( is_product() && in_array( $product->get_id(), $product_ids ) ) {
        $price .= '<p>Your custom message here</p>';
    }
    return $price;
}

Please note that the above code snippet is an example and you should test it in your environment to ensure it works as expected.