woocommerce-check-after-add-to-cart-subscription.php
5.56 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* Plugin Name: Fgc Process check product in cart after add product to cart
* Plugin URI: http://fgc.vn
* Description: Override product in cart if product was existed before, Process redirect page after add to cart
* Author: Hoang Bien <hoangbien264@gmail.com>, Phuong An <phuongantt.na@gmail.com>
* Author URI: http://fgc.vn
* Version: 1.0
* Date create: 8 July 2016
*
* Copyright 2016 Prospress, Inc. (email : freedoms@prospress.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package WooCommerce Subscriptions
* @author Hoang Bien <hoangbien264@gmail.com>
* @since 1.0
*/
/**
* [Phuong An]
* Xu ly xoa san pham cu khoi cart de add mot san pham moi
* @global type $woocommerce
* @param type $true
* @param type $product_id
* @param type $quantity
* @return type
*/
function fgc_woocommerce_remove_items_before_add_to_cart($result, $product_id, $quantity) {
global $woocommerce;
// $subscription_product_id = 308;
// if ($product_id == $subscription_product_id) {
if (!empty($woocommerce->cart->get_cart())) {
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
// $_product = $values['data'];
// if ($_product->id == $subscription_product_id) {
$woocommerce->cart->remove_cart_item($cart_item_key);
// }
}
}
// }
return $result;
}
// add the filter
add_filter('woocommerce_add_to_cart_validation', 'fgc_woocommerce_remove_items_before_add_to_cart', 10, 3);
/**
* Xu ly tra ve result sau khi add to cart thanh cong
* @param type $url
* @return type
*/
function fgc_return_result_after_add_to_cart_success($url) {
if (isset($_REQUEST['ajax_add_cart']) && $_REQUEST['ajax_add_cart'] == 'yes') {
$result = array("error" => '', 'message' => 'Product add to cart successfull');
echo json_encode($result);
exit();
} else {
return $url;
}
}
add_filter('woocommerce_add_to_cart_redirect', 'fgc_return_result_after_add_to_cart_success');
function fgc_check_error_add_to_cart() {
if (isset($_REQUEST['ajax_add_cart']) && $_REQUEST['ajax_add_cart'] == 'yes' && isset($_REQUEST['add-to-cart']) && is_numeric($_REQUEST['add-to-cart'])) {
$result = array("error" => 'yes', 'message' => 'Add to cart unsuccessfull');
echo json_encode($result);
exit();
}
}
add_action('wp_loaded', 'fgc_check_error_add_to_cart', 22);
/*
* Empty cart after login if the user has active subscription with simple login.
*/
function check_cart_after_login($login) {
$user = get_user_by('login', $login);
$user_ID = $user->ID;
if (FGC_ActiveCampaign::check_active_subscription_by_user_id($user_ID)) {
$cart_content = WC()->cart->cart_contents;
if (count($cart_content) > 0) {
foreach ($cart_content as $key => $value) {
if (!isset($value['subscription_switch']) && isset($value['variation_id'])) {
unset(WC()->session->order_awaiting_payment, WC()->session->applied_coupons, WC()->session->coupon_discount_amounts, WC()->session->coupon_discount_tax_amounts, WC()->session->cart);
delete_user_meta($user_id, '_woocommerce_persistent_cart');
}
}
}
}
}
add_action('wp_login', 'check_cart_after_login', 99);
/*
* Empty cart after login if the user has active subscription with facebook login.
*/
function check_cart_after_login_fb($user, $user_id) {
if (FGC_ActiveCampaign::check_active_subscription_by_user_id($user_id)) {
$cart_content = WC()->cart->cart_contents;
if (count($cart_content) > 0) {
foreach ($cart_content as $key => $value) {
if (!isset($value['subscription_switch']) && isset($value['variation_id'])) {
unset(WC()->session->order_awaiting_payment, WC()->session->applied_coupons, WC()->session->coupon_discount_amounts, WC()->session->coupon_discount_tax_amounts, WC()->session->cart);
delete_user_meta($user_id, '_woocommerce_persistent_cart');
}
}
}
}
}
add_action('fbl/after_login', 'check_cart_after_login_fb', 9, 2);
/*
* Empty cart after login if the user has active subscription with facebook login.
*/
function check_cart_has_subscription() {
$user_id = get_current_user_id();
if (FGC_ActiveCampaign::check_active_subscription_by_user_id($user_id)) {
$cart_content = WC()->cart->cart_contents;
if (count($cart_content) > 0) {
foreach ($cart_content as $key => $value) {
if (!isset($value['subscription_switch']) && isset($value['variation_id'])) {
unset(WC()->session->order_awaiting_payment, WC()->session->applied_coupons, WC()->session->coupon_discount_amounts, WC()->session->coupon_discount_tax_amounts, WC()->session->cart);
delete_user_meta($user_id, '_woocommerce_persistent_cart');
}
}
}
}
}
add_action('init', 'check_cart_has_subscription', 9, 2);