form-coupon.php
5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?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>