drne47241 2019-02-26 16:08
浏览 66
已采纳

WooCommerce:以购物车/迷你卡显示产品总价而不是产品价格

I am using WooCommerce on my Wordpress site and I want to display the total price per product in my cart/minicart (e.g.: 'Product Quantity' x 'Product Price' = 'Product Total Price'). The code I am using right now is working, but there are rounding errors.

Expected:

  • Product #1
  • Quantity: 2
  • Price: 3,50
  • Total Price: 7,00

Displayed:

  • Product #2
  • Quantity: 2
  • Price: 3,50
  • Total Price: 6,00

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 ?>&nbsp;<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

  • 写回答

1条回答 默认 最新

  • drt12345678 2019-02-28 08:42
    关注

    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 ?>&nbsp;<span class="woocommerce-Price-currencySymbol">€</span></span>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 centos7中sudo命令无法使用
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头