If you’re an e-commerce store owner or a developer working with WooCommerce, you might have come across the WC_Price
function. This function is used to format and display prices in a standardized manner in WooCommerce.
In WooCommerce, prices are stored as decimal values in the database, but they need to be displayed in a human-readable format. For example, instead of storing the price of a product as ‘10.00’, it will be stored as 1000 (representing 10.00 in cents). The WC_Price
function takes care of converting this value into the desired format for display.
Here’s a code sample that shows how you can use the WC_Price
function:
$price = 1000;
echo wc_price( $price );
This code will output the price as $10.00.
One of the benefits of using the WC_Price
function is that it automatically takes into account the currency symbol, decimal separator, and number of decimal places that are set in your WooCommerce store. This means that the price will be displayed correctly, no matter which currency your store is using.
Another advantage of the WC_Price
function is that it automatically adds any discounts or taxes to the price. For example, if a product has a discount of 10%, the WC_Price
function will automatically apply this discount to the price before it’s displayed.
Here’s a code sample that shows how you can use the WC_Price
function to display the price of a product with a discount:
$price = 1000;
$discount = 10;
echo wc_price( $price - ( $price * ( $discount / 100 ) ) );
This code will output the price as $9.00, taking into account the 10% discount.
The WC_Price
function is a useful tool for formatting and displaying prices in a standardized manner in WooCommerce. Whether you’re an e-commerce store owner or a developer, using this function can help ensure that your prices are displayed correctly and consistently, regardless of the currency or any discounts or taxes that may be applied.