PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.2.9
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.2.9
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 / funnel / steps / checkout.php

checkout.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster 1.2.9, at includes/funnel/steps/checkout.php

262 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Sellkit\Funnel\Steps;
4
5 use Sellkit\Funnel\Analytics\Data_Updater;
6 use Sellkit_Funnel;
7
8 defined( 'ABSPATH' ) || die();
9
10 /**
11 * Class Sellkit_Checkout.
12 *
13 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
14 * @since 1.1.0
15 */
16 class Checkout {
17
18 /**
19 * Funnel instance.
20 *
21 * @var Sellkit_Funnel Funnel object.
22 * @since 1.1.0
23 */
24 public $funnel;
25
26 /**
27 * Checkout constructor.
28 *
29 * @since 1.1.0
30 */
31 public function __construct() {
32 $this->update_funnel_data();
33
34 if ( empty( $this->funnel->funnel_id ) ) {
35 return;
36 }
37
38 add_action( 'woocommerce_checkout_create_order', [ $this, 'save_order_custom_meta_data' ], 10 );
39 add_action( 'woocommerce_after_order_notes', [ $this, 'custom_checkout_hidden_fields' ], 10 );
40 add_action( 'wc_ajax_update_order_review', [ $this, 'do_actions' ] );
41 }
42
43 /**
44 * Adding items to the checkout page.
45 *
46 * @since 1.1.0
47 * @SuppressWarnings(PHPMD.NPathComplexity)
48 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
49 */
50 public function do_actions() {
51 // These variables should be used in checkout builder widget.
52 $funnel_data = $this->funnel->current_step_data;
53 $optimization_data = ! empty( $funnel_data['data']['optimization'] ) ? $funnel_data['data']['optimization'] : '';
54 $redirect_url = ! empty( $optimization_data['closed_checkout_redirect_url'] ) ? $optimization_data['closed_checkout_redirect_url'] : site_url();
55
56 add_action( 'template_redirect', function () use ( $funnel_data ) {
57 $product_settings = ! empty( $funnel_data['data']['products']['settings'] ) ? $funnel_data['data']['products']['settings'] : '';
58 $bump_data = ! empty( $funnel_data['bump'] ) ? $funnel_data['bump'] : '';
59
60 if ( ! empty( $product_settings ) ) {
61 set_query_var( 'funnel_product_settings', $product_settings );
62 }
63
64 if ( ! empty( $bump_data ) ) {
65 $bump_data = $this->get_valid_bumps( $bump_data );
66
67 set_query_var( 'bump_data', $bump_data );
68 }
69 } );
70
71 if (
72 ! empty( $optimization_data['expiration_date'] ) &&
73 ! empty( $optimization_data['expire_checkout_coupons_switch'] ) &&
74 time() > $optimization_data['expiration_date'] &&
75 'on' === $optimization_data['expire_checkout_coupons_switch']
76 ) {
77 wp_redirect( $redirect_url ); // phpcs:ignore
78 exit;
79 }
80
81 if ( empty( $funnel_data ) ) {
82 return;
83 }
84
85 if (
86 empty( $funnel_data['data']['products']['list'] ) ||
87 ! is_array( $funnel_data['data']['products']['list'] )
88 ) {
89 return;
90 }
91
92 $funnel_products = $funnel_data['data']['products']['list'];
93
94 $action = sellkit_htmlspecialchars( INPUT_GET, 'wc-ajax' );
95
96 if ( 'update_order_review' !== $action && 'checkout' !== $action ) {
97 wc()->cart->empty_cart();
98
99 foreach ( $funnel_products as $product_id => $product ) {
100 $quantity = ! empty( $product['quantity'] ) ? $product['quantity'] : 1;
101
102 wc()->cart->add_to_cart( $product_id, $quantity );
103 }
104 }
105
106 if ( empty( $optimization_data ) ) {
107 return;
108 }
109
110 if ( self::apply_coupon_validation( $optimization_data ) ) {
111 foreach ( $optimization_data['auto_apply_coupons'] as $auto_apply_coupon ) {
112 wc()->cart->add_discount( get_the_title( $auto_apply_coupon['value'] ) );
113 }
114
115 wc_clear_notices();
116 }
117 }
118
119 /**
120 * Saving the funnel data to the order.
121 *
122 * @since 1.1.0
123 * @param object $order Order object.
124 */
125 public function save_order_custom_meta_data( $order ) {
126 if ( empty( $this->funnel->next_step_data ) ) {
127 return;
128 }
129
130 $analytics_updater = new Data_Updater();
131
132 $analytics_updater->set_funnel_id( $this->funnel->funnel_id );
133 $analytics_updater->add_new_order_log( $order );
134
135 $order->update_meta_data( 'sellkit_funnel_next_step_data', $this->funnel->next_step_data );
136 }
137
138 /**
139 * Adding page id field to the inputs.
140 *
141 * @since 1.1.0
142 */
143 public function custom_checkout_hidden_fields() {
144 woocommerce_form_field( 'sellkit_current_page_id', [
145 'type' => 'hidden',
146 'class' => [ 'hidden form-row-wide' ],
147 ], get_the_ID() );
148 }
149
150 /**
151 * Check If a coupon should be applied.
152 *
153 * @since 1.1.0
154 * @param array $optimization_data Optimization data.
155 */
156 public static function apply_coupon_validation( $optimization_data ) {
157 if (
158 empty( $optimization_data['auto_apply_coupons_switch'] ) ||
159 'on' !== $optimization_data['auto_apply_coupons_switch']
160 ) {
161 return false;
162 }
163
164 if (
165 empty( $optimization_data['auto_apply_coupons'] ) ||
166 ! is_array( $optimization_data['auto_apply_coupons'] )
167 ) {
168 return false;
169 }
170
171 return true;
172 }
173
174 /**
175 * Calculate total discount.
176 *
177 * @since 1.1.0
178 * @param string $total Total.
179 * @param string $value Value.
180 * @param string $type Type.
181 * @return false|float|int|mixed
182 */
183 public function calculate_discount( $total, $value, $type ) {
184 if ( empty( $value ) || 1 > $value ) {
185 return 0;
186 }
187
188 if ( strpos( $type, 'fixed' ) !== false ) {
189 return $value;
190 }
191
192 if ( strpos( $type, 'percentage' ) !== false ) {
193 return ( floatval( $total ) * floatval( $value ) ) / 100;
194 }
195
196 return 0;
197 }
198
199 /**
200 * Gets extracted post data.
201 *
202 * @since 1.1.0
203 * @param string $post_data Post data.
204 */
205 private function get_current_page_id_by_post_data( $post_data ) {
206 $vars = explode( '&', $post_data );
207 $extracted_data = [];
208
209 foreach ( $vars as $value ) {
210 $result = explode( '=', urldecode( $value ) );
211 $extracted_data[ $result[0] ] = $result[1];
212 }
213
214 if ( empty( $extracted_data['sellkit_current_page_id'] ) ) {
215 return null;
216 }
217
218 return $extracted_data['sellkit_current_page_id'];
219 }
220
221 /**
222 * Checks all bumps and return data.
223 *
224 * @since 1.1.0
225 * @param array $bump_data Bump data.
226 */
227 public function get_valid_bumps( $bump_data ) {
228 $valid_bumps = [];
229
230 foreach ( $bump_data as $bump ) {
231 $conditions = ! empty( $bump['data']['conditions'] ) ? $bump['data']['conditions'] : '';
232
233 if ( ! empty( $conditions ) && empty( sellkit_conditions_validation( $conditions ) ) ) {
234 continue;
235 }
236
237 $valid_bumps[] = $bump;
238 }
239
240 return $valid_bumps;
241 }
242
243 /**
244 * Updates funnel data.
245 *
246 * @since 1.1.0
247 */
248 public function update_funnel_data() {
249 $action = sellkit_htmlspecialchars( INPUT_GET, 'wc-ajax' );
250
251 if ( 'update_order_review' === $action ) {
252 $post_data = sellkit_htmlspecialchars( INPUT_POST, 'post_data' );
253
254 $this->funnel = new Sellkit_Funnel( $this->get_current_page_id_by_post_data( $post_data ) );
255
256 return;
257 }
258
259 $this->funnel = Sellkit_Funnel::get_instance();
260 }
261 }
262