为 WooCommerce 中显示的价格添加前缀,具体取决于它是否在促销以及在哪个页面上

问题描述 投票:0回答:1

我的问题与在 WooCommerce 中显示价格之前添加自定义文本答案相关。

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

我使用了以下代码:

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

但这并不能解决我想要的问题。

php wordpress woocommerce product price
1个回答
0
投票

以下内容将在显示的价格中添加不同的字符串前缀,具体取决于产品是否促销以及页面类型(单个产品、商店、档案、购物车和结帐)

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

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

© www.soinside.com 2019 - 2024. All rights reserved.