Skip to content

Instantly share code, notes, and snippets.

@incrize
Created September 21, 2015 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save incrize/c2be91df822af2c8f3c5 to your computer and use it in GitHub Desktop.
Save incrize/c2be91df822af2c8f3c5 to your computer and use it in GitHub Desktop.
diff --git a/app/functions/fn.promotions.php b/app/functions/fn.promotions.php
index 6f06298..f1d63d6 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_generate_code('', COUPON_CODE_LENGTH);
+ $cart['promotions'][$bonus['promotion_id']]['bonuses'][$bonus_id]['coupon_code'] = fn_promotion_generate_bonus_coupon($bonus, $bonus_id, $cart);
} elseif ($bonus['bonus'] == 'free_shipping') {
@@ -1693,8 +1693,6 @@ 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') {
@@ -1710,20 +1708,12 @@ 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_id = $cart['parent_order_id'];
+ } else { // this is sub order
+ $parent_order = fn_promotion_get_order($cart['parent_order_id']);
- // 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'])) {
+ if (!empty($parent_order['subtotal'])) {
// calculate the share of the full discount
- $value = $value * $price / $parent_orders[$parent_order_id]['subtotal'];
+ $value = $value * $price / $parent_order['subtotal'];
}
$discount = fn_promotions_calculate_discount($type, $price, $value);
@@ -1906,3 +1896,46 @@ 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);
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment