diff --git a/app/functions/fn.promotions.php b/app/functions/fn.promotions.php index f1d63d6..6f06298 100644 --- a/app/functions/fn.promotions.php +++ b/app/functions/fn.promotions.php @@ -482,7 +482,7 @@ function fn_promotion_apply_cart_rule($bonus, &$cart, &$auth, &$cart_products) } elseif ($bonus['bonus'] == 'give_coupon') { $cart['promotions'][$bonus['promotion_id']]['bonuses'][$bonus_id]['pending'] = true; - $cart['promotions'][$bonus['promotion_id']]['bonuses'][$bonus_id]['coupon_code'] = fn_promotion_generate_bonus_coupon($bonus, $bonus_id, $cart); + $cart['promotions'][$bonus['promotion_id']]['bonuses'][$bonus_id]['coupon_code'] = fn_generate_code('', COUPON_CODE_LENGTH); } elseif ($bonus['bonus'] == 'free_shipping') { @@ -1693,6 +1693,8 @@ function fn_promotions_calculate_order_discount($bonus, $bonus_id, $cart) $price = $cart['subtotal']; $value = $bonus['discount_value']; + static $parent_orders = array(); + // this calculations are actual only for the fixed (absolute) amount if ($type == 'to_fixed' || $type == 'by_fixed') { @@ -1708,12 +1710,20 @@ function fn_promotions_calculate_order_discount($bonus, $bonus_id, $cart) $session_orders_discount['parent_order_discount'] = $discount; $session_orders_discount['suborders_discount'] = 0; - } else { // this is sub order - $parent_order = fn_promotion_get_order($cart['parent_order_id']); + } else { + // this is sub order + + $parent_order_id = $cart['parent_order_id']; - if (!empty($parent_order['subtotal'])) { + // get parent order subtotal info + if (!isset($parent_orders[$parent_order_id]['subtotal'])) { + $parent_order_info = fn_get_order_info($parent_order_id); + $parent_orders[$parent_order_id]['subtotal'] = $parent_order_info['subtotal']; + } + + if (!empty($parent_orders[$parent_order_id]['subtotal'])) { // calculate the share of the full discount - $value = $value * $price / $parent_order['subtotal']; + $value = $value * $price / $parent_orders[$parent_order_id]['subtotal']; } $discount = fn_promotions_calculate_discount($type, $price, $value); @@ -1896,46 +1906,3 @@ function fn_promotion_shippings($this, $cart) return $result; } - - -/** - * Return order data. Result saved in internal cache. - * - * @param int $order_id - * @return array|bool - */ -function fn_promotion_get_order($order_id) -{ - static $orders = array(); - - if (empty($order_id)) { - return false; - } - - if (!isset($orders[$order_id])) { - $orders[$order_id] = fn_get_order_info($order_id); - } - - return !empty($orders[$order_id]) ? $orders[$order_id] : false; -} - -/** - * Generate bonus coupon for order - * - * @param array $bonus Array with promotion data - * @param int $bonus_id Bonus ID - * @param array $cart Array with cart data - * @return string Coupon code - */ -function fn_promotion_generate_bonus_coupon($bonus, $bonus_id, $cart) -{ - if (!empty($cart['parent_order_id'])) { - $parent_order = fn_promotion_get_order($cart['parent_order_id']); - - if (!empty($parent_order['promotions'][$bonus['promotion_id']]['bonuses'][$bonus_id]['coupon_code'])) { - return $parent_order['promotions'][$bonus['promotion_id']]['bonuses'][$bonus_id]['coupon_code']; - } - } - - return fn_generate_code('', COUPON_CODE_LENGTH); -}