0

我在处理一些计算时遇到了一些问题。我有一个产品页面,在该产品页面中,我有一些相关产品可以以折扣价作为一组购买。我一切正常,但在计算/折扣百分比中省略父产品时遇到问题。

下面的代码是从折扣中删除父产品,但是如果我将另一个组添加到购物车中,它只会省略第一个父项,而不是新添加的父项。

如何检查 $buy_together_id 并确认只有第一个具有唯一 ID 的产品从折扣计算中省略?

我最近添加的解决这个问题的代码是 if ($k != 0) 的两个实例,但同样它只检查第一个父级而不是所有父级。

谢谢,

塞尔吉奥

<?php

   global $config, $current_area;

   if ($current_area != 'C' || !function_exists('func_bt_distribute_discount')){
       return;
   }

   $discount_to_add = 0;
   $group_discounts = array();

   $bts_in_cart = array();
   foreach ($products as $k => $v){
       if ($k != 0) {
           if ($v['buy_together_id']){
               foreach ($v['buy_together_id'] as $gid){
                   $bts_in_cart[$gid][] = $v['productid'];
               }
           }
       }
   }

   // Go through each product and calculate discount to be applied. //

   foreach ($products as $k => $v){

       if ($k != 0) {

           if ($v['buy_together_id']){

               foreach ($v['buy_together_id'] as $buy_together_id){

                   $_discount = 0;

                   if (!isset($GLOBALS['group_bt_discounts'][$buy_together_id])){
                       $GLOBALS['group_bt_discounts'][$buy_together_id] = func_query_first('SELECT * FROM xcart_buy_together_groups
                                                                                             WHERE groupid='.intval($buy_together_id));
                   }

                   $g_discount = $GLOBALS['group_bt_discounts'][$buy_together_id];
                   $price = defined('BT_ADD_TAXES') && BT_ADD_TAXES && $v['taxed_price'] ? $v['taxed_price'] : $v['price'];

                   // Discount //
                   if ($g_discount['discount']){
                       if ($g_discount['discount_type'] == 'P'){
                           $_discount = ($price / 100) * $g_discount['discount'];
                       } else {
                           $_discount = $g_discount['discount']/count($bts_in_cart[$buy_together_id]);
                       }
                   }

                   if ($_discount > 0){

                       /* Add to discount for the quantity */
                       if ($config['Buy_Together']['bt_apply_to_all'] == 'Y'){
                           $_discount *= $v['amount'];
                       }

                       // Change the product discount //
                       $products[$k] = func_bt_distribute_discount($products[$k], $_discount);

                   }

                   /* Cumulative total */
                   $discount_to_add += $_discount;
               }
           }
       }

   }

   $return['products'] = $products;
   $return['discount'] = $discount+$discount_to_add;
   $return['discount_orig'] = $discount+$discount_to_add;
?>
4

1 回答 1

0

您可以尝试向父产品添加一个标志,这将允许在循环中使用更可靠的条件

foreach ($products as $k => $v){
  if ($k != 0) {
     // ......
  }
}

而不是 if ($k != 0)。

于 2016-06-22T13:16:52.010 回答