PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 2.8.16
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v2.8.16
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / admin / modules / steps / checkout / class-wpfnl-checkout.php

class-wpfnl-checkout.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 2.8.16, at admin/modules/steps/checkout/class-wpfnl-checkout.php

461 lines 17.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Checkout module
4 *
5 * @package
6 */
7
8 namespace WPFunnels\Admin\Modules\Steps\Checkout;
9
10 use WPFunnels\Admin\Modules\Steps\Module as Steps;
11 use WPFunnels\Metas\Wpfnl_Step_Meta_keys;
12 use WPFunnels\Wpfnl;
13 use WPFunnels\Wpfnl_functions;
14 use \WC_Subscriptions_Product;
15 use Wpfnl_Type_Factory;
16 class Module extends Steps
17 {
18 protected $validations;
19
20 protected $_internal_keys = [];
21
22 protected $type = 'checkout';
23
24 protected $prefix = '_wpfnl_checkout_';
25
26
27 public function __construct()
28 {
29 add_action('wp_ajax_order_bump_search_products', [$this, 'fetch_products']);
30 add_action('wp_ajax_order_bump_search_coupons', [$this, 'fetch_coupons']);
31 add_action('wpfunnels/after_save_order_bump_data', [$this, 'update_elementor_data'], 10, 2);
32 }
33
34
35 public function get_validation_data()
36 {
37 return $this->validations;
38 }
39
40
41 public function init($id)
42 {
43 parent::init($id);
44 $this->set_internal_meta_value();
45 }
46
47
48 /**
49 * Load assets
50 *
51 * @param $hook
52 *
53 * @since 1.0.0
54 */
55 public function load_scripts($hook)
56 {
57 if (isset($_GET['step_id'])) {
58 $step_id = filter_input(INPUT_GET, 'step_id', FILTER_VALIDATE_INT);
59 if (Wpfnl_functions::check_if_this_is_step_type_by_id($step_id, 'checkout')) {
60 wp_enqueue_script($this->type . '-js', WPFNL_URL . 'admin/assets/dist/js/order-bump.min.js', ['jquery', 'wp-util'], '1.0.0', true);
61 wp_localize_script(
62 $this->type . '-js',
63 'CheckoutStep',
64 [
65 'ajaxurl' => esc_url_raw(admin_url('admin-ajax.php')),
66 'rest_api_url' => esc_url_raw(get_rest_url()),
67 'wc_currency' => get_woocommerce_currency_symbol(),
68 'nonce' => wp_create_nonce('wp_rest'),
69 'security' => wp_create_nonce('wpfnl-admin'),
70 'image_path' => WPFNL_URL . 'admin/assets/images',
71 'tooltipIcon' => WPFNL_URL . 'admin/partials/icons/question-tooltip-icon.php',
72 'imageUploadIcon' => WPFNL_URL . 'admin/partials/icons/image-upload-icon.php',
73 'step_id' => $step_id,
74 'back' => add_query_arg(
75 [
76 'page' => WPFNL_EDIT_FUNNEL_SLUG,
77 'id' => filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT),
78 'step_id' => $step_id,
79 ],
80 admin_url('admin.php')
81 )
82 ]
83 );
84 }
85 }
86 }
87
88
89 /**
90 * Init ajax hooks for
91 * saving metas
92 *
93 * @since 1.0.0
94 */
95 public function init_ajax()
96 {
97 $this->validations = [
98 'logged_in' => true,
99 'user_can' => 'manage_options',
100 ];
101 wp_ajax_helper()->handle('wpfnl-update-checkout-product-tab')
102 ->with_callback([$this, 'save_products'])
103 ->with_validation($this->validations);
104
105 }
106
107 /**
108 * Get view of the checkout settings page
109 *
110 * @since 1.0.0
111 */
112 public function get_view()
113 {
114 $is_pro_activated = Wpfnl_functions::is_wpfnl_pro_activated();
115 $show_settings = filter_input(INPUT_GET, 'show_settings', FILTER_SANITIZE_SPECIAL_CHARS);
116 $this->_internal_keys = Wpfnl_Step_Meta_keys::get_meta_keys($this->type);
117 $this->set_internal_meta_value();
118
119 if ($show_settings == 1) {
120 require_once WPFNL_DIR . '/admin/modules/steps/checkout/views/settings.php';
121 } else {
122 require_once WPFNL_DIR . '/admin/modules/steps/checkout/views/view.php';
123 }
124 }
125
126
127 /**
128 * Save products
129 *
130 * @param Array $payload
131 *
132 * @return Array
133 * @since 2.0.4
134 */
135 public function save_products($payload)
136 {
137 $step_id = $payload['step_id'];
138 $products = array();
139
140 if (isset($payload['products'])) {
141 $products = $payload['products'];
142 }
143
144 $funnel_id = get_post_meta($step_id,'_funnel_id',true);
145 $is_gbf = get_post_meta($funnel_id,'is_global_funnel',true);
146
147 $coupon = $payload['coupon'];
148 $isMultipleProduct = $payload['isMultipleProduct'];
149 $isQuantity = $payload['isQuantity'];
150
151 if ($coupon == 'true') {
152 $coupon = 'yes';
153 } else {
154 $coupon = 'no';
155 }
156
157 if ($isMultipleProduct == 'true') {
158 $isMultipleProduct = 'yes';
159 } else {
160 $isMultipleProduct = 'no';
161 }
162 if ($isQuantity == 'true') {
163 $isQuantity = 'yes';
164 } else {
165 $isQuantity = 'no';
166 }
167
168 if( isset($payload['enableAutoCoupon'], $payload['selectedCoupon'] ) ){
169 $coupon = [
170 'enableAutoCoupon' => $payload['enableAutoCoupon'],
171 'selectedCoupon' => $payload['selectedCoupon'],
172 ];
173 update_post_meta($step_id, '_wpfnl_checkout_auto_coupon', $coupon);
174 }
175
176 if( isset($payload['discountOptions']) ){
177 $discount = [
178 'discountOptions' => isset($payload['discountOptions']) ? $payload['discountOptions'] : 'original',
179 'discountapplyto' => isset($payload['discountapplyto']) && $payload['discountOptions'] != 'original' ? $payload['discountapplyto'] : '',
180 'mutedDiscountValue' => isset($payload['mutedDiscountValue']) && $payload['discountOptions'] != 'original' ? $payload['mutedDiscountValue'] : 0,
181 'discountedPrice' => isset($payload['discountedPrice']) && $payload['discountOptions'] != 'original' ? $payload['discountedPrice'] : 0,
182 ];
183 update_post_meta($step_id, '_wpfnl_checkout_discount_main_product', $discount);
184 }
185
186 if( 'yes' == $is_gbf ){
187 return [
188 'success' => true,
189 'message' => 'Saved Successfully',
190 ];
191 }
192
193 if (!$products) {
194 return [
195 'success' => false,
196 'message' => 'No Product Found',
197 ];
198 }
199
200 foreach ($products as $pr_key => $pr_value) {
201 foreach ($pr_value as $key => $value) {
202 if ($key == 'price' || $key == 'image' || $key == 'title') {
203 unset($products[$pr_key][$key]);
204 }
205 }
206 }
207
208
209
210 update_post_meta($step_id, '_wpfnl_checkout_products', $products);
211 update_post_meta($step_id, '_wpfnl_checkout_coupon', $coupon);
212 update_post_meta($step_id, '_wpfnl_multiple_product', $isMultipleProduct);
213 update_post_meta($step_id, '_wpfnl_quantity_support', $isQuantity);
214
215 return [
216 'success' => true,
217 'message' => 'Saved Successfully',
218 ];
219 }
220
221 /**
222 * Save checkout product tab
223 * data
224 *
225 * @param $payload
226 *
227 * @return array
228 */
229 public function checkout_update_product_tab_options($payload)
230 {
231
232 $step_id = sanitize_text_field($payload['step_id']);
233 unset($payload['step_id']);
234 $step = Wpfnl::get_instance()->step_store;
235 $step->set_id($step_id);
236 $this->_internal_keys = Wpfnl_Step_Meta_keys::get_meta_keys($this->type);
237 foreach ($payload as $key => $value) {
238 if (array_key_exists($this->prefix . $key, $this->_internal_keys)) {
239 switch ($key) {
240 case 'products':
241 $products = [];
242 if (!empty($value)) {
243 $saved_products = get_post_meta($step_id, '_wpfnl_checkout_products', true);
244 if ($saved_products) {
245 $saved_products[] = $value;
246 $products = $saved_products;
247 } else {
248 $products[] = $value;
249 }
250 }
251 $step->update_meta($step_id, $this->prefix . $key, $products);
252 break;
253 case 'discount':
254 $discount[] = $value;
255 $step->update_meta( $step_id, $this->prefix . $key, $discount );
256 break;
257 case 'coupon':
258 $coupon = $value;
259 $step->update_meta($step_id, $this->prefix . $key, $coupon);
260 break;
261 default:
262 $step->update_meta($step_id, $this->prefix . $key, $value);
263 break;
264 }
265 }
266 }
267 return [
268 'success' => true,
269 ];
270 }
271
272
273 /**
274 * Fetch product from WC data store
275 *
276 * @throws \Exception
277 * @since 1.0.0
278 */
279 public function fetch_products()
280 {
281
282 check_ajax_referer('wpfnl-admin', 'security');
283 if (isset($_GET['term'])) {
284 $term = (string)esc_attr(wp_unslash($_GET['term']));
285 }
286 if (empty($term)) {
287 wp_die();
288 }
289
290 if( isset($_GET['isLearndash']) && $_GET['isLearndash'] == 'true' ){
291 $_class = 'lms';
292 }else{
293 $_class = 'wc';
294 }
295 $class_object = Wpfnl_Type_Factory::build($_class);
296 if( $class_object ){
297 $products = $class_object->retrieve_ob_item( $term );
298 }
299 wp_send_json($products);
300 }
301
302
303 /**
304 * Fetch product from WC data store
305 *
306 * @throws \Exception
307 * @since 1.0.0
308 */
309 public function fetch_coupons()
310 {
311 check_ajax_referer('wpfnl-admin', 'security');
312 $term = (string)esc_attr(urldecode(wp_unslash($_GET['term'])));
313 $term = (string)wp_unslash($term);
314
315 if (empty($term)) {
316 wp_die();
317 }
318
319 $args = [
320 'posts_per_page' => -1,
321 'orderby' => 'title',
322 'order' => 'asc',
323 'post_type' => 'shop_coupon',
324 'post_status' => 'publish',
325 's' => $term,
326 ];
327 $coupons = get_posts($args);
328
329 $discount_types = wc_get_coupon_types();
330 $fetched_coupons = [];
331
332 if ($coupons) {
333 foreach ($coupons as $coupon) {
334 $discount_type = get_post_meta($coupon->ID, 'discount_type', true);
335 if (!empty($discount_types[$discount_type])) {
336 $fetched_coupons[$coupon->post_title] = $coupon->post_title;
337 }
338 }
339 }
340 wp_send_json($fetched_coupons);
341 }
342
343
344 /**
345 * This will regenerate the elementor data.
346 * This approach is not for programmers. Logic behind this code is We know position of WPFunnels widget - $path_array e.g. $path_array = array(0). This means
347 * position of WPFunnels widget will be $elementor_data[$key]['elements'][$path_array[0]]. if $path_array = array(0, 0), the position will be
348 * $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]. So we update the WPFunnels widget with new data ($settings) one by one manullay.
349 * This is a very bad approach. We will update the code soon.
350 *
351 * @param $key
352 * @param $elementor_data
353 * @param $settings
354 * @param $path_array
355 *
356 * @return mixed
357 * @since 2.0.0
358 */
359 private function regenerate_elementor_data($key, $elementor_data, $all_settings, $path_array)
360 {
361
362 foreach( $all_settings as $settings ){
363 $path_array_count = count($path_array);
364 if ($path_array_count == 1) {
365 $elementor_data[$key]['elements'][$path_array[0]]['settings']['order_bump_position'] = $settings['position'];
366 $elementor_data[$key]['elements'][$path_array[0]]['settings']['order_bump_layout'] = $settings['selectedStyle'];
367 $elementor_data[$key]['elements'][$path_array[0]]['settings']['order_bump_image']['url'] = $settings['productImage']['url'];
368 $elementor_data[$key]['elements'][$path_array[0]]['settings']['order_bump_image']['id'] = $settings['productImage']['id'];
369 $elementor_data[$key]['elements'][$path_array[0]]['settings']['order_bump_checkbox_label'] = $settings['checkBoxLabel'];
370 $elementor_data[$key]['elements'][$path_array[0]]['settings']['settings']['order_bump_product_detail_header'] = $settings['highLightText'];
371 $elementor_data[$key]['elements'][$path_array[0]]['settings']['order_bump_product_detail'] = $settings['productDescriptionText'];
372 $elementor_data[$key]['elements'][$path_array[0]]['settings']['order_bump'] = $settings['isEnabled'];
373 } elseif ($path_array_count == 2) {
374 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['settings']['order_bump_position'] = $settings['position'];
375 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['settings']['order_bump_layout'] = $settings['selectedStyle'];
376 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['settings']['order_bump_image']['url'] = $settings['productImage']['url'];
377 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['settings']['order_bump_image']['id'] = $settings['productImage']['id'];
378 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['settings']['order_bump_checkbox_label'] = $settings['checkBoxLabel'];
379 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['settings']['order_bump_product_detail_header'] = $settings['highLightText'];
380 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['settings']['order_bump_product_detail'] = $settings['productDescriptionText'];
381 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['settings']['order_bump'] = $settings['isEnabled'];
382 } elseif ($path_array_count == 3) {
383 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['settings']['order_bump_position'] = $settings['position'];
384 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['settings']['order_bump_layout'] = $settings['selectedStyle'];
385 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['settings']['order_bump_image']['url'] = $settings['productImage']['url'];
386 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['settings']['order_bump_image']['id'] = $settings['productImage']['id'];
387 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['settings']['order_bump_checkbox_label'] = $settings['checkBoxLabel'];
388 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['settings']['order_bump_product_detail_header'] = $settings['highLightText'];
389 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['settings']['order_bump_product_detail'] = $settings['productDescriptionText'];
390 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['settings']['order_bump'] = $settings['isEnabled'];
391 } elseif ($path_array_count == 4) {
392 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['elements'][$path_array[3]]['settings']['order_bump_position'] = $settings['position'];
393 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['elements'][$path_array[3]]['settings']['order_bump_layout'] = $settings['selectedStyle'];
394 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['elements'][$path_array[3]]['settings']['order_bump_image']['url'] = $settings['productImage']['url'];
395 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['elements'][$path_array[3]]['settings']['order_bump_image']['id'] = $settings['productImage']['id'];
396 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['elements'][$path_array[3]]['settings']['order_bump_checkbox_label'] = $settings['checkBoxLabel'];
397 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['elements'][$path_array[3]]['settings']['order_bump_product_detail_header'] = $settings['highLightText'];
398 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['elements'][$path_array[3]]['settings']['order_bump_product_detail'] = $settings['productDescriptionText'];
399 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['elements'][$path_array[3]]['settings']['order_bump'] = $settings['isEnabled'];
400 } elseif ($path_array_count == 5) {
401 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['elements'][$path_array[3]]['elements'][$path_array[4]]['settings']['order_bump_position'] = $settings['position'];
402 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['elements'][$path_array[3]]['elements'][$path_array[4]]['settings']['order_bump_layout'] = $settings['selectedStyle'];
403 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['elements'][$path_array[3]]['elements'][$path_array[4]]['settings']['order_bump_image']['url'] = $settings['productImage']['url'];
404 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['elements'][$path_array[3]]['elements'][$path_array[4]]['settings']['order_bump_image']['id'] = $settings['productImage']['id'];
405 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['elements'][$path_array[3]]['elements'][$path_array[4]]['settings']['order_bump_checkbox_label'] = $settings['checkBoxLabel'];
406 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['elements'][$path_array[3]]['elements'][$path_array[4]]['settings']['order_bump_product_detail_header'] = $settings['highLightText'];
407 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['elements'][$path_array[3]]['elements'][$path_array[4]]['settings']['order_bump_product_detail'] = $settings['productDescriptionText'];
408 $elementor_data[$key]['elements'][$path_array[0]]['elements'][$path_array[1]]['elements'][$path_array[2]]['elements'][$path_array[3]]['elements'][$path_array[4]]['settings']['order_bump'] = $settings['isEnabled'];
409 }
410 }
411 return $elementor_data;
412 }
413
414
415 /**
416 * This hook will trigger once the order bump data is saved from admin
417 *
418 * @param $post_id
419 * @param $settings
420 *
421 * @since 2.0.0
422 */
423 public function update_elementor_data($post_id, $all_settings)
424 {
425 if (Wpfnl_functions::get_builder_type() === 'elementor') {
426 $elementor_data = get_post_meta($post_id, '_elementor_data', true);
427 if ($elementor_data) {
428 if (is_array($elementor_data)) {
429 $elementor_data = $elementor_data;
430 } else {
431 $elementor_data = add_magic_quotes($elementor_data);
432 $elementor_data = json_decode($elementor_data, true);
433 }
434
435
436 $el_data = array();
437 $checkout_widget = null;
438 foreach ($elementor_data as $key => $inner_element) {
439 $checkout_widget = Wpfnl_functions::recursive_multidimensional_ob_array_search_by_value('wpfnl-checkout', $inner_element['elements']);
440 if ($checkout_widget) {
441 $path_array = $checkout_widget['path'];
442
443 if (!$path_array) continue;
444
445 if ($path_array) {
446 $path = '';
447 $widget_settings = $checkout_widget['settings'];
448 $widget_settings['order_bump_checkbox_label'] = 'hukka hua';
449 $regenerated_elementor_data = $this->regenerate_elementor_data($key, $elementor_data, $all_settings, $path_array);
450 update_post_meta($post_id, '_elementor_data', $regenerated_elementor_data);
451 }
452 break;
453 }
454 }
455 }
456 }
457 }
458
459
460 }
461