在 WooCommerce 中,我尝试使用以下代码输出产品 html 价格增加了 20%:
echo $product->get_price_html(); // +20%
我已经尝试了很长时间让它工作,但不幸的是我做不到。
我怎样才能实现这一目标?
谢谢
首先您必须知道变量 $product 是 WC_Product 类的实例,这将允许您应用该 WC_Product 类中的任何方法。
您可以这样使用 和 WC_Product method adjust_price()
:
// Get the numerical (float) price value
$product_price = (float) $product->get_price();
// calculate 20% of product price
$add_to_price = $product_price * 0.2;
// Adjust the new displayed price
$product->adjust_price( $add_to_price );
// Displaying the new adjusted price html
echo $product->get_price_htm();
所以现在你终于可以简单地重用
WC_Product get_price_html() 方法… html 产品价格输出增加了 20%