I am using WooCommerce on my Wordpress site and I want to display the total price per product in my cart/minicart (eg: 'Product Quantity' x 'Product Price' = 'Product Total Price'). The code I am using right now is working, but there are rounding errors.
Expected:
Displayed:
Here is the code I am using:
<?php $new_product_price_old = number_format((float)wp_kses( woocommerce_price( $_product->get_price() ), array() ) * $cart_item['quantity'], 2, ',', '.'); ?>
<?php echo '<span class="sidecart-item-price">' . $new_product_price ?> <span class="woocommerce-Price-currencySymbol">€</span></span>
Is there a simple way to fix this or maybe another piece of code? Or a hook I am missing? Total beginner here...
Thanks in advance
So I fixed this myself. I don't know where I got my previous code from, but I guess this part was causing the issue:
wp_kses( woocommerce_price( $_product->get_price() ), array()
Here is my working code (I am a beginner, so there might be a better way to do this):
<?php
$product_price_single = $cart_item['data']->get_price();
$product_price_total_formatted = number_format($product_price_single * $cart_item['quantity'], 2, ',', '.');
?>
<?php echo '<span>' . $product_price_total_formatted ?> <span class="woocommerce-Price-currencySymbol">€</span></span>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.