首页 > 解决方案 > Woocommerce 从含税的产品价格中舍入和修剪小数。不含税的价格保持小数

问题描述

对于我们在 Woocommerce 上运行的 eshop,我需要更改计算价格的方式。

目前我正在使用一个代码片段,它将在含税价格的后面显示不含税的价格。片段在这里:

add_filter('woocommerce_get_price_suffix', function ( $html, $product, $price, $qty ) {
     if ( ! $html && $product instanceof WC_Product_Variable) {
        
         if ( ( $suffix = get_option( 'woocommerce_price_display_suffix' ) ) 
             && wc_tax_enabled() 
             && 'taxable' === $product->get_tax_status() 
         ) {
             $replacements = array(
                 '{price_including_tax}' => wc_price( wc_get_price_including_tax( $product, array( 'qty' => $qty, 'price' => $price ) ) ),
                 '{price_excluding_tax}' => wc_price( wc_get_price_excluding_tax( $product, array( 'qty' => $qty, 'price' => $price ) ) ),
             );
             $html = str_replace( array_keys( $replacements ), array_values( $replacements ), ' <small class="woocommerce-price-suffix">' . wp_kses_post( $suffix ) . '</small>' );
         }
     }
 
     return $html;
}, 10, 4);   

多亏了这个片段,我的价格看起来像这样:

在此处输入图像描述

但是有时我需要在我们的 eshop 上批量编辑许多产品,例如将所有夹克的价格提高 2.5%。但是,如果我这样做,价格为 100、00 的产品现在将花费 102、50。我不希望包含税的价格包含任何小数,所以如果有人要更改价格,我需要将其四舍五入并去掉小数,但是只有这个价格含税!如果我Woocommerce > Settings > General在这里删除小数点,它也会从不含税的价格中删除小数点,这对我们来说是一个大问题,因为四舍五入的税收不是最聪明的主意。

有没有办法强制仅在不含税的价格上显示小数,并在含税的情况下去掉小数和四舍五入?

标签: phpwordpresswoocommercehook-woocommerce

解决方案


推荐阅读