Search code examples
phpwordpresswoocommerceproduct

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.


Solution

  • 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 );