省略计算中的父产品

时间:2016-05-27 18:19:57

标签: php if-statement x-cart

我遇到了一些解决某些计算的问题。我有一个产品页面,在该产品页面中,我有一些相关产品,可以折扣价格作为一组购买。我有一切工作,但我有问题从计算/折扣百分比省略父产品。

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

如何检查$ 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;
?>

1 个答案:

答案 0 :(得分:0)

您可以尝试向父产品添加标记,并允许在循环中使用更可靠的条件

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

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