在 WooCommerce 打折产品上显示自定义价格后缀

Display custom price suffix on WooCommerce discounted products

我有一个 WooCommerce 商店并且正在使用 'Advanced Dynamic Pricing for Woocommerce' 插件。我需要使用这个插件,因为我设置了折扣结构

百分比折扣已应用于商店中的所有产品。原价被划掉,旁边是新价。

在此处输入图片描述

我想在新价格旁边显示一个后缀 'inc VAT' 以表明该价格包含增值税。

我已经尝试过此代码,它似乎适用于未应用折扣但不适用于打折产品的产品。

add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ){
$price = $price . 'inc VAT' ;
return apply_filters( 'woocommerce_get_price', $price );}

有人知道我怎样才能做到这一点吗?

生成的 HTML 代码如下所示:

<div class="woosg-price">
<div class="woosg-price-ori">
<del>
<span class="woocommerce-Price-amount amount">
<span class="woocommerce-Price-currencySymbol">£</span>262.00</span>
</del> 
<ins><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price- 
currencySymbol">£</span>117.90</span></ins> 
</div>
<div class="woosg-price-new"></div>
</div>

您可以使用以下过滤器添加后缀。

add_filter( 'woocommerce_get_price_suffix', 'swt_add_price_suffix', 99, 4 );
  
function swt_add_price_suffix( $html, $product, $price, $qty ){
    $html .= ' inc VAT';
    return $html;
}

或者您也可以使用下面的代码。

add_filter( 'woocommerce_get_price_html', 'swt_add_price_suffix', 99, 2 );
  
function swt_add_price_suffix( $price, $product ){
    $price = $price.' inc VAT';
    return $price;
}

您可以使用以下步骤从 WooCommerce 设置中添加后缀。

  1. 转到 WooCommerce -> 设置 -> 常规。
  2. 标记选中“启用税率和计算”复选框。
  3. 打开税收选项卡。
  4. 在“价格显示后缀”文本字段中添加后缀文本。
  5. 保存设置。

您可以使用 woocommerce_get_price_suffix 专用挂钩,针对促销产品,例如:

add_filter( 'woocommerce_get_price_suffix', 'custom_price_suffix', 999, 4 );
function custom_price_suffix( $html, $product, $price, $qty ){
    if ( $product->is_on_sale() ) {
        return  ' ' .  __('inc VAT', 'woocommerce');
    }
    return $html;
}

或者可能是这个 (因为 Woocommerce 插件的高级动态定价)

add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 999, 2 );
function custom_price_suffix( $price_html, $product ){
    if ( $product->is_on_sale() ) {
        $price_html .= ' ' .  __('inc VAT', 'woocommerce');
    }
    return $price_html;
}

代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。

相关: