0

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;
?>
cmorrissey
  • 8,493
  • 2
  • 23
  • 27
Sergio
  • 792
  • 3
  • 10
  • 35
  • we're missing a lot of info that would be needed to resolve this. – cmorrissey May 27 '16 at 18:26
  • @cmorrissey This is a file part of a plugin for X-Cart. Not really sure what other data/info you need. I contacted the seller of the plugin and he said he can't make the changes but the file to modify is the one I included above. – Sergio May 27 '16 at 18:36
  • tag your question with those tags! name the plugin, etc. – cmorrissey May 27 '16 at 18:38
  • It did not allow me to! Something about not enough reputation. The plugin is called Buy Together. – Sergio May 27 '16 at 18:42

1 Answers1

0

You could try to add a flag to parent products and it would allow to use more reliable condition in the loop

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

rather than if ($k != 0).

Tony
  • 149
  • 5
  • I'm not sure I see the difference between your example and what I have included. Thank you btw. – Sergio Jun 23 '16 at 19:42
  • There is no difference, because it's a piece of code from your example :) My point is that you may want to add an additional flag to product info that will indicate a parent product, so when you cycle from product array as foreach ($products as $k => $v) you could use a condition other than if ($k != 0). It is hard to give more precise instruction as I do not have a code of the mod. – Tony Jun 24 '16 at 19:53