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 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计