PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.3.2
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.3.2
2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 1.7.9 1.8.1 All 42 releases
sellkit / includes / elementor / modules / checkout / classes / helper.php

helper.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster 1.3.2, at includes/elementor/modules/checkout/classes/helper.php

711 lines 22.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Sellkit\Elementor\Modules\Checkout\Classes;
4
5 defined( 'ABSPATH' ) || exit;
6
7 use Elementor\Plugin as Elementor;
8
9 /**
10 * Helper class.
11 *
12 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
13 * @SuppressWarnings(PHPMD.NPathComplexity)
14 * @since 1.1.0
15 */
16 class Helper {
17
18 /**
19 * Instance of this class.
20 *
21 * @since NEXT
22 * @access public
23 * @var Checkout
24 */
25 public static $instance;
26
27 /**
28 * Provides access to a single instance of a module using the singleton pattern.
29 *
30 * @since NEXT
31 * @return object
32 */
33 public static function instance() {
34 if ( null === self::$instance ) {
35 self::$instance = new self();
36 }
37
38 return self::$instance;
39 }
40
41 /**
42 * Default sellkit checkout widget billing fields.
43 *
44 * @return array
45 * @since 1.1.0
46 */
47 public function billing_fields() {
48 return [
49 'billing_first_name' => esc_html__( 'Billing First Name', 'sellkit' ),
50 'billing_last_name' => esc_html__( 'Billing Last Name', 'sellkit' ),
51 'billing_address_1' => esc_html__( 'Billing Address 1', 'sellkit' ),
52 'billing_address_2' => esc_html__( 'Billing Address 2', 'sellkit' ),
53 'billing_city' => esc_html__( 'Billing City', 'sellkit' ),
54 'billing_postcode' => esc_html__( 'Billing Postal Code', 'sellkit' ),
55 'billing_country' => esc_html__( 'Billing Country', 'sellkit' ),
56 'billing_state' => esc_html__( 'Billing State', 'sellkit' ),
57 'billing_email' => esc_html__( 'Billing State', 'sellkit' ),
58 ];
59 }
60
61 /**
62 * Default sellkit checkout widget shipping fields.
63 *
64 * @return array
65 * @since 1.1.0
66 */
67 public function shipping_fields() {
68 return [
69 'shipping_first_name' => esc_html__( 'shipping First Name', 'sellkit' ),
70 'shipping_last_name' => esc_html__( 'shipping Last Name', 'sellkit' ),
71 'shipping_address_1' => esc_html__( 'shipping Address 1', 'sellkit' ),
72 'shipping_address_2' => esc_html__( 'shipping Address 2', 'sellkit' ),
73 'shipping_city' => esc_html__( 'shipping City', 'sellkit' ),
74 'shipping_postcode' => esc_html__( 'shipping Postal Code', 'sellkit' ),
75 'shipping_country' => esc_html__( 'shipping Country', 'sellkit' ),
76 'shipping_state' => esc_html__( 'shipping State', 'sellkit' ),
77 ];
78 }
79
80 /**
81 * Gets user defined fields and return array of slug.
82 *
83 * @param array $user_defined_fields widget fields.
84 * @param string $type billing/shipping field.
85 * @return array
86 * @since 1.1.0
87 */
88 public function get_user_defined_fields_slug( $user_defined_fields, $type ) {
89 $fields = [];
90
91 foreach ( $user_defined_fields as $field ) {
92 $fields[] = $field[ $type ];
93 }
94
95 return $fields;
96 }
97
98 /**
99 * Assign user defined values to each field.
100 *
101 * @param array $default_fields : default woocommerce fields.
102 * @param array $widget_fields : fields get added by user from widget option.
103 * @param string $type : shipping / billing.
104 * @param array $settings : whole widget settings.
105 * @return array
106 * @since 1.1.0
107 * @SuppressWarnings(PHPMD.NPathComplexity)
108 */
109 public function assign_settings_per_field( $default_fields, $widget_fields, $type, $settings ) {
110 // Assign user properties.
111 foreach ( $widget_fields as $key => $field ) {
112 $field_slug = $field[ $type . '_list_field' ];
113 $is_custom = false;
114
115 // IF field is a custom field we replace key with id. So it will be unique.
116 // !important: user has to define id for each field using custom id field.
117 if ( 'custom_role' === $field_slug ) {
118 // Create billing_bla_bla & shipping_bla_bla for custom field.
119 $field_slug = $type . '_' . $field['_id'];
120
121 if ( ! empty( $field[ $type . '_custom_id' ] ) ) {
122 $field_slug = $type . '_' . $field[ $type . '_custom_id' ];
123 }
124
125 $is_custom = true;
126 }
127
128 $default_fields[ $field_slug ]['label'] = $field[ $type . '_list_placeholder' ];
129 $default_fields[ $field_slug ]['class'] = explode( ' ', $field[ $type . '_list_class' ] ); // phpcs:ignore
130 $default_fields[ $field_slug ]['class'][] = $field[ $type . '_width' ];
131 $default_fields[ $field_slug ]['class'][] = 'sellkit-widget-checkout-fields';
132 $default_fields[ $field_slug ]['class'][] = 'sellkit-checkout-fields-validation-' . $field[ $type . '_list_validation' ];
133 $default_fields[ $field_slug ]['required'] = ( 'yes' === $field[ $type . '_list_required' ] ) ? true : false;
134 $default_fields[ $field_slug ]['clear'] = ( 'yes' === $field[ $type . '_list_clear' ] ) ? true : false;
135 $default_fields[ $field_slug ]['priority'] = ( $key + 1 ) * 10;
136 // Widget defined fields are local. third party plugins's fields are not.
137 $default_fields[ $field_slug ]['local'] = true;
138
139 // Append postal code autocomplete class.
140 if (
141 array_key_exists( 'state_lookup_by_postcode', $settings )
142 && 'yes' === $settings['state_lookup_by_postcode']
143 && ( 'billing_postcode' === $field_slug || 'shipping_postcode' === $field_slug )
144 ) {
145 $default_fields[ $field_slug ]['class'][] = 'post_code_autocomplete';
146 }
147
148 // IF not custom field, go next.
149 if ( false === $is_custom ) {
150 continue;
151 }
152
153 $default_fields = $this->custom_field_setup( $field, $type, $field_slug, $default_fields );
154 }
155
156 foreach ( $default_fields as $key => $details ) {
157 if ( ! array_key_exists( 'local', $details ) || false === $details['local'] ) {
158 $default_fields[ $key ]['priority'] = 500;
159 }
160 }
161
162 return $default_fields;
163 }
164
165 /**
166 * Setup custom field options and assign required things to custom fields.
167 * also helps to reduce assign_settings_per_field method complexity.
168 *
169 * @param array $field html string of checkout fields.
170 * @param string $type type of checkout fields.
171 * @param string $field_slug unique sluf per field.
172 * @param array $default_fields default checkout fields.
173 * @return array
174 * @since 1.1.0
175 */
176 private function custom_field_setup( $field, $type, $field_slug, $default_fields ) {
177 $tag = $field[ $type . '_custom_type' ];
178
179 // Set default options.
180 $default_fields[ $field_slug ]['class'][] = ( $field[ $type . '_width' ] ) ? $field[ $type . '_width' ] : 'w-100';
181 $default_fields[ $field_slug ]['type'] = ( $field[ $type . '_custom_type' ] ) ? $field[ $type . '_custom_type' ] : 'text';
182 $default_fields[ $field_slug ]['default'] = $field[ $type . '_custom_value' ];
183
184 // Convert select to multiselect.
185 if ( 'multiselect' === $tag ) {
186 $default_fields[ $field_slug ]['type'] = 'multiselect';
187 }
188
189 // Assign one extra class to style radio group wrapper.
190 if ( 'radio' === $tag ) {
191 $default_fields[ $field_slug ]['type'] = 'radio';
192 $default_fields[ $field_slug ]['class'][] = 'radio-group-wrapper';
193 $default_fields[ $field_slug ]['mode'] = $field[ $type . '_radio_field_mode' ];
194 }
195
196 // Check if checkbox must be checked on default.
197 if ( 'checkbox' === $tag ) {
198 $default_fields[ $field_slug ]['checked'] = $field[ $type . '_checkbox_custom_value' ];
199 }
200
201 // Assign options.
202 $tags = [ 'select', 'multiselect', 'radio' ];
203 if ( in_array( $tag, $tags, true ) ) {
204 $options = $this->convert_value_to_array( $field[ $type . '_custom_options' ] );
205
206 $default_fields[ $field_slug ]['options'] = $options;
207 }
208
209 return $default_fields;
210 }
211
212 /**
213 * Gets widget options string and convert it to array to be used for select / radio and etc.
214 *
215 * @param string $value string of options.
216 * @return array
217 * @since 1.1.0
218 */
219 private function convert_value_to_array( $value ) {
220 $array = [];
221 $values = preg_split( '/\r\n|\r|\n/', $value );
222
223 foreach ( $values as $option ) {
224 $split = explode( ':', $option );
225 $array[ $split[0] ] = $split[1];
226 }
227
228 return $array;
229 }
230
231 /**
232 * Is used to validate user defined custom fields after pressing place order button at checkout page.
233 * Validate both custom shipping and billing fields at once if exists.
234 *
235 * @uses sellkit_Core\Raven\Modules\Checkout\Classes\Global_Hooks::validate_user_defined_fields
236 * @param array $shipping_fields custom shipping fields.
237 * @param array $billing_fields custom billing fields.
238 * @return void
239 * @since 1.1.0
240 */
241 public function validate_user_defined_fields( $shipping_fields, $billing_fields ) {
242 foreach ( $shipping_fields as $data ) {
243 // Escape all if cart does not need shipping information.
244 if ( ! WC()->cart->needs_shipping() ) {
245 continue;
246 }
247
248 $final_name = $data['shipping_list_field'];
249
250 if ( 'custom_role' === $final_name ) {
251 $final_name = 'shipping_' . $data['_id'];
252
253 if ( ! empty( $data['shipping_custom_id'] ) ) {
254 $id = $data['shipping_custom_id'];
255 $final_name = "shipping_$id";
256 }
257 }
258
259 // Get $name variable from label or placeholder and/or id to be used in validate error.
260 $label = $this->create_name_for_errors( $data, __( 'Shipping', 'sellkit' ), 'shipping', $final_name );
261
262 // Field value.
263 $value = filter_input( INPUT_POST, $final_name, FILTER_DEFAULT );
264
265 // #1. Required validation.
266 $is_required = ( ! empty( $data['shipping_list_required'] ) && 'yes' === $data['shipping_list_required'] ) ? true : false;
267
268 if ( true === $is_required && ( ! array_key_exists( $final_name, $_POST ) || empty( $value ) ) ) { // phpcs:ignore
269 /* translators: %s field_name. */
270 wc_add_notice( sprintf( __( '%s is a required field.', 'sellkit' ), '<strong>' . $label . '</strong>' ), 'error' );
271 }
272
273 // #2. Phone validation.
274 $this->phone_validation( $data, $final_name, 'shipping', $label );
275
276 // #3. Postcode validation.
277 $this->postcode_validation( $data, $final_name, 'shipping', $label );
278 }
279
280 foreach ( $billing_fields as $data ) {
281 $final_name = $data['billing_list_field'];
282
283 if ( 'custom_role' !== $final_name ) {
284 continue;
285 }
286
287 $final_name = 'billing_' . $data['_id'];
288
289 if ( ! empty( $data['billing_custom_id'] ) ) {
290 $id = $data['billing_custom_id'];
291 $final_name = "billing_$id";
292 }
293
294 // Get $name variable from label or placeholder and/or id to be used in validate error.
295 $label = $this->create_name_for_errors( $data, __( 'Billing', 'sellkit' ), 'billing', $final_name );
296
297 // Field value.
298 $value = filter_input( INPUT_POST, $final_name, FILTER_DEFAULT );
299
300 // #1. Required validation.
301 $is_required = ( 'yes' === $data['billing_list_required'] ) ? true : false;
302
303 if ( true === $is_required && empty( $final_name ) ) { // phpcs:ignore
304 /* translators: %s field_name. */
305 wc_add_notice( sprintf( __( '%s is a required field.', 'sellkit' ), '<strong>' . $label . '</strong>' ), 'error' );
306 }
307
308 // #2. phone validation.
309 $this->phone_validation( $data, $final_name, 'billing', $label );
310
311 // #3. Postcode validation.
312 $this->postcode_validation( $data, $final_name, 'billing', $label );
313 }
314
315 // Validate main checkout email.
316 $email = filter_input( INPUT_POST, 'billing_email', FILTER_SANITIZE_EMAIL );
317 $valid = filter_var( $email, FILTER_VALIDATE_EMAIL );
318
319 if ( ! $email ) {
320 /* translators: %s field_name. */
321 wc_add_notice( sprintf( __( '%s is a required field.', 'sellkit' ), '<strong>' . __( 'Email address', 'sellkit' ) . '</strong>' ), 'error' );
322 return;
323 }
324
325 if ( ! $valid ) {
326 /* translators: %s field_name. */
327 wc_add_notice( sprintf( __( '%s is not a valid email.', 'sellkit' ), '<strong>' . __( 'Email address', 'sellkit' ) . '</strong>' ), 'error' );
328 }
329 }
330
331 /**
332 * Create name for each field to be used in errors text.
333 *
334 * @param array $data field data.
335 * @param string $display_type shipping / billing with text-domain.
336 * @param string $type shipping / billing.
337 * @param string $final_name field key.
338 * @return string
339 * @since 1.1.0
340 */
341 private function create_name_for_errors( $data, $display_type, $type, $final_name ) {
342 $name = $data[ $type . '_list_placeholder' ];
343
344 if ( empty( $data[ $type . '_list_placeholder' ] ) ) {
345 $name = $data[ $type . '_list_label' ];
346 }
347
348 $label = $display_type . ' ' . $name;
349
350 if ( empty( $name ) ) {
351 $label = $final_name;
352 }
353
354 return $label;
355 }
356
357 /**
358 * Validate phone number using woocommerce way.
359 *
360 * @see woocommerce/includes/class-wc-validation.php
361 * @param array $data widget field data.
362 * @param string $final_name key of field in checkout form.
363 * @param string $type shipping/billing.
364 * @param string $label field label for error.
365 * @return boolean
366 * @since 1.1.0
367 */
368 private function phone_validation( $data, $final_name, $type, $label ) {
369 if ( ! array_key_exists( $type . '_list_validation', $data ) || 'phone' !== $data[ $type . '_list_validation' ] ) {
370 return true;
371 }
372
373 $phone = filter_input( INPUT_POST, $final_name, FILTER_DEFAULT );
374 $valid = \WC_Validation::is_phone( $phone );
375
376 if ( ! $valid ) {
377 /* translators: %s field_name. */
378 wc_add_notice( sprintf( __( '%s is not a valid phone.', 'sellkit' ), '<strong>' . $label . '</strong>' ), 'error' );
379 }
380 }
381
382 /**
383 * Validate postcode using woocommerce way.
384 *
385 * @param array $data field data.
386 * @param string $final_name field key in checkout form.
387 * @param string $type shipping / billing.
388 * @param string $label field label to be used in error text.
389 * @return void
390 * @since 1.1.0
391 */
392 private function postcode_validation( $data, $final_name, $type, $label ) {
393 if ( ! array_key_exists( $type . '_list_validation', $data ) || 'postcode' !== $data[ $type . '_list_validation' ] ) {
394 return;
395 }
396
397 $postcode = filter_input( INPUT_POST, $final_name, FILTER_DEFAULT );
398 $country = filter_input( INPUT_POST, $type . '_country' );
399 $postcode = wc_format_postcode( $postcode, $country );
400 $valid = \WC_Validation::is_postcode( $postcode, $country );
401
402 if ( ! $valid ) {
403 /* translators: %s field_name. */
404 wc_add_notice( sprintf( __( '%s is not a valid postcode.', 'sellkit' ), '<strong>' . $label . '</strong>' ), 'error' );
405 }
406 }
407
408 /**
409 * Is used to save user defined custom fields in database for the current order id.
410 * save both billing and shipping custom fields at once if exists.
411 *
412 * @uses sellkit_Core\Raven\Modules\Checkout\Classes\Global_Hooks::save_user_defined_fields
413 * @param array $shipping_fields custom shipping fields.
414 * @param array $billing_fields custom billing fields.
415 * @param int $order_id order id.
416 * @return void
417 * @since 1.1.0
418 */
419 public function save_user_defined_fields( $shipping_fields, $billing_fields, $order_id ) {
420 // We store saved fields in this array and save keys as a meta for this order, to use it later.
421 $saved_fields = [];
422
423 foreach ( $shipping_fields as $field ) {
424 if ( empty( $field['shipping_custom_id'] ) ) {
425 continue;
426 }
427
428 $item = $this->prepare_custom_fields_to_save( $field, 'shipping' );
429 $final_name = $item['key_name'];
430
431 // Field value.
432 $value = sellkit_htmlspecialchars( INPUT_POST, $final_name );
433 if ( 'multiselect' === $field['shipping_custom_type'] ) {
434 $value = filter_input( INPUT_POST, $final_name, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
435 }
436
437 // Some user defined field might be not required ( & empty ). so again we check if field is has value save it.
438 if ( ! empty( $value ) ) { // phpcs:ignore
439 update_post_meta( $order_id, $final_name, sanitize_text_field( $value ) ); // phpcs:ignore
440 array_push( $saved_fields, $item );
441 }
442 }
443
444 foreach ( $billing_fields as $field ) {
445 if ( empty( $field['billing_custom_id'] ) ) {
446 continue;
447 }
448
449 $item = $this->prepare_custom_fields_to_save( $field, 'billing' );
450 $final_name = $item['key_name'];
451
452 // Field value.
453 $value = sellkit_htmlspecialchars( INPUT_POST, $final_name );
454 if ( 'multiselect' === $field['billing_custom_type'] ) {
455 $value = filter_input( INPUT_POST, $final_name, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
456 }
457
458 // Some user defined field might be not required ( & empty ). so again we check if field has value save it.
459 if ( ! empty( $value ) ) { // phpcs:ignore
460 update_post_meta( $order_id, $final_name, sanitize_text_field( $value ) ); // phpcs:ignore
461 array_push( $saved_fields, $item );
462 }
463 }
464
465 // Save id of those custom fields that are validate and has value as an meta for order to be used later.
466 if ( count( $saved_fields ) > 0 ) {
467 update_post_meta( $order_id, 'sellkit_checkout_widget_custom_field_of_order', $saved_fields );
468 }
469 }
470
471 /**
472 * The way to save custom fields names as array for order as meta to be used later.
473 *
474 * @param array $data sent data from checkout.
475 * @param string $type shippin/billing.
476 * @return array
477 * @since 1.1.0
478 */
479 private function prepare_custom_fields_to_save( $data, $type ) {
480 $id = $data[ $type . '_custom_id' ];
481 $final_name = $type . '_' . $id;
482 $item = [
483 'show_in_thank_you' => ( 'yes' === $data[ $type . '_show_in_thankyou' ] ) ? 'yes' : 'no',
484 'show_in_email' => ( 'yes' === $data[ $type . '_show_in_order_mail' ] ) ? 'yes' : 'no',
485 'key_name' => $final_name,
486 ];
487
488 return $item;
489 }
490
491 /**
492 * Convert required fields to optional based on widget options.
493 * will be called after clicking place order button.
494 *
495 * @uses sellkit_Core\Raven\Modules\Checkout\Classes\Global_Hooks::validate_user_defined_fields
496 * @param array $widget_shipping_fields user defined fields in widget.
497 * @param array $widget_billing_fields user defined fields in widget.
498 * @return void
499 * @since 1.1.0
500 */
501 public function make_sure_to_convert_required_field_to_optional( $widget_shipping_fields, $widget_billing_fields ) {
502 add_filter( 'woocommerce_checkout_fields', function( $default_fields ) use ( $widget_shipping_fields, $widget_billing_fields ) {
503 // Make phone field optional at beginning.
504 $default_fields['billing']['billing_phone']['required'] = false;
505 unset( $default_fields['billing']['billing_email'] );
506
507 // Make it optional.
508 foreach ( $widget_billing_fields as $data ) {
509 $slug = $data['billing_list_field'];
510 $is_required = false;
511
512 if ( ! empty( $data['billing_list_required'] ) && 'yes' === $data['billing_list_required'] ) {
513 $is_required = true;
514 }
515
516 $default_fields['billing'][ $slug ]['required'] = $is_required;
517 }
518
519 foreach ( $widget_shipping_fields as $data ) {
520 $slug = $data['shipping_list_field'];
521 $is_required = false;
522
523 if ( ! empty( $data['shipping_list_required'] ) && 'yes' === $data['shipping_list_required'] ) {
524 $is_required = true;
525 }
526
527 $default_fields['shipping'][ $slug ]['required'] = $is_required;
528 }
529
530 return $default_fields;
531 } );
532 }
533
534 /**
535 * Retrieve required widget settings of current checkout widget.
536 *
537 * @param int $post_id page id.
538 * @param string $form_id form id.
539 * @return array
540 * @since 1.1.0
541 */
542 public function retrieve_checkout_widget_settings( $post_id, $form_id ) {
543 $form_meta = Elementor::$instance->documents->get( $post_id )->get_elements_data();
544 $form = self::find_element_recursive( $form_meta, $form_id );
545
546 if ( ! is_array( $form ) || ! array_key_exists( 'settings', $form ) ) {
547 return [];
548 }
549
550 $settings = $form['settings'];
551
552 return $settings;
553 }
554
555 /**
556 * Find elementor element.
557 * Code block copied from sellkit-core directly.
558 *
559 * @param array $elements elements of elementor.
560 * @param string $form_id elementor form id.
561 * @return array
562 * @since 1.1.0
563 */
564 public static function find_element_recursive( $elements, $form_id ) {
565 foreach ( $elements as $element ) {
566 if ( $form_id === $element['id'] ) {
567 return $element;
568 }
569
570 if ( ! empty( $element['elements'] ) ) {
571 $element = self::find_element_recursive( $element['elements'], $form_id );
572
573 if ( $element ) {
574 return $element;
575 }
576 }
577 }
578
579 return false;
580 }
581
582 /**
583 * Hide checkout order product quantity input and show raw quantity.
584 *
585 * @param string $input_html default quantity input.
586 * @param int $quantity product quantity in cart.
587 * @since 1.1.0
588 * @return void
589 *
590 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
591 */
592 public function checkout_order_hidden_quantity( $input_html, $quantity ) {
593 echo sprintf(
594 /** Translators: %s: product quantity */
595 '<strong class="product-quantity"> <i class="fa fa-times" aria-hidden="true"></i>%s</strong>',
596 esc_html( $quantity )
597 );
598 }
599
600 /**
601 * Calculate item discount.
602 *
603 * @param int $id product id.
604 * @param string $type discount type.
605 * @param int $value discount value.
606 * @return int|boolean
607 * @since 1.2.8
608 */
609 public static function calculate_discount( $id, $type, $value ) {
610 if ( false === $type && false === $value ) {
611 return false;
612 }
613
614 $product = wc_get_product( $id );
615 $regular = $product->get_regular_price();
616 $sale = $product->get_sale_price();
617 $price = false;
618
619 if ( strpos( $type, 'sale' ) !== false ) {
620 $discount = ( 'fixed-sale' === $type ) ? floatval( $value ) : ( floatval( $sale ) * floatval( $value ) ) / 100;
621 $discount = floatval( $sale ) - $discount;
622
623 return ( $discount > 0 ) ? $discount : 0;
624 }
625
626 if ( strpos( $type, 'sale' ) === false ) {
627 $discount = ( 'fixed' === $type ) ? floatval( $value ) : ( floatval( $regular ) * floatval( $value ) ) / 100;
628 $discount = floatval( $regular ) - $discount;
629
630 return ( $discount > 0 ) ? $discount : 0;
631 }
632
633 return $price;
634 }
635
636 /**
637 * Add extra js in editor mode.
638 *
639 * @since 1.1.0
640 * @return void
641 */
642 public function editor_mode_extra_js() {
643 ?>
644 <script>
645 jQuery( document ).on( 'mousemove', function() {
646 $( '.sellkit-coupon-toggle' ).off( 'click' ).on( 'click', function() {
647 let direction = 'row';
648 const status = $( '.sellkit-custom-coupon-form' ).css( 'display' );
649 let displayValue = '';
650
651 if ( $( window ).width() < 600 ) {
652 direction = 'column';
653 }
654
655 if ( 'none' === status ) {
656 displayValue = 'inline-flex';
657 } else {
658 displayValue = 'none';
659 }
660
661 $( '.sellkit-custom-coupon-form' ).css( {
662 display: displayValue,
663 flexDirection: direction,
664 } );
665 } );
666 } );
667 </script>
668 <?php
669 }
670
671 /**
672 * Assigning checkout ajax default shipping fields after ajax is sent.
673 *
674 * @param array $post post request.
675 * @param array $clear checkout form data.
676 * @return array
677 * @since 1.2.8
678 */
679 public function assigning_default_ajax_shipping_fields( $post, $clear ) {
680 if ( array_key_exists( 'billing_state', $clear ) ) {
681 $post['state'] = $clear['billing_state'];
682 }
683
684 if ( array_key_exists( 'shipping_country', $clear ) ) {
685 $post['s_country'] = $clear['shipping_country'];
686 }
687
688 if ( array_key_exists( 'shipping_state', $clear ) ) {
689 $post['s_state'] = $clear['shipping_state'];
690 }
691
692 if ( array_key_exists( 'shipping_postcode', $clear ) ) {
693 $post['s_postcode'] = $clear['shipping_postcode'];
694 }
695
696 if ( array_key_exists( 'shipping_city', $clear ) ) {
697 $post['s_city'] = $clear['shipping_city'];
698 }
699
700 if ( array_key_exists( 'shipping_address_1', $clear ) ) {
701 $post['s_address'] = $clear['shipping_address_1'];
702 }
703
704 if ( array_key_exists( 'shipping_address_2', $clear ) ) {
705 $post['s_address_2'] = $clear['shipping_address_2'];
706 }
707
708 return $post;
709 }
710 }
711