Andy
Andy

Reputation: 57

Woocommerce: Show Price Suffix only on Product Page

I added the following code to "My Custom Functions PHP Inserter" plugin to show a custom price suffix in my woocommerce shop.

add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );

function custom_price_suffix( $price, $product ){
    $price = $price . ' inkl. MwSt. und <a href="http://www.link.to/shippinginfo">zzgl. Versandkosten</a>'; 
    return apply_filters( 'woocommerce_get_price', $price );
}

It works, but it also shows the price on the product category page and shop page. How can I avoid that?

I tried this css code but doesn't work:

.woocommerce-price-suffix {
      display: none;
    }
    .single-product .product-price-wrap .woocommerce-price-suffix {
      display: block !important;
    }

The following solution might work, but I don't want to overwrite a php file in my theme: hide Woocommerce price suffix on category page

I also want to change the font size of "inkl. MwSt. und zzgl. Versandkosten", but I don't know how to do this in php. Tried this css but doesn't do anything:

.custom_price_suffix {
  font-size: small;
}

Upvotes: 0

Views: 1812

Answers (1)

Frits
Frits

Reputation: 7614

You can use the built-in is_singular() function to check if you are on a singular product page

add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ) {
    if(is_singular('product')) {
        $price = $price . ' inkl. MwSt. und <a href="http://www.link.to/shippinginfo">zzgl. Versandkosten</a>'; 
    }
    return apply_filters( 'woocommerce_get_price', $price );
}

If you want to change the size - you can simply wrap your text inside a span and add CSS to it - i.e. change your $price variable to this:

$price = $price . ' <span class="make-me-small">inkl. MwSt. und <a href="http://www.link.to/shippinginfo">zzgl. Versandkosten</a></span>'; 

and then add the following to your CSS:

.make-me-small {
    font-size: 0.8rem;
}

Edit: Adding location specific adjustment

As per your comment regarding adjustment of the link text based on the base language of the website, there are two ways to achieve this:

The first route (and probably the best/accepted way) is to use the built in string translation management functions. You can do this by using the __() function and wrapping it around your text like this:

$price = $price . __(' inkl. MwSt. und <a href="http://www.link.to/shippinginfo">zzgl. Versandkosten</a>', 'my-text-domain');

Once you've done that, you will see that your text will now show up under your String Translations tab under the WPML menu item on your dashboard - from there you can manually assign it a new string(s) based on the different site languages.

The second route is to manually add the adjustment inside the function using a switch (or if statement) statement:

add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ) {
    if(is_singular('product')) {
        $language_code = apply_filters( 'wpml_current_language', NULL );
        switch ($language_code) {
            case 'de':
                $suffix = ' inkl. MwSt. und <a href="http://www.link.to/shippinginfo">zzgl. Versandkosten</a>';
                break;

            case 'it':
                $suffix = ' IVA e spese di <a href="http://www.link.to/shippinginfo">spedizione incluse</a>';
                break;

            default:
                $suffix = ' incl. VAT and <a href="http://www.link.to/shippinginfo">shipping costs</a>';
                break;
        }
        $price = $price . $suffix; 
    }
    return apply_filters( 'woocommerce_get_price', $price );
}

Upvotes: 2

Related Questions