コード例 #1
0
ファイル: fn.promotions.php プロジェクト: askzap/ultimate
/**
 * Calculate order discount for sub orders (used in MVE)
 *
 * @param string $type discount type
 * @param array $bonus Array with promotion data
 * @param int $bonus_id Bonus ID
 * @param array $cart Array with cart data
 * @return float calculated discount value
 */
function fn_promotions_calculate_order_discount($bonus, $bonus_id, $cart)
{
    $type = $bonus['discount_bonus'];
    $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') {
        // if it is parent or usual order
        if (empty($cart['parent_order_id'])) {
            // calculate usual discount
            $discount = fn_promotions_calculate_discount($type, $price, $value);
            // save order discount for future calculations of suborders
            $discount = fn_format_price($discount);
            $session_orders_discount =& $_SESSION['orders_discount'][$bonus['promotion_id'] . '_' . $bonus_id];
            $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'];
            // 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_orders[$parent_order_id]['subtotal'];
            }
            $discount = fn_promotions_calculate_discount($type, $price, $value);
            $discount = fn_format_price($discount);
            $session_orders_discount =& $_SESSION['orders_discount'][$bonus['promotion_id'] . '_' . $bonus_id];
            $parent_order_discount = !empty($session_orders_discount['parent_order_discount']) ? $session_orders_discount['parent_order_discount'] : 0;
            $suborders_discount = !empty($session_orders_discount['suborders_discount']) ? $session_orders_discount['suborders_discount'] : 0;
            // check that total suborders discount is less than parent_order_discount
            // or this is last sub order, so we have to distract discount, to avoid the extra cents
            $new_suborders_discount = $suborders_discount + $discount;
            if ($new_suborders_discount > $parent_order_discount || !empty($cart['companies']) && end($cart['companies']) == $cart['company_id']) {
                $discount = $parent_order_discount - (!empty($session_orders_discount['suborders_discount']) ? $session_orders_discount['suborders_discount'] : 0);
                if ($discount < 0) {
                    $discount = 0;
                }
            }
            $session_orders_discount['suborders_discount'] = $suborders_discount + $discount;
        }
    } else {
        $discount = fn_promotions_calculate_discount($type, $price, $value);
    }
    return $discount;
}
コード例 #2
0
/**
 * Apply promotion cart rule
 *
 * @param array $promotion promotion array
 * @param array $cart cart array
 * @param array $auth (optional) - auth array
 * @param array $cart_products (optional) - cart products array (for cart rules)
 * @return bool true if rule can be applied, false - otherwise
 */
function fn_promotion_apply_cart_rule($bonus, &$cart, &$auth, &$cart_products)
{
    // Clean bonuses
    $cart['promotions'][$bonus['promotion_id']]['bonuses'][$bonus['bonus']] = $bonus;
    if ($bonus['bonus'] == 'order_discount') {
        if (floatval($cart['subtotal'])) {
            if (!isset($cart['subtotal_discount'])) {
                $cart['subtotal_discount'] = 0;
            }
            $discount = fn_promotions_calculate_discount($bonus['discount_bonus'], $cart['subtotal'], $bonus['discount_value']);
            if (floatval($discount)) {
                $cart['use_discount'] = true;
                $cart['subtotal_discount'] += fn_format_price($discount);
            }
        }
    } elseif ($bonus['bonus'] == 'discount_on_products') {
        foreach ($cart_products as $k => $v) {
            if (isset($v['exclude_from_calculate']) || !floatval($v['base_price']) && $v['base_price'] != 0) {
                continue;
            }
            if (fn_promotion_validate_attribute($v['product_id'], $bonus['value'], 'in')) {
                if (fn_promotion_apply_discount($bonus['promotion_id'], $bonus, $cart_products[$k])) {
                    $cart['use_discount'] = true;
                }
            }
        }
    } elseif ($bonus['bonus'] == 'discount_on_categories') {
        foreach ($cart_products as $k => $v) {
            if (isset($v['exclude_from_calculate']) || !floatval($v['base_price']) && $v['base_price'] != 0) {
                continue;
            }
            $c_ids = array_keys($v['category_ids']);
            if (fn_promotion_validate_attribute($c_ids, $bonus['value'], 'in')) {
                if (fn_promotion_apply_discount($bonus['promotion_id'], $bonus, $cart_products[$k])) {
                    $cart['use_discount'] = true;
                }
            }
        }
    } elseif ($bonus['bonus'] == 'give_usergroup') {
        $cart['promotions'][$bonus['promotion_id']]['bonuses'][$bonus['bonus']]['pending'] = true;
    } elseif ($bonus['bonus'] == 'give_coupon') {
        $cart['promotions'][$bonus['promotion_id']]['bonuses'][$bonus['bonus']]['pending'] = true;
        $cart['promotions'][$bonus['promotion_id']]['bonuses'][$bonus['bonus']]['coupon_code'] = fn_generate_code('', COUPON_CODE_LENGTH);
    } elseif ($bonus['bonus'] == 'free_shipping') {
        $cart['free_shipping'][] = $bonus['value'];
    } elseif ($bonus['bonus'] == 'free_products') {
        foreach ($bonus['value'] as $p_data) {
            $product_data = array($p_data['product_id'] => array('amount' => $p_data['amount'], 'product_id' => $p_data['product_id'], 'extra' => array('exclude_from_calculate' => true, 'aoc' => empty($p_data['product_options']), 'saved_options_key' => $bonus['promotion_id'] . '_' . $p_data['product_id'])));
            if (!empty($cart['saved_product_options'][$bonus['promotion_id'] . '_' . $p_data['product_id']])) {
                $product_data[$p_data['product_id']]['product_options'] = $cart['saved_product_options'][$bonus['promotion_id'] . '_' . $p_data['product_id']];
            } elseif (!empty($p_data['product_options'])) {
                $product_data[$p_data['product_id']]['product_options'] = $p_data['product_options'];
            }
            $existing_products = array_keys($cart['products']);
            if ($ids = fn_add_product_to_cart($product_data, $cart, $auth)) {
                $new_products = array_diff(array_keys($cart['products']), $existing_products);
                if (!empty($new_products)) {
                    $hash = array_pop($new_products);
                } else {
                    $hash = key($ids);
                }
                $_cproduct = fn_get_cart_product_data($hash, $cart['products'][$hash], true, $cart, $auth, !empty($new_products) ? 0 : $p_data['amount']);
                if (!empty($_cproduct)) {
                    $cart_products[$hash] = $_cproduct;
                }
            }
        }
    }
    return true;
}