如果价格高于Woocommerce中的特定数量,请在产品价格之前添加文本 [英] Add text before product price if it's higher than a specific amount in Woocommerce

查看:53
本文介绍了如果价格高于Woocommerce中的特定数量,请在产品价格之前添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Woocommerce中,如果价格高于59欧元,我会尝试添加文本之前

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;
}

如果产品价格高于59,该如何在产品价格前添加(GRATIS!)文本?

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

推荐答案

已更新:您应该尝试以下重新访问的功能代码:

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;
}

代码进入活动子主题(或活动主题)的function.php文件.

经过测试,可以正常工作.

Tested and works.

仅对于单个产品页面,您将替换以下行:

if( $price > 15 ){

通过这个:

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


对于单个产品页面和存档页面,您将替换以下行:

if( $price > 15 ){

通过这个:

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

这篇关于如果价格高于Woocommerce中的特定数量,请在产品价格之前添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆