WooCommerce 自定义模板上的小数点分隔符问题 price.php

Decimal separator issue on WooCommerce custom template price.php

关于 woo 我有几个问题 price.php 首先,我想更改 get_price_html() 显示价格的方式,但我找不到方法,然后我决定更改 price.php 文件并在 get_price_html 之前添加以下行:

<div class="row">
    
    <div class="col-md-6">
        <p class="<?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?>" style="color: #212529; font-weight: 700; font-size: 1.61rem; line-height: 2rem; text-transform: none; margin-top: -0.25rem;"><?php
           echo get_woocommerce_currency_symbol();
        echo $product->get_regular_price(); ?></p>
    </div>
    <div class="col-md-6">
        <span class="text-danger">VAT included </span>
    </div>
</div>
<div class="row">
    <div class="col-md-6">
        <p class="<?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?>" style="color: #212529; font-weight: 700; font-size: 1.61rem; line-height: 2rem; text-transform: none; margin-top: -0.25rem;"><?php 
           echo get_woocommerce_currency_symbol();
            echo $product->get_sale_price();
        ?></p>
    </div>
    <div class="col-md-6">
        <span class="text-danger">Price per KG </span>
    </div>
</div>

它适合我,并以我正在寻找的样式显示产品价格。但有几个问题:

  1. 正如您在 get_price_html 中看到的那样,价格以 , 显示,但在 get_regular_price 中显示的价格以 . 作为小数
  2. 此外,当 get_sale_price 可用时,它不会在正常价格基础上增加 <strike>

完全是编辑 get_price_html 而不是 get_sale_priceget_sale_price

的最佳方式

以下重新访问的代码将解决小数点分隔符问题,使用 WooCommerce wc_price() 专用价格格式化函数 (也处理货币符号显示).

它也会处理正常价格。

<div class="row">
    <div class="col-md-6">
        <p class="<?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?>" style="color: #212529; font-weight: 700; font-size: 1.61rem; line-height: 2rem; text-transform: none; margin-top: -0.25rem;"><del><?php
           echo wc_price(wc_get_price_to_display($product, array('price' => $product->get_regular_price())));
        ?></del></p>
    </div>
    <div class="col-md-6">
        <span class="text-danger"><?php _e("VAT included"); ?> </span>
    </div>
</div>
<div class="row">
    <div class="col-md-6">
        <p class="<?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?>" style="color: #212529; font-weight: 700; font-size: 1.61rem; line-height: 2rem; text-transform: none; margin-top: -0.25rem;"><ins><?php
           echo wc_price(wc_get_price_to_display($product, array('price' => $product->get_sale_price())));
        ?></ins></p>
    </div>
    <div class="col-md-6">
        <span class="text-danger"><?php _e("Price per KG"); ?></span>
    </div>
</div>

它应该更好用。