vendredi 27 mai 2016

Omitting the parent product from calculation

I'm having some issues addressing some calculations. I have a product page, and in that product page I have some related products that can be purchased as a group at a discounted price. I have everything working, but I am having issues omitting the parent product from the calculation/discounted percentage.

The code below is removing the parent prod from the discount, but if I add another group to the cart, it only omits the first parent and not the newly added parent.

How can I check the $buy_together_id and confirm only the first of each product with a unique id is omitted from the discount calculation?

My most recent addition to the code to tackle this issue, is both instances of if ($k != 0), but again it is only checking for the first parent and not all parents.

Thank you,

Sergio

<?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;
?>

Aucun commentaire:

Enregistrer un commentaire