← All changes
|
modules/woocommerce-analytics/classes/class-jetpack-woocommerce-analytics-universal.php
+242
-165
12.6.4
→
16.3-a.7
View file →
| @@ -1,24 +1,26 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Jetpack_WooCommerce_Analytics_Universal |
| 4 | 4 | * |
| 5 | + * @deprecated 13.3 | |
| 6 | + * | |
| 5 | 7 | * @package automattic/jetpack |
| 6 | 8 | * @author Automattic |
| 7 | 9 | */ |
| 8 | 10 | |
| 9 | -use Automattic\Jetpack\Assets; | |
| 10 | - | |
| 11 | 11 | /** |
| 12 | 12 | * Bail if accessed directly |
| 13 | 13 | */ |
| 14 | 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | - exit; | |
| 15 | + exit( 0 ); | |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * Class Jetpack_WooCommerce_Analytics_Universal |
| 20 | 20 | * Filters and Actions added to Store pages to perform analytics |
| 21 | + * | |
| 22 | + * @deprecated 13.3 | |
| 21 | 23 | */ |
| 22 | 24 | class Jetpack_WooCommerce_Analytics_Universal { |
| 23 | 25 | |
| 24 | 26 | /** |
| @@ -27,25 +29,22 @@ | ||
| 27 | 29 | use Jetpack_WooCommerce_Analytics_Trait; |
| 28 | 30 | |
| 29 | 31 | /** |
| 30 | 32 | * Jetpack_WooCommerce_Analytics_Universal constructor. |
| 33 | + * | |
| 34 | + * @deprecated 13.3 | |
| 31 | 35 | */ |
| 32 | 36 | public function __construct() { |
| 33 | - // loading _wca. | |
| 34 | - add_action( 'wp_head', array( $this, 'wp_head_top' ), 1 ); | |
| 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' ); | |
| 35 | 40 | |
| 36 | 41 | // add to carts from non-product pages or lists -- search, store etc. |
| 37 | 42 | add_action( 'wp_head', array( $this, 'loop_session_events' ), 2 ); |
| 38 | 43 | |
| 39 | - // loading s.js. | |
| 40 | - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_tracking_script' ) ); | |
| 41 | - | |
| 42 | 44 | // Capture cart events. |
| 43 | 45 | add_action( 'woocommerce_add_to_cart', array( $this, 'capture_add_to_cart' ), 10, 6 ); |
| 44 | 46 | |
| 45 | - // single product page view. | |
| 46 | - add_action( 'woocommerce_after_single_product', array( $this, 'capture_product_view' ) ); | |
| 47 | - | |
| 48 | 47 | add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart' ) ); |
| 49 | 48 | add_action( 'woocommerce_after_mini_cart', array( $this, 'remove_from_cart' ) ); |
| 50 | 49 | add_action( 'wcct_before_cart_widget', array( $this, 'remove_from_cart' ) ); |
| 51 | 50 | add_filter( 'woocommerce_cart_item_remove_link', array( $this, 'remove_from_cart_attributes' ), 10, 2 ); |
| @@ -60,34 +59,16 @@ | ||
| 60 | 59 | add_action( 'woocommerce_thankyou', array( $this, 'order_process' ), 10, 1 ); |
| 61 | 60 | add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart_via_quantity' ), 10, 1 ); |
| 62 | 61 | |
| 63 | 62 | add_filter( 'woocommerce_checkout_posted_data', array( $this, 'save_checkout_post_data' ), 10, 1 ); |
| 64 | - } | |
| 65 | 63 | |
| 66 | - /** | |
| 67 | - * Make _wca available to queue events | |
| 68 | - */ | |
| 69 | - public function wp_head_top() { | |
| 70 | - if ( is_cart() || is_checkout() || is_checkout_pay_page() || is_order_received_page() || is_add_payment_method_page() ) { | |
| 71 | - echo '<script>window._wca_prevent_referrer = true;</script>' . "\r\n"; | |
| 72 | - } | |
| 73 | - echo '<script>window._wca = window._wca || [];</script>' . "\r\n"; | |
| 64 | + add_action( 'woocommerce_created_customer', array( $this, 'capture_created_customer' ), 10, 2 ); | |
| 74 | 65 | } |
| 75 | 66 | |
| 76 | 67 | /** |
| 77 | - * Place script to call s.js, Store Analytics. | |
| 78 | - */ | |
| 79 | - public function enqueue_tracking_script() { | |
| 80 | - $filename = sprintf( | |
| 81 | - 'https://stats.wp.com/s-%d.js', | |
| 82 | - gmdate( 'YW' ) | |
| 83 | - ); | |
| 84 | - | |
| 85 | - Assets::enqueue_async_script( 'woocommerce-analytics', esc_url( $filename ), esc_url( $filename ), array(), null, false ); | |
| 86 | - } | |
| 87 | - | |
| 88 | - /** | |
| 89 | 68 | * On product lists or other non-product pages, add an event listener to "Add to Cart" button click |
| 69 | + * | |
| 70 | + * @deprecated 13.3 | |
| 90 | 71 | */ |
| 91 | 72 | public function loop_session_events() { |
| 92 | 73 | // Check for previous events queued in session data. |
| 93 | 74 | if ( is_object( WC()->session ) ) { |
| @@ -109,12 +90,20 @@ | ||
| 109 | 90 | } |
| 110 | 91 | |
| 111 | 92 | /** |
| 112 | 93 | * On the cart page, add an event listener for removal of product click |
| 94 | + * | |
| 95 | + * @deprecated 13.3 | |
| 113 | 96 | */ |
| 114 | 97 | public function remove_from_cart() { |
| 115 | - $common_props = $this->render_properties_as_js( | |
| 116 | - $this->get_common_properties() | |
| 98 | + $common_props = wp_json_encode( | |
| 99 | + array_merge( | |
| 100 | + array( | |
| 101 | + '_en' => 'woocommerceanalytics_remove_from_cart', | |
| 102 | + ), | |
| 103 | + $this->get_common_properties() | |
| 104 | + ), | |
| 105 | + JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP | |
| 117 | 106 | ); |
| 118 | 107 | |
| 119 | 108 | // We listen at div.woocommerce because the cart 'form' contents get forcibly |
| 120 | 109 | // updated and subsequent removals from cart would then not have this click |
| @@ -122,19 +111,13 @@ | ||
| 122 | 111 | wc_enqueue_js( |
| 123 | 112 | "jQuery( 'div.woocommerce' ).on( 'click', 'a.remove', function() { |
| 124 | 113 | var productID = jQuery( this ).data( 'product_id' ); |
| 125 | 114 | var quantity = jQuery( this ).parent().parent().find( '.qty' ).val() |
| 126 | - var productDetails = { | |
| 127 | - 'id': productID, | |
| 128 | - 'quantity': quantity ? quantity : '1', | |
| 129 | - }; | |
| 130 | - _wca.push( { | |
| 131 | - '_en': 'woocommerceanalytics_remove_from_cart', | |
| 132 | - 'pi': productDetails.id, | |
| 133 | - 'pq': productDetails.quantity, " . | |
| 134 | - $common_props . ' | |
| 135 | - } ); | |
| 136 | - } );' | |
| 115 | + var common_props = $common_props; | |
| 116 | + common_props.pi = productID; | |
| 117 | + common_props.pq = quantity ? quantity : '1'; | |
| 118 | + _wca.push( common_props ); | |
| 119 | + } );" | |
| 137 | 120 | ); |
| 138 | 121 | } |
| 139 | 122 | |
| 140 | 123 | /** |
| @@ -139,15 +122,17 @@ | ||
| 139 | 122 | |
| 140 | 123 | /** |
| 141 | 124 | * Adds the product ID to the remove product link (for use by remove_from_cart above) if not present |
| 142 | 125 | * |
| 126 | + * @deprecated 13.3 | |
| 127 | + * | |
| 143 | 128 | * @param string $url Full HTML a tag of the link to remove an item from the cart. |
| 144 | 129 | * @param string $key Unique Key ID for a cart item. |
| 145 | 130 | * |
| 146 | - * @return mixed. | |
| 131 | + * @return string | |
| 147 | 132 | */ |
| 148 | 133 | public function remove_from_cart_attributes( $url, $key ) { |
| 149 | - if ( false !== strpos( $url, 'data-product_id' ) ) { | |
| 134 | + if ( str_contains( $url, 'data-product_id' ) ) { | |
| 150 | 135 | return $url; |
| 151 | 136 | } |
| 152 | 137 | |
| 153 | 138 | $item = WC()->cart->get_cart_item( $key ); |
| @@ -162,48 +147,63 @@ | ||
| 162 | 147 | return $url; |
| 163 | 148 | } |
| 164 | 149 | |
| 165 | 150 | /** |
| 166 | - * Gather relevant product information | |
| 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. | |
| 167 | 153 | * |
| 168 | - * @param array $product product. | |
| 169 | - * @return array | |
| 154 | + * @deprecated 13.3 | |
| 155 | + * | |
| 156 | + * @param string $cart_item_key the cart item key. | |
| 157 | + * | |
| 158 | + * @return mixed|bool | |
| 170 | 159 | */ |
| 171 | - public function get_product_details( $product ) { | |
| 172 | - return array( | |
| 173 | - 'id' => $product->get_id(), | |
| 174 | - 'name' => $product->get_title(), | |
| 175 | - 'category' => $this->get_product_categories_concatenated( $product ), | |
| 176 | - 'price' => $product->get_price(), | |
| 177 | - 'type' => $product->get_type(), | |
| 178 | - ); | |
| 179 | - } | |
| 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' ); | |
| 180 | 163 | |
| 181 | - /** | |
| 182 | - * Track a product page view | |
| 183 | - */ | |
| 184 | - public function capture_product_view() { | |
| 185 | - global $product; | |
| 186 | - if ( ! $product instanceof WC_Product ) { | |
| 187 | - return; | |
| 164 | + if ( ! is_array( $packages ) || ! is_array( $selected_options ) ) { | |
| 165 | + return false; | |
| 188 | 166 | } |
| 189 | 167 | |
| 190 | - $this->record_event( | |
| 191 | - 'woocommerceanalytics_product_view', | |
| 192 | - array(), | |
| 193 | - $product->get_id() | |
| 194 | - ); | |
| 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; | |
| 195 | 194 | } |
| 196 | 195 | |
| 197 | 196 | /** |
| 198 | 197 | * On the Checkout page, trigger an event for each product in the cart |
| 198 | + * | |
| 199 | + * @deprecated 13.3 | |
| 199 | 200 | */ |
| 200 | 201 | public function checkout_process() { |
| 201 | - $cart = WC()->cart->get_cart(); | |
| 202 | + global $post; | |
| 203 | + $checkout_page_id = wc_get_page_id( 'checkout' ); | |
| 204 | + $cart = WC()->cart->get_cart(); | |
| 202 | 205 | |
| 203 | - $guest_checkout = ucfirst( get_option( 'woocommerce_enable_guest_checkout', 'No' ) ); | |
| 204 | - $create_account = ucfirst( get_option( 'woocommerce_enable_signup_and_login_from_checkout', 'No' ) ); | |
| 205 | - | |
| 206 | 206 | $enabled_payment_options = array_filter( |
| 207 | 207 | WC()->payment_gateways->get_available_payment_gateways(), |
| 208 | 208 | function ( $payment_gateway ) { |
| 209 | 209 | if ( ! $payment_gateway instanceof WC_Payment_Gateway ) { |
| @@ -214,18 +214,14 @@ | ||
| 214 | 214 | } |
| 215 | 215 | ); |
| 216 | 216 | |
| 217 | 217 | $enabled_payment_options = array_keys( $enabled_payment_options ); |
| 218 | - $include_express_payment = false; | |
| 219 | 218 | |
| 220 | - $wcpay_version = get_option( 'woocommerce_woocommerce_payments_version' ); | |
| 221 | - $has_required_wcpay_version = version_compare( $wcpay_version, '2.9.0', '>=' ); | |
| 222 | - // Check express payment availablity only if WC Pay is enabled and express checkout (payment request) is enabled. | |
| 223 | - if ( in_array( 'woocommerce_payments', $enabled_payment_options, true ) && $has_required_wcpay_version ) { | |
| 224 | - $wcpay_settings = get_option( 'woocommerce_woocommerce_payments_settings', array() ); | |
| 225 | - if ( array_key_exists( 'payment_request', $wcpay_settings ) && 'yes' === $wcpay_settings['payment_request'] ) { | |
| 226 | - $include_express_payment = true; | |
| 227 | - } | |
| 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(); | |
| 228 | 224 | } |
| 229 | 225 | |
| 230 | 226 | foreach ( $cart as $cart_item_key => $cart_item ) { |
| 231 | 227 | /** |
| @@ -236,51 +232,79 @@ | ||
| 236 | 232 | if ( ! $product || ! $product instanceof WC_Product ) { |
| 237 | 233 | continue; |
| 238 | 234 | } |
| 239 | 235 | |
| 240 | - if ( true === $include_express_payment ) { | |
| 241 | - $properties = $this->process_event_properties( | |
| 242 | - 'woocommerceanalytics_product_checkout', | |
| 243 | - array( | |
| 244 | - 'pq' => $cart_item['quantity'], | |
| 245 | - 'payment_options' => $enabled_payment_options, | |
| 246 | - 'device' => wp_is_mobile() ? 'mobile' : 'desktop', | |
| 247 | - 'guest_checkout' => 'Yes' === $guest_checkout ? 'Yes' : 'No', | |
| 248 | - 'create_account' => 'Yes' === $create_account ? 'Yes' : 'No', | |
| 249 | - 'express_checkout' => 'null', | |
| 250 | - ), | |
| 251 | - $product->get_id() | |
| 252 | - ); | |
| 253 | - wc_enqueue_js( | |
| 254 | - " | |
| 255 | - // wcpay.payment-request.availability event gets fired twice. | |
| 256 | - // make sure we push only one event. | |
| 257 | - var cartItem_{$cart_item_key}_logged = false; | |
| 258 | - wp.hooks.addAction('wcpay.payment-request.availability', 'wcpay', function(args) { | |
| 259 | - if ( true === cartItem_{$cart_item_key}_logged ) { | |
| 260 | - return; | |
| 261 | - } | |
| 262 | - var properties = {$properties}; | |
| 263 | - properties.express_checkout = args.paymentRequestType; | |
| 264 | - _wca.push(properties); | |
| 265 | - cartItem_{$cart_item_key}_logged = true; | |
| 266 | - }); | |
| 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( | |
| 267 | 257 | " |
| 268 | - ); | |
| 269 | - } else { | |
| 270 | - $this->record_event( | |
| 271 | - 'woocommerceanalytics_product_checkout', | |
| 272 | - array( | |
| 273 | - 'pq' => $cart_item['quantity'], | |
| 274 | - 'payment_options' => $enabled_payment_options, | |
| 275 | - 'device' => wp_is_mobile() ? 'mobile' : 'desktop', | |
| 276 | - 'guest_checkout' => 'Yes' === $guest_checkout ? 'Yes' : 'No', | |
| 277 | - 'create_account' => 'Yes' === $create_account ? 'Yes' : 'No', | |
| 278 | - 'express_checkout' => 'null', | |
| 279 | - ), | |
| 280 | - $product->get_id() | |
| 281 | - ); | |
| 282 | - } | |
| 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 | + | |
| 283 | 307 | } |
| 284 | 308 | } |
| 285 | 309 | |
| 286 | 310 | /** |
| @@ -285,8 +309,10 @@ | ||
| 285 | 309 | |
| 286 | 310 | /** |
| 287 | 311 | * After the checkout process, fire an event for each item in the order |
| 288 | 312 | * |
| 313 | + * @deprecated 13.3 | |
| 314 | + * | |
| 289 | 315 | * @param string $order_id Order Id. |
| 290 | 316 | */ |
| 291 | 317 | public function order_process( $order_id ) { |
| 292 | 318 | $order = wc_get_order( $order_id ); |
| @@ -300,14 +326,17 @@ | ||
| 300 | 326 | |
| 301 | 327 | $payment_option = $order->get_payment_method(); |
| 302 | 328 | |
| 303 | 329 | if ( is_object( WC()->session ) ) { |
| 304 | - $create_account = true === WC()->session->get( 'wc_checkout_createaccount_used' ) ? 'Y' : 'N'; | |
| 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 | + | |
| 305 | 333 | } else { |
| 306 | - $create_account = 'N'; | |
| 334 | + $create_account = 'No'; | |
| 335 | + $checkout_page_used = 'No'; | |
| 307 | 336 | } |
| 308 | 337 | |
| 309 | - $guest_checkout = $order->get_user() ? 'N' : 'Y'; | |
| 338 | + $guest_checkout = $order->get_user() ? 'No' : 'Yes'; | |
| 310 | 339 | |
| 311 | 340 | $express_checkout = 'null'; |
| 312 | 341 | // When the payment option is woocommerce_payment |
| 313 | 342 | // See if Google Pay or Apple Pay was used. |
| @@ -319,22 +348,48 @@ | ||
| 319 | 348 | $express_checkout = array( 'apple_pay' ); |
| 320 | 349 | } |
| 321 | 350 | } |
| 322 | 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 | + } elseif ( 'checkout' === $order_source ) { | |
| 359 | + $checkout_page_contains_checkout_shortcode = '1'; | |
| 360 | + } | |
| 361 | + | |
| 323 | 362 | // loop through products in the order and queue a purchase event. |
| 324 | 363 | foreach ( $order->get_items() as $order_item ) { |
| 364 | + // @phan-suppress-next-line PhanUndeclaredMethod -- Checked before being called. See also https://github.com/phan/phan/issues/1204. | |
| 325 | 365 | $product_id = is_callable( array( $order_item, 'get_product_id' ) ) ? $order_item->get_product_id() : -1; |
| 326 | 366 | |
| 367 | + $order_items = $order->get_items(); | |
| 368 | + $order_items_count = 0; | |
| 369 | + if ( is_array( $order_items ) ) { | |
| 370 | + $order_items_count = count( $order_items ); | |
| 371 | + } | |
| 372 | + $order_coupons = $order->get_coupons(); | |
| 373 | + $order_coupons_count = 0; | |
| 374 | + if ( is_array( $order_coupons ) ) { | |
| 375 | + $order_coupons_count = count( $order_coupons ); | |
| 376 | + } | |
| 327 | 377 | $this->record_event( |
| 328 | 378 | 'woocommerceanalytics_product_purchase', |
| 329 | 379 | array( |
| 330 | 380 | 'oi' => $order->get_order_number(), |
| 331 | 381 | 'pq' => $order_item->get_quantity(), |
| 332 | - 'device' => wp_is_mobile() ? 'mobile' : 'desktop', | |
| 333 | 382 | 'payment_option' => $payment_option, |
| 334 | 383 | 'create_account' => $create_account, |
| 335 | 384 | 'guest_checkout' => $guest_checkout, |
| 336 | 385 | 'express_checkout' => $express_checkout, |
| 386 | + 'products_count' => $order_items_count, | |
| 387 | + 'coupon_used' => $order_coupons_count, | |
| 388 | + 'order_value' => $order->get_total(), | |
| 389 | + 'from_checkout' => $checkout_page_used, | |
| 390 | + 'checkout_page_contains_checkout_block' => $checkout_page_contains_checkout_block, | |
| 391 | + 'checkout_page_contains_checkout_shortcode' => $checkout_page_contains_checkout_shortcode, | |
| 337 | 392 | ), |
| 338 | 393 | $product_id |
| 339 | 394 | ); |
| 340 | 395 | } |
| @@ -342,12 +397,20 @@ | ||
| 342 | 397 | |
| 343 | 398 | /** |
| 344 | 399 | * Listen for clicks on the "Update Cart" button to know if an item has been removed by |
| 345 | 400 | * updating its quantity to zero |
| 401 | + * | |
| 402 | + * @deprecated 13.3 | |
| 346 | 403 | */ |
| 347 | 404 | public function remove_from_cart_via_quantity() { |
| 348 | - $common_props = $this->render_properties_as_js( | |
| 349 | - $this->get_common_properties() | |
| 405 | + $common_props = wp_json_encode( | |
| 406 | + array_merge( | |
| 407 | + array( | |
| 408 | + '_en' => 'woocommerceanalytics_remove_from_cart', | |
| 409 | + ), | |
| 410 | + $this->get_common_properties() | |
| 411 | + ), | |
| 412 | + JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP | |
| 350 | 413 | ); |
| 351 | 414 | |
| 352 | 415 | wc_enqueue_js( |
| 353 | 416 | " |
| @@ -356,22 +419,42 @@ | ||
| 356 | 419 | cartItems.each( function( item ) { |
| 357 | 420 | var qty = jQuery( this ).find( 'input.qty' ); |
| 358 | 421 | if ( qty && qty.val() === '0' ) { |
| 359 | 422 | var productID = jQuery( this ).find( '.product-remove a' ).data( 'product_id' ); |
| 360 | - _wca.push( { | |
| 361 | - '_en': 'woocommerceanalytics_remove_from_cart', | |
| 362 | - 'pi': productID, " . | |
| 363 | - $common_props . ' | |
| 364 | - } ); | |
| 423 | + var common_props = $common_props; | |
| 424 | + common_props.pi = productID; | |
| 425 | + _wca.push( common_props ); | |
| 365 | 426 | } |
| 366 | 427 | } ); |
| 367 | - } );' | |
| 428 | + } );" | |
| 368 | 429 | ); |
| 369 | 430 | } |
| 370 | 431 | |
| 371 | 432 | /** |
| 433 | + * Gets the inner blocks of a block. | |
| 434 | + * | |
| 435 | + * @deprecated 13.3 | |
| 436 | + * | |
| 437 | + * @param array $inner_blocks The inner blocks. | |
| 438 | + * | |
| 439 | + * @return array | |
| 440 | + */ | |
| 441 | + private function get_inner_blocks( $inner_blocks ) { | |
| 442 | + $block_names = array(); | |
| 443 | + if ( ! empty( $inner_blocks['blockName'] ) ) { | |
| 444 | + $block_names[] = $inner_blocks['blockName']; | |
| 445 | + } | |
| 446 | + if ( isset( $inner_blocks['innerBlocks'] ) && is_array( $inner_blocks['innerBlocks'] ) ) { | |
| 447 | + $block_names = array_merge( $block_names, $this->get_inner_blocks( $inner_blocks['innerBlocks'] ) ); | |
| 448 | + } | |
| 449 | + return $block_names; | |
| 450 | + } | |
| 451 | + | |
| 452 | + /** | |
| 372 | 453 | * Track adding items to the cart. |
| 373 | 454 | * |
| 455 | + * @deprecated 13.3 | |
| 456 | + * | |
| 374 | 457 | * @param string $cart_item_key Cart item key. |
| 375 | 458 | * @param int $product_id Product added to cart. |
| 376 | 459 | * @param int $quantity Quantity added to cart. |
| 377 | 460 | * @param int $variation_id Product variation. |
| @@ -392,8 +475,10 @@ | ||
| 392 | 475 | |
| 393 | 476 | /** |
| 394 | 477 | * Track in-session data. |
| 395 | 478 | * |
| 479 | + * @deprecated 13.3 | |
| 480 | + * | |
| 396 | 481 | * @param int $product_id Product ID. |
| 397 | 482 | * @param int $quantity Quantity. |
| 398 | 483 | * @param string $event Fired event. |
| 399 | 484 | */ |
| @@ -429,49 +514,41 @@ | ||
| 429 | 514 | WC()->session->set( 'wca_session_data', $data ); |
| 430 | 515 | } |
| 431 | 516 | |
| 432 | 517 | /** |
| 433 | - * Gets product categories or varation attributes as a formatted concatenated string | |
| 518 | + * Save createaccount post data to be used in $this->order_process. | |
| 434 | 519 | * |
| 435 | - * @param object $product WC_Product. | |
| 436 | - * @return string | |
| 520 | + * @deprecated 13.3 | |
| 521 | + * | |
| 522 | + * @param array $data post data from the checkout page. | |
| 523 | + * | |
| 524 | + * @return array | |
| 437 | 525 | */ |
| 438 | - public function get_product_categories_concatenated( $product ) { | |
| 439 | - | |
| 440 | - if ( ! $product instanceof WC_Product ) { | |
| 441 | - return ''; | |
| 442 | - } | |
| 443 | - | |
| 444 | - $variation_data = $product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $product->get_id() ) : ''; | |
| 445 | - if ( is_array( $variation_data ) && ! empty( $variation_data ) ) { | |
| 446 | - $line = wc_get_formatted_variation( $variation_data, true ); | |
| 447 | - } else { | |
| 448 | - $out = array(); | |
| 449 | - $categories = get_the_terms( $product->get_id(), 'product_cat' ); | |
| 450 | - if ( $categories ) { | |
| 451 | - foreach ( $categories as $category ) { | |
| 452 | - $out[] = $category->name; | |
| 453 | - } | |
| 526 | + public function save_checkout_post_data( array $data ) { | |
| 527 | + $session = WC()->session; | |
| 528 | + if ( is_object( $session ) ) { | |
| 529 | + if ( isset( $data['createaccount'] ) && ! empty( $data['createaccount'] ) ) { | |
| 530 | + $session->set( 'wc_checkout_createaccount_used', true ); | |
| 531 | + $session->save_data(); | |
| 454 | 532 | } |
| 455 | - $line = implode( '/', $out ); | |
| 456 | 533 | } |
| 457 | - return $line; | |
| 534 | + return $data; | |
| 458 | 535 | } |
| 459 | 536 | |
| 460 | 537 | /** |
| 461 | - * Save createaccount post data to be used in $this->order_process. | |
| 538 | + * Capture the create account event. Similar to save_checkout_post_data but works with Store API. | |
| 462 | 539 | * |
| 463 | - * @param array $data post data from the checkout page. | |
| 540 | + * @deprecated 13.3 | |
| 464 | 541 | * |
| 465 | - * @return array | |
| 542 | + * @param int $customer_id Customer ID. | |
| 543 | + * @param array $new_customer_data New customer data. | |
| 466 | 544 | */ |
| 467 | - public function save_checkout_post_data( array $data ) { | |
| 545 | + public function capture_created_customer( $customer_id, $new_customer_data ) { | |
| 468 | 546 | $session = WC()->session; |
| 469 | 547 | if ( is_object( $session ) ) { |
| 470 | - if ( isset( $data['createaccount'] ) && ! empty( $data['createaccount'] ) ) { | |
| 548 | + if ( str_contains( $new_customer_data['source'], 'store-api' ) ) { | |
| 471 | 549 | $session->set( 'wc_checkout_createaccount_used', true ); |
| 472 | 550 | $session->save_data(); |
| 473 | 551 | } |
| 474 | 552 | } |
| 475 | - return $data; | |
| 476 | 553 | } |
| 477 | 554 | } |