form-coupon.php 5.39 KB
<?php
/**
 * Checkout coupon form
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/checkout/form-coupon.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you (the theme developer).
 * will need to copy the new files to your theme to maintain compatibility. We try to do this.
 * as little as possible, but it does happen. When this occurs the version of the template file will.
 * be bumped and the readme will list any important changes.
 *
 * @see     http://docs.woothemes.com/document/template-structure/
 * @author  WooThemes
 * @package WooCommerce/Templates
 * @version 2.2
 */
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

if (!wc_coupons_enabled()) {
    return;
}

if (!WC()->cart->applied_coupons) {
    $info_message = apply_filters('woocommerce_checkout_coupon_message', __('Have a coupon?', 'woocommerce') . ' <a href="#" class="showcoupon">' . __('Click here to enter your code', 'woocommerce') . '</a>');
    wc_print_notice($info_message, 'notice');
}
$customer_orders = get_posts(array(
    'numberposts' => -1,
    'meta_key' => '_customer_user',
    'meta_value' => get_current_user_id(),
    'post_type' => wc_get_order_types(),
    'post_status' => array_keys(wc_get_order_statuses()),
        ));
// WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] )
if (!session_id())
    session_start();
$arrayCart = WC()->cart->get_cart();
$subtotal = 0;
$old_product = 0;
$currentProduct = 0;
if (!empty($arrayCart)) {
    foreach ($arrayCart as $cart_item_key => $cart_item) {
        $_product = $cart_item['data'];
        $quantity = $cart_item['quantity'];
        $price = $_product->get_price();
        $subtotal += round($quantity * $price, 2);
        $currentProduct = $_product->get_id();
        break;
    }
    $subtotal = round($subtotal, 2);
}

global $user_id_created;
global $firstreferee;
if (class_exists('ClassProcessCronjob')) {

    $is_affiliates = ($_COOKIE['wp_affiliates'] != '' && (count($customer_orders) == 0)) ? $_COOKIE['wp_affiliates'] : false;
    //if (count($customer_orders) == 0) {
    $valueSession = isset($_SESSION['data_coupon']) ? $_SESSION['data_coupon'] : array();
    $checkCouponCode = isset($valueSession['coupon_code']) ? $valueSession['coupon_code'] : '';
    
    if (in_array($checkCouponCode, $firstreferee)) {
        unset($_SESSION['data_coupon']);
        $valueSession = array();
    }

    //array('coupon_code_id' => $coupon_id, 'coupon_code' => $data['coupon'], 'coupon_amount' => $data['amount'], 'sub_total2' => $sub_total2, 'sub_total' => $sub_total, 'old_product' => $old_porduct);
    //createCouponForCustomerInFirstCheckout($sub_total, $customer_id, $user_create_id, $old_porduct, $old_coupon_code = '', $eixst_aff = false)
    if (!empty($valueSession)) {
        $oldProduct = isset($valueSession['old_product']) ? $valueSession['old_product'] : $currentProduct;
        $oldCoupon = isset($valueSession['coupon_code']) ? $valueSession['coupon_code'] : '';
        $arrayResult = ClassProcessCronjob::createCouponForCustomerInFirstCheckout($subtotal, get_current_user_id(), $user_create_id, $currentProduct, $oldCoupon, $is_affiliates);

        if (!empty($arrayResult)) {
            $_SESSION['data_coupon'] = $arrayResult;
            $coupon = isset($arrayResult['coupon_code']) ? $arrayResult['coupon_code'] : '';
            WC()->cart->remove_coupons();
            if (!WC()->cart->has_discount($coupon)) {

                WC()->cart->add_discount($coupon);
            }
            if (!in_array($coupon, $firstreferee)) {
                ClassProcessCronjob::updateReferralsUserCoupons($arrayResult);
            }
        }
    } else {
        $arrayResult = ClassProcessCronjob::createCouponForCustomerInFirstCheckout($subtotal, get_current_user_id(), $user_create_id, $currentProduct, '', $is_affiliates);

        if (!empty($arrayResult)) {
            $_SESSION['data_coupon'] = $arrayResult;
            $coupon = isset($arrayResult['coupon_code']) ? $arrayResult['coupon_code'] : '';
            WC()->cart->remove_coupons();
            if (!WC()->cart->has_discount($coupon)) {
                WC()->cart->add_discount($coupon);
            }
            if (!in_array($coupon, $firstreferee)) {
                ClassProcessCronjob::updateReferralsUserCoupons($arrayResult);
            }
        }
    }
    // }
} else {
    if (isset($_COOKIE['wp_affiliates']) && $_COOKIE['wp_affiliates'] != '') {
        // Check is first order
        if (count($customer_orders) == 0) {
            $firstreferee_coupon_code = 'firstreferee';
            $affiliateId = $_COOKIE['wp_affiliates'];
            if (in_array($affiliateId, $firstreferee)) {
                $firstreferee_coupon_code = $firstreferee_coupon_code . $affiliateId;
            }
            if (!WC()->cart->has_discount($firstreferee_coupon_code)) {
                WC()->cart->add_discount($firstreferee_coupon_code);
            }
        }
    }
}
?>

<form class="checkout_coupon" method="post" style="display:none">

    <p class="form-row form-row-first">
        <input type="text" name="coupon_code" class="input-text" placeholder="<?php esc_attr_e('Coupon code', 'woocommerce'); ?>" id="coupon_code" value="" />
    </p>

    <p class="form-row form-row-last">
        <input type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e('Apply Coupon', 'woocommerce'); ?>" />
    </p>

    <div class="clear"></div>
</form>