Add text before product price if it's higher than a specific amount in Woocommerce

佐手、 提交于 2020-01-13 20:43:23

问题


In Woocommerce I'm trying to add text before price if is higher than 59€

I tried the following code (and others one) but they didn't work:

    add_filter( 'woocommerce_get_price_html', 'custom_price_message' );
function custom_price_message( $price ) {
if ($price>='59'){
$new_price = $price .__('(GRATIS!)');
}
return $new_price;
}

How can I do to add (GRATIS!) text before product price if it's higher than 59?


回答1:


Updated: You should try the following revisited function code:

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 > 15 )
        $price_html = '<span>'.__('(GRATIS)', 'woocommerce' ).'</span> '.$price_html;

    return $price_html;
}

Code goes in function.php file of the active child theme (or active theme).

Tested and works.


For single product pages only you will replace this line:

if( $price > 15 ){

By this:

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

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

if( $price > 15 ){

By this:

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


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!