PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 13.5.2
Jetpack – WP Security, Backup, Speed, & Growth v13.5.2
12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 14.4.2 14.5.1 14.6.1 14.7.1 14.8.1 14.9.2 15.0.3 15.1.2 15.2.1 15.3.2 15.4.1 15.5.1 15.6.1 15.7.2 15.8.1 15.9.2 16.0.2 16.1.3 16.2-a.5 16.2-a.3 16.1.2 16.2-a.1 16.1.1 16.1 16.1-beta 16.1-beta.2 16.1-beta.3 16.1-a.5 16.1-a.3 16.0.1 16.1-a.1 16.0 16.0-beta 16.0-a.7 16.0-a.5 15.9.1 16.0-a.3 16.0-a.1 15.9 15.9-beta 15.9-a.7 15.9-a.5 15.9-a.3 15.9-a.1 15.8 15.8-beta 15.8-a.7 15.8-a.5 5.2.5 5.3.4 5.4.4 5.5.5 5.6.5 5.7.5 5.8.4 5.9.4 6.0.4 6.1 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.2 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.3 6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.4 6.4.1 6.4.2 6.4.3 6.4.4 6.4.5 6.4.6 6.5 6.5.1 6.5.2 6.5.3 6.5.4 6.6 6.6.1 6.6.2 6.6.3 6.6.4 6.6.5 6.7 6.7.1 6.7.2 6.7.3 6.7.4 6.8 6.8.1 6.8.2 6.8.3 6.8.4 6.8.5 6.9 6.9.1 6.9.2 6.9.3 6.9.4 7.0 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2 7.2.1 7.2.1.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3 7.3.0.1 7.3.1 7.3.1.1 7.3.2 7.3.3 7.3.4 7.3.5 7.4 7.4.1 7.4.2 7.4.3 7.4.4 7.4.5 7.5 7.5.0.1 7.5.1 7.5.2 7.5.3 7.5.4 7.5.5 7.5.6 7.5.7 7.6 7.6.1 7.6.2 7.6.3 7.6.4 7.7 7.7.1 7.7.2 7.7.3 7.7.4 7.7.5 7.7.6 7.8 7.8.1 7.8.2 7.8.3 7.8.4 7.9 7.9.1 7.9.2 7.9.3 7.9.4 8.0 8.0.1 8.0.2 8.0.3 8.1 8.1.1 8.1.2 8.1.3 8.1.4 8.2 8.2.0.1 8.2.1 8.2.2 8.2.3 8.2.4 8.2.5 8.2.6 8.3 8.3.1 8.3.2 8.3.3 8.4 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.5 8.5.1 8.5.2 8.5.3 8.6 8.6.1 8.6.2 8.6.3 8.6.4 8.7 8.7.0.1 8.7.1 8.7.2 8.7.3 8.7.4 8.8 8.8.1 8.8.2 8.8.3 8.8.4 8.8.5 8.9 8.9.1 8.9.2 8.9.3 8.9.4 9.0 9.0.1 9.0.2 9.0.3 9.0.4 9.0.5 9.1 9.1.1 9.1.2 9.1.3 9.2 9.2.1 9.2.2 9.2.3 9.2.4 9.3 9.3.1 9.3.2 9.3.3 9.3.4 9.3.5 9.4 9.4.1 9.4.2 9.4.3 9.4.4 9.5 9.5.1 9.5.2 9.5.3 9.5.4 9.5.5 9.6 9.6.1 9.6.2 9.6.3 9.6.4 9.7 9.7.1 9.7.2 15.7-beta.2 9.7.3 15.7.1 9.8 15.8-a.1 9.8.1 15.8-a.3 9.8.2 2.0.9 9.8.3 2.1.7 9.9 2.2.10 9.9.1 2.3.10 9.9.2 2.4.7 9.9.3 2.5.5 2.6.6 2.7.5 2.8.5 2.9.6 3.0.6 3.1.5 3.2.5 3.3.6 3.4.6 3.5.6 3.6.4 3.7.5 3.8.5 3.9.10 4.0.7 4.1.4 4.2.5 4.3.5 4.4.5 4.5.3 4.6.3 4.7.4 4.8.5 4.9.3 5.0.3 5.1.4 trunk 10.0 10.0.1 10.0.2 10.1 10.1.1 10.1.2 10.2 10.2.1 10.2.2 10.2.3 10.3 10.3.1 10.3.2 10.4 10.4.1 10.4.2 10.5 10.5.1 10.5.2 10.5.3 10.6 10.6.1 10.6.2 10.7 10.7.1 10.7.2 10.8 10.8.1 10.8.2 10.9 10.9.1 10.9.2 10.9.3 11.0 11.0.1 11.0.2 11.1 11.1.1 11.1.2 11.1.3 11.1.4 11.2 11.2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / modules / woocommerce-analytics / classes / class-jetpack-woocommerce-analytics-universal.php
jetpack / modules / woocommerce-analytics / classes Last commit date
class-jetpack-woocommerce-analytics-checkout-flow.php 2 years ago class-jetpack-woocommerce-analytics-my-account.php 2 years ago class-jetpack-woocommerce-analytics-trait.php 2 years ago class-jetpack-woocommerce-analytics-universal.php 2 years ago
class-jetpack-woocommerce-analytics-universal.php
553 lines
1 <?php
2 /**
3 * Jetpack_WooCommerce_Analytics_Universal
4 *
5 * @deprecated 13.3
6 *
7 * @package automattic/jetpack
8 * @author Automattic
9 */
10
11 /**
12 * Bail if accessed directly
13 */
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 /**
19 * Class Jetpack_WooCommerce_Analytics_Universal
20 * Filters and Actions added to Store pages to perform analytics
21 *
22 * @deprecated 13.3
23 */
24 class Jetpack_WooCommerce_Analytics_Universal {
25
26 /**
27 * Trait to handle common analytics functions.
28 */
29 use Jetpack_WooCommerce_Analytics_Trait;
30
31 /**
32 * Jetpack_WooCommerce_Analytics_Universal constructor.
33 *
34 * @deprecated 13.3
35 */
36 public function __construct() {
37 $this->find_cart_checkout_content_sources();
38 $this->additional_blocks_on_cart_page = $this->get_additional_blocks_on_page( 'cart' );
39 $this->additional_blocks_on_checkout_page = $this->get_additional_blocks_on_page( 'checkout' );
40
41 // add to carts from non-product pages or lists -- search, store etc.
42 add_action( 'wp_head', array( $this, 'loop_session_events' ), 2 );
43
44 // Capture cart events.
45 add_action( 'woocommerce_add_to_cart', array( $this, 'capture_add_to_cart' ), 10, 6 );
46
47 add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart' ) );
48 add_action( 'woocommerce_after_mini_cart', array( $this, 'remove_from_cart' ) );
49 add_action( 'wcct_before_cart_widget', array( $this, 'remove_from_cart' ) );
50 add_filter( 'woocommerce_cart_item_remove_link', array( $this, 'remove_from_cart_attributes' ), 10, 2 );
51
52 // Checkout.
53 // Send events after checkout template (shortcode).
54 add_action( 'woocommerce_after_checkout_form', array( $this, 'checkout_process' ) );
55 // Send events after checkout block.
56 add_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_after', array( $this, 'checkout_process' ) );
57
58 // order confirmed.
59 add_action( 'woocommerce_thankyou', array( $this, 'order_process' ), 10, 1 );
60 add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart_via_quantity' ), 10, 1 );
61
62 add_filter( 'woocommerce_checkout_posted_data', array( $this, 'save_checkout_post_data' ), 10, 1 );
63
64 add_action( 'woocommerce_created_customer', array( $this, 'capture_created_customer' ), 10, 2 );
65 }
66
67 /**
68 * On product lists or other non-product pages, add an event listener to "Add to Cart" button click
69 *
70 * @deprecated 13.3
71 */
72 public function loop_session_events() {
73 // Check for previous events queued in session data.
74 if ( is_object( WC()->session ) ) {
75 $data = WC()->session->get( 'wca_session_data' );
76 if ( ! empty( $data ) ) {
77 foreach ( $data as $data_instance ) {
78 $this->record_event(
79 $data_instance['event'],
80 array(
81 'pq' => $data_instance['quantity'],
82 ),
83 $data_instance['product_id']
84 );
85 }
86 // Clear data, now that these events have been recorded.
87 WC()->session->set( 'wca_session_data', '' );
88 }
89 }
90 }
91
92 /**
93 * On the cart page, add an event listener for removal of product click
94 *
95 * @deprecated 13.3
96 */
97 public function remove_from_cart() {
98 $common_props = $this->render_properties_as_js(
99 $this->get_common_properties()
100 );
101
102 // We listen at div.woocommerce because the cart 'form' contents get forcibly
103 // updated and subsequent removals from cart would then not have this click
104 // handler attached.
105 wc_enqueue_js(
106 "jQuery( 'div.woocommerce' ).on( 'click', 'a.remove', function() {
107 var productID = jQuery( this ).data( 'product_id' );
108 var quantity = jQuery( this ).parent().parent().find( '.qty' ).val()
109 var productDetails = {
110 'id': productID,
111 'quantity': quantity ? quantity : '1',
112 };
113 _wca.push( {
114 '_en': 'woocommerceanalytics_remove_from_cart',
115 'pi': productDetails.id,
116 'pq': productDetails.quantity, " .
117 $common_props . '
118 } );
119 } );'
120 );
121 }
122
123 /**
124 * Adds the product ID to the remove product link (for use by remove_from_cart above) if not present
125 *
126 * @deprecated 13.3
127 *
128 * @param string $url Full HTML a tag of the link to remove an item from the cart.
129 * @param string $key Unique Key ID for a cart item.
130 *
131 * @return string
132 */
133 public function remove_from_cart_attributes( $url, $key ) {
134 if ( str_contains( $url, 'data-product_id' ) ) {
135 return $url;
136 }
137
138 $item = WC()->cart->get_cart_item( $key );
139 $product = $item['data'];
140
141 $new_attributes = sprintf(
142 '" data-product_id="%s">',
143 esc_attr( $product->get_id() )
144 );
145
146 $url = str_replace( '">', $new_attributes, $url );
147 return $url;
148 }
149
150 /**
151 * Get the selected shipping option for a cart item. If the name cannot be found in the options table, the method's
152 * ID will be used.
153 *
154 * @deprecated 13.3
155 *
156 * @param string $cart_item_key the cart item key.
157 *
158 * @return mixed|bool
159 */
160 public function get_shipping_option_for_item( $cart_item_key ) {
161 $packages = wc()->shipping()->get_packages();
162 $selected_options = wc()->session->get( 'chosen_shipping_methods' );
163
164 if ( ! is_array( $packages ) || ! is_array( $selected_options ) ) {
165 return false;
166 }
167
168 foreach ( $packages as $package_id => $package ) {
169
170 if ( ! isset( $package['contents'] ) || ! is_array( $package['contents'] ) ) {
171 return false;
172 }
173
174 foreach ( $package['contents'] as $package_item ) {
175 if ( ! isset( $package_item['key'] ) || $package_item['key'] !== $cart_item_key || ! isset( $selected_options[ $package_id ] ) ) {
176 continue;
177 }
178 $selected_rate_id = $selected_options[ $package_id ];
179 $method_key_id = sanitize_text_field( str_replace( ':', '_', $selected_rate_id ) );
180 $option_name = 'woocommerce_' . $method_key_id . '_settings';
181 $option_value = get_option( $option_name );
182 $title = '';
183 if ( is_array( $option_value ) && isset( $option_value['title'] ) ) {
184 $title = $option_value['title'];
185 }
186 if ( ! $title ) {
187 return $selected_rate_id;
188 }
189 return $title;
190 }
191 }
192
193 return false;
194 }
195
196 /**
197 * On the Checkout page, trigger an event for each product in the cart
198 *
199 * @deprecated 13.3
200 */
201 public function checkout_process() {
202 global $post;
203 $checkout_page_id = wc_get_page_id( 'checkout' );
204 $cart = WC()->cart->get_cart();
205
206 $enabled_payment_options = array_filter(
207 WC()->payment_gateways->get_available_payment_gateways(),
208 function ( $payment_gateway ) {
209 if ( ! $payment_gateway instanceof WC_Payment_Gateway ) {
210 return false;
211 }
212
213 return $payment_gateway->is_available();
214 }
215 );
216
217 $enabled_payment_options = array_keys( $enabled_payment_options );
218
219 $is_in_checkout_page = $checkout_page_id === $post->ID ? 'Yes' : 'No';
220 $session = WC()->session;
221 if ( is_object( $session ) ) {
222 $session->set( 'checkout_page_used', true );
223 $session->save_data();
224 }
225
226 foreach ( $cart as $cart_item_key => $cart_item ) {
227 /**
228 * This filter is already documented in woocommerce/templates/cart/cart.php
229 */
230 $product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
231
232 if ( ! $product || ! $product instanceof WC_Product ) {
233 continue;
234 }
235
236 $data = $this->get_cart_checkout_shared_data();
237
238 $data['from_checkout'] = $is_in_checkout_page;
239
240 if ( ! empty( $data['products'] ) ) {
241 unset( $data['products'] );
242 }
243
244 if ( ! empty( $data['shipping_options_count'] ) ) {
245 unset( $data['shipping_options_count'] );
246 }
247
248 $data['pq'] = $cart_item['quantity'];
249
250 $properties = $this->process_event_properties(
251 'woocommerceanalytics_product_checkout',
252 $data,
253 $product->get_id()
254 );
255
256 wc_enqueue_js(
257 "
258 var cartItem_{$cart_item_key}_logged = false;
259 var properties = {$properties};
260 // Check if jQuery is available
261 if ( typeof jQuery !== 'undefined' ) {
262 // This is only triggered on the checkout shortcode.
263 jQuery( document.body ).on( 'init_checkout', function () {
264 if ( true === cartItem_{$cart_item_key}_logged ) {
265 return;
266 }
267 wp.hooks.addAction( 'wcpay.payment-request.availability', 'wcpay', function ( args ) {
268 properties.express_checkout = args.paymentRequestType;
269 } );
270 properties.checkout_page_contains_checkout_block = '0';
271 properties.checkout_page_contains_checkout_shortcode = '1';
272
273 _wca.push( properties );
274 cartItem_{$cart_item_key}_logged = true;
275
276 } );
277 }
278
279 if (
280 typeof wp !== 'undefined' &&
281 typeof wp.data !== 'undefined' &&
282 typeof wp.data.subscribe !== 'undefined'
283 ) {
284 wp.data.subscribe( function () {
285 if ( true === cartItem_{$cart_item_key}_logged ) {
286 return;
287 }
288
289 const checkoutDataStore = wp.data.select( 'wc/store/checkout' );
290 // Ensures we're not in Cart, but in Checkout page.
291 if (
292 typeof checkoutDataStore !== 'undefined' &&
293 checkoutDataStore.getOrderId() !== 0
294 ) {
295 properties.express_checkout = Object.keys( wc.wcBlocksRegistry.getExpressPaymentMethods() );
296 properties.checkout_page_contains_checkout_block = '1';
297 properties.checkout_page_contains_checkout_shortcode = '0';
298
299 _wca.push( properties );
300 cartItem_{$cart_item_key}_logged = true;
301 }
302 } );
303 }
304 "
305 );
306
307 }
308 }
309
310 /**
311 * After the checkout process, fire an event for each item in the order
312 *
313 * @deprecated 13.3
314 *
315 * @param string $order_id Order Id.
316 */
317 public function order_process( $order_id ) {
318 $order = wc_get_order( $order_id );
319
320 if (
321 ! $order
322 || ! $order instanceof WC_Order
323 ) {
324 return;
325 }
326
327 $payment_option = $order->get_payment_method();
328
329 if ( is_object( WC()->session ) ) {
330 $create_account = true === WC()->session->get( 'wc_checkout_createaccount_used' ) ? 'Yes' : 'No';
331 $checkout_page_used = true === WC()->session->get( 'checkout_page_used' ) ? 'Yes' : 'No';
332
333 } else {
334 $create_account = 'No';
335 $checkout_page_used = 'No';
336 }
337
338 $guest_checkout = $order->get_user() ? 'No' : 'Yes';
339
340 $express_checkout = 'null';
341 // When the payment option is woocommerce_payment
342 // See if Google Pay or Apple Pay was used.
343 if ( 'woocommerce_payments' === $payment_option ) {
344 $payment_option_title = $order->get_payment_method_title();
345 if ( 'Google Pay (WooCommerce Payments)' === $payment_option_title ) {
346 $express_checkout = array( 'google_pay' );
347 } elseif ( 'Apple Pay (WooCommerce Payments)' === $payment_option_title ) {
348 $express_checkout = array( 'apple_pay' );
349 }
350 }
351
352 $checkout_page_contains_checkout_block = '0';
353 $checkout_page_contains_checkout_shortcode = '0';
354
355 $order_source = $order->get_created_via();
356 if ( 'store-api' === $order_source ) {
357 $checkout_page_contains_checkout_block = '1';
358 $checkout_page_contains_checkout_shortcode = '0';
359 } elseif ( 'checkout' === $order_source ) {
360 $checkout_page_contains_checkout_block = '0';
361 $checkout_page_contains_checkout_shortcode = '1';
362 }
363
364 // loop through products in the order and queue a purchase event.
365 foreach ( $order->get_items() as $order_item ) {
366 // @phan-suppress-next-line PhanUndeclaredMethodInCallable,PhanUndeclaredMethod -- Checked before being called. See also https://github.com/phan/phan/issues/1204.
367 $product_id = is_callable( array( $order_item, 'get_product_id' ) ) ? $order_item->get_product_id() : -1;
368
369 $order_items = $order->get_items();
370 $order_items_count = 0;
371 if ( is_array( $order_items ) ) {
372 $order_items_count = count( $order_items );
373 }
374 $order_coupons = $order->get_coupons();
375 $order_coupons_count = 0;
376 if ( is_array( $order_coupons ) ) {
377 $order_coupons_count = count( $order_coupons );
378 }
379 $this->record_event(
380 'woocommerceanalytics_product_purchase',
381 array(
382 'oi' => $order->get_order_number(),
383 'pq' => $order_item->get_quantity(),
384 'payment_option' => $payment_option,
385 'create_account' => $create_account,
386 'guest_checkout' => $guest_checkout,
387 'express_checkout' => $express_checkout,
388 'products_count' => $order_items_count,
389 'coupon_used' => $order_coupons_count,
390 'order_value' => $order->get_total(),
391 'from_checkout' => $checkout_page_used,
392 'checkout_page_contains_checkout_block' => $checkout_page_contains_checkout_block,
393 'checkout_page_contains_checkout_shortcode' => $checkout_page_contains_checkout_shortcode,
394 ),
395 $product_id
396 );
397 }
398 }
399
400 /**
401 * Listen for clicks on the "Update Cart" button to know if an item has been removed by
402 * updating its quantity to zero
403 *
404 * @deprecated 13.3
405 */
406 public function remove_from_cart_via_quantity() {
407 $common_props = $this->render_properties_as_js(
408 $this->get_common_properties()
409 );
410
411 wc_enqueue_js(
412 "
413 jQuery( 'button[name=update_cart]' ).on( 'click', function() {
414 var cartItems = jQuery( '.cart_item' );
415 cartItems.each( function( item ) {
416 var qty = jQuery( this ).find( 'input.qty' );
417 if ( qty && qty.val() === '0' ) {
418 var productID = jQuery( this ).find( '.product-remove a' ).data( 'product_id' );
419 _wca.push( {
420 '_en': 'woocommerceanalytics_remove_from_cart',
421 'pi': productID, " .
422 $common_props . '
423 } );
424 }
425 } );
426 } );'
427 );
428 }
429
430 /**
431 * Gets the inner blocks of a block.
432 *
433 * @deprecated 13.3
434 *
435 * @param array $inner_blocks The inner blocks.
436 *
437 * @return array
438 */
439 private function get_inner_blocks( $inner_blocks ) {
440 $block_names = array();
441 if ( ! empty( $inner_blocks['blockName'] ) ) {
442 $block_names[] = $inner_blocks['blockName'];
443 }
444 if ( isset( $inner_blocks['innerBlocks'] ) && is_array( $inner_blocks['innerBlocks'] ) ) {
445 $block_names = array_merge( $block_names, $this->get_inner_blocks( $inner_blocks['innerBlocks'] ) );
446 }
447 return $block_names;
448 }
449
450 /**
451 * Track adding items to the cart.
452 *
453 * @deprecated 13.3
454 *
455 * @param string $cart_item_key Cart item key.
456 * @param int $product_id Product added to cart.
457 * @param int $quantity Quantity added to cart.
458 * @param int $variation_id Product variation.
459 * @param array $variation Variation attributes..
460 * @param array $cart_item_data Other cart data.
461 */
462 public function capture_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
463 $referer_postid = isset( $_SERVER['HTTP_REFERER'] ) ? url_to_postid( esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) ) : 0;
464 // if the referring post is not a product OR the product being added is not the same as post.
465 // (eg. related product list on single product page) then include a product view event.
466 $product_by_referer_postid = wc_get_product( $referer_postid );
467 if ( ! $product_by_referer_postid instanceof WC_Product || (int) $product_id !== $referer_postid ) {
468 $this->capture_event_in_session_data( $product_id, $quantity, 'woocommerceanalytics_product_view' );
469 }
470 // add cart event to the session data.
471 $this->capture_event_in_session_data( $product_id, $quantity, 'woocommerceanalytics_add_to_cart' );
472 }
473
474 /**
475 * Track in-session data.
476 *
477 * @deprecated 13.3
478 *
479 * @param int $product_id Product ID.
480 * @param int $quantity Quantity.
481 * @param string $event Fired event.
482 */
483 public function capture_event_in_session_data( $product_id, $quantity, $event ) {
484
485 $product = wc_get_product( $product_id );
486 if ( ! $product instanceof WC_Product ) {
487 return;
488 }
489
490 $quantity = ( 0 === $quantity ) ? 1 : $quantity;
491
492 // check for existing data.
493 if ( is_object( WC()->session ) ) {
494 $data = WC()->session->get( 'wca_session_data' );
495 if ( empty( $data ) || ! is_array( $data ) ) {
496 $data = array();
497 }
498 } else {
499 $data = array();
500 }
501
502 // extract new event data.
503 $new_data = array(
504 'event' => $event,
505 'product_id' => (string) $product_id,
506 'quantity' => (string) $quantity,
507 );
508
509 // append new data.
510 $data[] = $new_data;
511
512 WC()->session->set( 'wca_session_data', $data );
513 }
514
515 /**
516 * Save createaccount post data to be used in $this->order_process.
517 *
518 * @deprecated 13.3
519 *
520 * @param array $data post data from the checkout page.
521 *
522 * @return array
523 */
524 public function save_checkout_post_data( array $data ) {
525 $session = WC()->session;
526 if ( is_object( $session ) ) {
527 if ( isset( $data['createaccount'] ) && ! empty( $data['createaccount'] ) ) {
528 $session->set( 'wc_checkout_createaccount_used', true );
529 $session->save_data();
530 }
531 }
532 return $data;
533 }
534
535 /**
536 * Capture the create account event. Similar to save_checkout_post_data but works with Store API.
537 *
538 * @deprecated 13.3
539 *
540 * @param int $customer_id Customer ID.
541 * @param array $new_customer_data New customer data.
542 */
543 public function capture_created_customer( $customer_id, $new_customer_data ) {
544 $session = WC()->session;
545 if ( is_object( $session ) ) {
546 if ( str_contains( $new_customer_data['source'], 'store-api' ) ) {
547 $session->set( 'wc_checkout_createaccount_used', true );
548 $session->save_data();
549 }
550 }
551 }
552 }
553