繁体   English   中英

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

[英]Display custom price suffix on WooCommerce discounted products

我有一家 WooCommerce 商店,并且正在使用“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>

您可以使用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 文件中。 测试和工作。

有关的:

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

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. 保存设置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM