Append text to product price in WooCommerce

Categories: CMS, Wordpress

Add the following snippet to append text to product price depending on the price:

add_filter( 'woocommerce_get_price_html', 'prepend_text_to_product_price', 20, 2 );
function prepend_text_to_product_price( $price_html, $product ) {
    // Only on frontend and excluding min/max prices on variable products
    if( is_admin() || $product->is_type('variable') ) 
        return $price_html;

    // Get product price
    $price = (float) $product->get_price(); // Regular price

    if ( $price > 99 ) {
        $price_html = $price_html . "<span><div style=\"font-size: 12px; color: #8bc500;\"><i class=\"fas fa-truck\"></i> Envío GRATIS </div></span>";
	} elseif ( $price <= 49 ) {
		$price_html = $price_html . "<span><div style=\"font-size: 12px; color: #2a2a5b;\"><i class=\"fas fa-truck\"></i>  </div></span>";
	}
    return $price_html;
}

For single product pages only you will replace this line:

if( $price > 99 ){

By this:

if( $price > 99 && is_product() ){

For single product pages and archives pages you will replace this line:

if( $price > 99 ){

By this:

if( $price > 99 && ( is_product() || is_shop() || is_product_category() || is_product_tag() ){

Reference: https://stackoverflow.com/questions/48552886/add-text-before-product-price-if-its-higher-than-a-specific-amount-in-woocommer

«
»