dtwr2012 2019-02-19 03:14
浏览 9
已采纳

关于购物车中的数组项目的Woocommerce问题

This is my code in cart:

<?php 
$totaleiva_1 = 0;
$items = $woocommerce->cart->get_cart();
foreach($items as $item ) {
    echo $totaleforsebene;
    $totaleiva_1 = $totaleiva_1 + $totaleforsebene;
}
echo $totaleiva_1;
?>

The result is: the $totaleforsebene value of the last product in cart is added n times for product I have (for example I have two products, the last value $totaleforsebene is added two times).

Instead, I would want to have the $totaleforsebene for different products and then They are been added.

Thanks for help

  • 写回答

1条回答 默认 最新

  • dtgta48604 2019-02-19 04:09
    关注

    You can create array with ID for each product and then add the correct value:

    When you calculate the $totaleforsebene do this:

    foreach($items as $item => $values) {
        $product = wc_get_product( $values['data']->get_id());
        ...
        $totaleforsebene = ($scontato * $percent)/100; 
        $totalForSebeneArray[$values['data']->get_id()] = $totaleforsebene; // add the value by the item ID
        $totaleforsebene = number_format($totaleforsebene, 2, '.', '')." €"; 
    }
    

    Now, when you loop on the item and sum you can do:

    $totaleiva_1 = 0;
    $items = $woocommerce->cart->get_cart();
    foreach($items as $item ) {
        $totaleiva_1 += $totalForSebeneArray[$item ['data']->get_id()];
    }
    echo $totaleiva_1;
    

    You also need to format your question better as now it is very hard to understand your issue and I am not even sure if my explanation answer your problem...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 C#i编程中so-ir-192编码的字符集转码UTF8问题
  • ¥15 51嵌入式入门按键小项目
  • ¥30 海外项目,如何降低Google Map接口费用?
  • ¥15 fluentmeshing
  • ¥15 手机/平板的浏览器里如何实现类似荧光笔的效果
  • ¥15 盘古气象大模型调用(python)
  • ¥15 传人记程序做的plc 485从机程序该如何写
  • ¥15 已知手指抓握过程中掌指关节、手指各关节和指尖每一帧的坐标,用贝塞尔曲线可以拟合手指抓握的运动轨迹吗?
  • ¥50 libwebsockets 如何添加其他socket事件回调
  • ¥50 实现画布拖拽算子排布,通过flink实现算子编排计算,请提供思路
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部