dtwr2012 2019-02-19 11: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 12: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...

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!