在 Woocommerce 中,我有这个 PHP 代码,可以输出产品价格及其罚款。
但是我也需要他打印价格除以10。我尝试了很多方法,但就是不行。
代码如下:
if ($hide_price !== '0') :
$output_price .= '<div class="dhvc-woo-price dhvc-woo-span6">';
$output_price .= $product->get_price_html (); //this is where the price is called
$output_price .= '<p><br>Em até 10x de</p>'; //this is where it should print the price / by 10
$output_price .= '</div>';
endif;
$output .= apply_filters('dhvc_woo_price', $output_price, $product, $display);
我尝试了
$output_price .= $product->get_price_html() / 10;
//它给了我0
还尝试了其他方法,但它只是给了我 php 错误。
如有任何帮助,我们将不胜感激。
您需要使用
wc_get_price_to_display($product)
而不是 $product->get_price_html()
来输出原始数字价格。
会关心显示含税或非一般选项…(wc_get_price_to_display($product)
不是这种情况).$product->get_price()
代码:
if ($hide_price !== '0') :
$output_price .= '<div class="dhvc-woo-price dhvc-woo-span6">';
$output_price .= $product->get_price_html (); //this is where the price is called
// Here your correct price divided by 10 and formatted for the output
$price_d10 = wc_price(wc_get_price_to_display($product) / 10);
$output_price .= '<p><br>Em até 10x de '. $price_d10 .'</p>';
$output_price .= '</div>';
endif;
$output .= apply_filters('dhvc_woo_price', $output_price, $product, $display);
已测试且有效
也许:
$product->get_price()/10;
或
number_format((float)$product->get_price()/10, 2, '.', '');
有2位小数?