Onur
Onur

Reputation: 33

Setting a second price label for WooCommerce single product pages

In WooCommerce I have a single product view with one price displayed. I want to add another price tag, which should show the original price divided by a number.

Right Now I am working in the cart.php file which is in my themes folder and that is my code:

<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>

The first line is displayed correctly, but for the second one is displaying only a 0 value.

What I'm doing wrong?

Thanks.

Upvotes: 3

Views: 1060

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 254182

As you are trying to manipulate the price value with calculations, you should use get_price() instead of get_price_html() method.

Your code:

<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>

Once your calculations are done and working, you could use optionally 'woocommerce_price_html' filter hook to format $division value (as get_price_html() does).

To Optionally display $division formatted HTML value you could use in your code:

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

Upvotes: 2

Onur
Onur

Reputation: 33

Thanks for the answer!

Well, it still didn't work, because I have variable prices. If I Use $product->get_price() instead of $product->get_price_html(), I get $division = $flaschenpreis * 6 / $literpreis; as a result.

I guess the reason is, that my first variation of the product is price * 6. So

<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>

leads me to the right price. That still needs to be formatted (rounded to two decimal positions).

Anyway, I think that this is a dirty solution. Is there a clean method? And why is it not possible to use get_price_html a second time?

Upvotes: 0

Related Questions