我的问题是关于Add a custom text before the price display in WooCommerce个答案的.

  1. 我希望仅当产品有折扣时才显示文本'TEXT',如果产品没有折扣,则应显示另一个文本(或默认情况下,在价格之前不应显示任何文本).
  2. 我在产品页面上有相关的产品,我不希望这些文本显示在这一部分,我只希望它们显示在产品页面上(单一产品).
  3. 如何管理此文本在不同部分中的显示?(例如,一个文本应该显示在单一产品页面上,另一个文本应该显示在store 页面上,另一个文本应该显示在存档页面上、搜索列表中,以此类推,在其他部分中)

我使用了以下代码:

add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
function cw_change_product_price_display( $price ) { 
    if ( is_product() && $product->is_on_sale() ){ 
    // Your additional text in a translatable string 
        $text = __('TEXT'); // returning the text before the price return 
        $text. ' ' . $price; 
    } 
    return $price; 
}

但它并没有给我我想要的东西.

推荐答案

以下代码将根据产品是否在销售以及页面类型(single product, shop, archives, cart and checkout)的不同,向显示的价格添加不同的字符串前缀:

add_filter( 'woocommerce_get_price_html', 'add_string_prefix_to_price_html', 10, 2 );
function add_string_prefix_to_price_html( $price_html, $product ) { 
    global $woocommerce_loop;

    // Not on related products
    if ( isset($woocommerce_loop['name']) && $woocommerce_loop['name'] === 'related' ) {
        return $price_html; 
    }

    $prefix = ''; // Initializing

    // Set the text by section type
    if ( is_shop() ) {
        if ( $product->is_on_sale() ){ 
            $prefix = __('ON SALE SHOP', 'woocommerce') . ' ';
        } else {
            $prefix = __('NORMAL SHOP', 'woocommerce') . ' '; // optional
        }
    } elseif( is_tax() ) {
        if ( $product->is_on_sale() ){ 
            $prefix = __('ON SALE ARCHIVES', 'woocommerce') . ' ';
        } else {
            $prefix = __('NORMAL ARCHIVES', 'woocommerce') . ' '; // optional
        }
    } elseif( is_product() ) {
        if ( $product->is_on_sale() ){ 
            $prefix = __('ON SALE PRODUCT', 'woocommerce') . ' ';
        } else {
            $prefix = __('NORMAL PRODUCT', 'woocommerce') . ' '; // optional
        }
    } else {
        if ( $product->is_on_sale() ){ 
            $prefix = __('ON SALE OTHERS', 'woocommerce') . ' ';
        } else {
            $prefix = __('NORMAL OTHERS', 'woocommerce') . ' '; // optional
        }
    }
    return $prefix . $price_html; 
}

// For cart (optional)
add_filter( 'woocommerce_cart_item_price', 'add_string_prefix_to_cart_item_price_html', 10, 2 ); 
function add_string_prefix_to_cart_item_price_html( $price_html, $cart_item ) {
    $prefix = ''; // Initializing
    
    if ( $cart_item['data']->is_on_sale() ){ 
        $prefix = __('ON SALE CART', 'woocommerce') . ' ';
    } else {
        $prefix = __('NORMAL CART', 'woocommerce') . ' '; // optional
    }
    return $prefix . $price_html;
}

// For checkout (optional)
add_filter( 'woocommerce_cart_item_subtotal', 'add_string_prefix_to_checkout_item_subtotal_html', 10, 2 );
function add_string_prefix_to_checkout_item_subtotal_html( $subtotal_html, $cart_item ) {
    $prefix = ''; // Initializing

    if( is_checkout() ) {
        if ( $cart_item['data']->is_on_sale() ){ 
            $prefix = __('ON SALE CHECKOUT', 'woocommerce') . ' ';
        } else {
            $prefix = __('NORMAL CHECKOUT', 'woocommerce') . ' '; // optional
        }
    }
    return $prefix . $subtotal_html;
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.

Php相关问答推荐

提交表格后的重定向不起作用

如何在WordPress中为特定自定义帖子类型自定义URL struct

将带有值的属性添加到WooCommerce产品,保留现有属性

将购买限制在WooCommerce中具有相同产品属性的项目

筛选器具有多个查询之一

使用php ZipArhive类将Zip压缩文件分成多个部分

在Laravel中从动态嵌套数组中获取值

我有一个显示记录的表格,每行在最右边有两个按钮.如何才能以正确的顺序获得表的值?

fpm-php + nginx + POST数据

Laravel withOnly不限制查询

需要以一种形式执行两个操作

根据支付网关选项更改 WooCommerce Checkout 上的立即付款按钮文本

根据所选付款方式启用/禁用 WooCommerce 所需的 checkout 字段

Woocommerce单个添加到购物车页面中的产品动态定价

如何设置Mailersend PHP API 模板变量?

Laravel 8:会话不适用

是否有适用于 Windows 的真正 64 位版本的 PHP 5.6?

Laravel 10 单元测试看不到来自 Artisan Command 的数据库更改

如何将数字文本文件添加到 PHP 数学方程式中?

在管理表单字段中