为WooCommerce单个产品页面设置第二个价格标签


Setting a second price label for WooCommerce single product pages

在WooCommerce中,我有一个单一的产品视图,显示一个价格。我想再加一个价格标签,它应该显示原价除以一个数字。

现在我正在工作的 cart.php 文件是在我的主题文件夹,这是我的代码:

<p class="price"><?php echo $product->get_price_html(); ?> / Flasche <br> </p>
<p class="preisproliter"><?php 
$flaschenpreis = $product->get_price_html(); 
$literpreis = 0.75; 
$division = $flaschenpreis / $literpreis; 
echo $division; 
?></p>

第一行显示正确,但第二行只显示0值。

我做错了什么?

谢谢。

当您试图通过计算来操纵价格值时,您应该使用 get_price() 而不是 get_price_html() 方法。

代码:

<p class="price"><?php echo $product->get_price_html(); ?> / Flasche <br> </p>
<p class="preisproliter"><?php 
$flaschenpreis = $product->get_price(); // <= HERE
$literpreis = 0.75; 
$division = $flaschenpreis / $literpreis; 
echo $division;
?></p>

一旦你的计算完成并工作,你可以选择使用 'woocommerce_price_html' 过滤器钩子来格式化$division(就像 get_price_html() 那样)

选择显示 $division 格式化的HTML值,你可以在你的代码中使用:

echo apply_filters( 'woocommerce_price_html', $division, $this ); 

谢谢你的回答!

它仍然不起作用,因为我有可变价格。如果我使用$product->get_price()而不是$product->get_price_html(),我得到$division = $flaschenpreis * 6/$literpreis;

我想原因是,我对产品的第一个变化是价格* 6。所以
<p class="price"><?php echo $product->get_price_html(); ?> / Flasche <br> </p>
<p class="preisproliter"><?php 
$flaschenpreis = $product->get_price(); // <= HERE
$literpreis = 0.75; 
$division = $flaschenpreis / $literpreis / 6; 
echo $division;
?></p>

使我得到正确的价格。这仍然需要格式化(四舍五入到两个小数点)。

无论如何,我认为这是一个肮脏的解决方案。有干净的方法吗?为什么不可能第二次使用get_price_html ?