Search code examples
phpwordpresstemplateswoocommercecurrency-formatting

Decimal separator issue on WooCommerce custom template price.php


I have few questions regarding woo price.php first I like to change the way get_price_html() showing the price and I couldn't find a way and then I decide to change price.php file and add the following lines before get_price_html:

<div class="row">
    
    <div class="col-md-6">
        <p class="<?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?>" style="color: #212529; font-weight: 700; font-size: 1.61rem; line-height: 2rem; text-transform: none; margin-top: -0.25rem;"><?php
           echo get_woocommerce_currency_symbol();
        echo $product->get_regular_price(); ?></p>
    </div>
    <div class="col-md-6">
        <span class="text-danger">VAT included </span>
    </div>
</div>
<div class="row">
    <div class="col-md-6">
        <p class="<?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?>" style="color: #212529; font-weight: 700; font-size: 1.61rem; line-height: 2rem; text-transform: none; margin-top: -0.25rem;"><?php 
           echo get_woocommerce_currency_symbol();
            echo $product->get_sale_price();
        ?></p>
    </div>
    <div class="col-md-6">
        <span class="text-danger">Price per KG </span>
    </div>
</div>

enter image description here

It works for me and showing the product's price in the style that I am looking for. but there is few questions:

  1. As you see in get_price_html the price appears with , but in get_regular_price the price shown with . as a decimal
  2. Also when get_sale_price is available, it doesn't add <strike> to the regular price.

Totally is the arny way to edit get_price_html instead of get_sale_price and get_sale_price


Solution

  • The following revisited code that will solve the decimal separator issue, using WooCommerce wc_price() dedicated price formatting function (that also handle the currency symbol display).

    It will handle the striked regular price too.

    <div class="row">
        <div class="col-md-6">
            <p class="<?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?>" style="color: #212529; font-weight: 700; font-size: 1.61rem; line-height: 2rem; text-transform: none; margin-top: -0.25rem;"><del><?php
               echo wc_price(wc_get_price_to_display($product, array('price' => $product->get_regular_price())));
            ?></del></p>
        </div>
        <div class="col-md-6">
            <span class="text-danger"><?php _e("VAT included"); ?> </span>
        </div>
    </div>
    <div class="row">
        <div class="col-md-6">
            <p class="<?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?>" style="color: #212529; font-weight: 700; font-size: 1.61rem; line-height: 2rem; text-transform: none; margin-top: -0.25rem;"><ins><?php
               echo wc_price(wc_get_price_to_display($product, array('price' => $product->get_sale_price())));
            ?></ins></p>
        </div>
        <div class="col-md-6">
            <span class="text-danger"><?php _e("Price per KG"); ?></span>
        </div>
    </div>
    

    It should better work.