PluginProbe
Ultimate Store Kit – Store Builder Addons for Elementor, WooCommerce Store Builder, EDD Store Builder / 1.5.2
Ultimate Store Kit – Store Builder Addons for Elementor, WooCommerce Store Builder, EDD Store Builder v1.5.2
3.1.4 3.0.8 3.0.9 3.1.0 3.1.2 3.1.3 3.0.7 3.0.5 3.0.4 3.0.3 3.0.2 trunk 1.5.0 1.5.1 1.5.2 1.6.1 1.6.2 1.6.3 1.6.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 All 93 releases
ultimate-store-kit / includes / wishlist-compare.php

wishlist-compare.php in Ultimate Store Kit – Store Builder Addons for Elementor, WooCommerce Store Builder, EDD Store Builder 1.5.2, at includes/wishlist-compare.php

182 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace UltimateStoreKit;
4
5 if (!defined('ABSPATH')) {
6 exit;
7 }
8
9 // Exit if accessed directly
10
11 if (!function_exists('is_plugin_active')) {
12 include_once ABSPATH . 'wp-admin/includes/plugin.php';
13 }
14
15 final class WishlistCompare {
16 public function __construct() {
17 add_action('wp_ajax_usk_add_to_wishlist', [$this, 'usk_add_to_wishlist']);
18 add_action('wp_ajax_nopriv_usk_add_to_wishlist', [$this, 'usk_add_to_wishlist']);
19
20 add_action('wp_ajax_usk_add_to_compare_products', [$this, 'usk_add_to_compare_products']);
21 add_action('wp_ajax_nopriv_usk_add_to_compare_products', [$this, 'usk_add_to_compare_products']);
22
23 add_action('wp_ajax_usk_remove_from_compare_products', [$this, 'usk_remove_from_compare_products']);
24 add_action('wp_ajax_nopriv_usk_remove_from_compare_products', [$this, 'usk_remove_from_compare_products']);
25
26 //wishlist
27 // add_action('woocommerce_account_wishlist_endpoint', [$this, 'usk_wishlist_content']);
28 // add_filter('woocommerce_account_menu_items', [$this, 'usk_wishlist_link_my_account']);
29 // add_filter('query_vars', [$this, 'usk_wishlist_query_vars'], 0);
30 // add_action('init', [$this, 'usk_add_rewrite_flash_rules_endpoint']);
31 }
32
33 public function usk_add_to_wishlist() {
34 $response = [
35 'status' => 0,
36 'message' => __('Unauthorized!', 'ultimate-store-kit'),
37 ];
38
39 if (!isset($_POST['product_id'])) {
40 $response['message'] = __('No product selected!', 'ultimate-store-kit');
41 wp_send_json($response);
42 }
43
44 $product_id = isset($_POST['product_id']) ? sanitize_text_field($_POST['product_id']) : '';
45
46 $user_id = get_current_user_id();
47 $wishlist = ultimate_store_kit_get_wishlist($user_id);
48
49 $wishlistCounter = count($wishlist);
50 // print_r(count($wishlist));
51
52 if (($key = array_search($product_id, $wishlist)) !== false) {
53 $response['action'] = 'removed';
54 $response['count'] = --$wishlistCounter;
55 unset($wishlist[$key]);
56 } else {
57 $response['action'] = 'added';
58 $response['count'] = ++$wishlistCounter;
59 $wishlist[] = $product_id;
60 }
61
62 $wishlist = array_unique($wishlist);
63
64 // update wishlist
65 $this->ultimate_store_kit_set_wishlist($wishlist, $user_id);
66
67 // send response
68 $response['status'] = 1;
69
70 if ($response['action'] == 'added') {
71 $response['message'] = __("Wishlist item Added!", "ultimate-store-kit");
72 wp_send_json($response);
73 } else {
74 $response['message'] = __("Add To Wishlist", "ultimate-store-kit");
75 wp_send_json($response);
76 }
77 }
78
79 public function ultimate_store_kit_set_wishlist($wishlist, $user_id = 0) {
80 $_wishlist_key = '_ultimate_store_kit_wishlist';
81 $_wishlist = [];
82 // if ($user_id != 0) {
83 // update_user_meta($user_id, $_wishlist_key, $wishlist);
84 // } else {
85 setcookie($_wishlist_key, serialize($wishlist), time() + MONTH_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN);
86 // }
87 }
88
89 public function get_compare_product_page_id() {
90 if($comparePage = ultimate_store_kit_compare_product_page()){
91 return $comparePage->ID;
92 }
93 }
94
95
96 //======================================
97 //=========COMPARE PRODUCTS=============
98 //======================================
99 public function usk_add_to_compare_products() {
100 $response = [
101 'status' => 0,
102 'message' => __('Unauthorized!', 'usk'),
103 ];
104
105 if (!isset($_POST['product_id'])) {
106 $response['message'] = __('No product selected!', 'usk');
107 wp_send_json($response);
108 }
109
110 $user_id = get_current_user_id();
111 $compare_products = usk_get_compare_products($user_id);
112
113 // count compare products
114 if (is_array($compare_products)) {
115 $response['count'] = count($compare_products) + 1;
116 }
117
118 //add to compare products
119 $response['action'] = 'added';
120 $compare_products[] = $_POST['product_id'];
121
122 $compare_products = array_unique($compare_products);
123
124 // update compare_productsusk_add_to_compare_products
125 $this->ultimate_store_kit_set_compare_products($compare_products, $user_id);
126
127 // send response
128 $response['status'] = 1;
129 if ($response['action'] == 'added') {
130 $response['message'] = __("Added", "ultimate-store-kit");
131 $response['url'] = '';
132 if($pageId = $this->get_compare_product_page_id()){
133 $response['url'] = get_permalink($pageId);
134 }
135
136 wp_send_json($response);
137 } else {
138 $response['message'] = __("Compare", "ultimate-store-kit");
139 wp_send_json($response);
140 }
141 }
142 public function usk_remove_from_compare_products() {
143 $response = [
144 'status' => 0,
145 'message' => __('Unauthorized!', 'ultimate-store-kit'),
146 ];
147 if (!isset($_POST['product_id'])) {
148 $response['message'] = __('No product selected!', 'ultimate-store-kit');
149 wp_send_json($response);
150 }
151 $user_id = get_current_user_id();
152 $compare_products = usk_get_compare_products($user_id);
153
154 //add remove from compare products
155 if (($key = array_search($_POST['product_id'], $compare_products)) !== false) {
156 $response['action'] = 'removed';
157 unset($compare_products[$key]);
158 }
159 $compare_products = array_unique($compare_products);
160
161 // update compare_products
162 $this->ultimate_store_kit_set_compare_products($compare_products, $user_id);
163
164 // send response
165 $response['status'] = 1;
166 $response['message'] = sprintf(__('compare products item %s!', 'ultimate-store-kit'), $response['action']);
167 wp_send_json($response);
168 }
169 public function ultimate_store_kit_set_compare_products($compare_products, $user_id = 0) {
170 $_compare_products_key = '_ultimate_store_kit_compare_products';
171 $_compare_products = [];
172
173 if ($user_id != 0) {
174 update_user_meta($user_id, $_compare_products_key, $compare_products);
175 } else {
176 setcookie($_compare_products_key, serialize($compare_products), time() + MONTH_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN);
177 }
178 }
179 }
180
181 new WishlistCompare();
182