← All changes
|
modules/woocommerce-analytics/classes/class-jetpack-woocommerce-analytics-universal.php
+182
-275
12.8.3
→
16.3-a.5
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,26 +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 | - $this->cart_checkout_templates_in_use = class_exists( 'Automattic\WooCommerce\Blocks\Package' ) && version_compare( Automattic\WooCommerce\Blocks\Package::get_version(), '10.6.0', '>=' ); | |
| 34 | - // loading _wca. | |
| 35 | - 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' ); | |
| 36 | 40 | |
| 37 | 41 | // add to carts from non-product pages or lists -- search, store etc. |
| 38 | 42 | add_action( 'wp_head', array( $this, 'loop_session_events' ), 2 ); |
| 39 | 43 | |
| 40 | - // loading s.js. | |
| 41 | - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_tracking_script' ) ); | |
| 42 | - | |
| 43 | 44 | // Capture cart events. |
| 44 | 45 | add_action( 'woocommerce_add_to_cart', array( $this, 'capture_add_to_cart' ), 10, 6 ); |
| 45 | 46 | |
| 46 | - // single product page view. | |
| 47 | - add_action( 'woocommerce_after_single_product', array( $this, 'capture_product_view' ) ); | |
| 48 | - | |
| 49 | 47 | add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart' ) ); |
| 50 | 48 | add_action( 'woocommerce_after_mini_cart', array( $this, 'remove_from_cart' ) ); |
| 51 | 49 | add_action( 'wcct_before_cart_widget', array( $this, 'remove_from_cart' ) ); |
| 52 | 50 | add_filter( 'woocommerce_cart_item_remove_link', array( $this, 'remove_from_cart_attributes' ), 10, 2 ); |
| @@ -61,34 +59,16 @@ | ||
| 61 | 59 | add_action( 'woocommerce_thankyou', array( $this, 'order_process' ), 10, 1 ); |
| 62 | 60 | add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart_via_quantity' ), 10, 1 ); |
| 63 | 61 | |
| 64 | 62 | add_filter( 'woocommerce_checkout_posted_data', array( $this, 'save_checkout_post_data' ), 10, 1 ); |
| 65 | - } | |
| 66 | 63 | |
| 67 | - /** | |
| 68 | - * Make _wca available to queue events | |
| 69 | - */ | |
| 70 | - public function wp_head_top() { | |
| 71 | - if ( is_cart() || is_checkout() || is_checkout_pay_page() || is_order_received_page() || is_add_payment_method_page() ) { | |
| 72 | - echo '<script>window._wca_prevent_referrer = true;</script>' . "\r\n"; | |
| 73 | - } | |
| 74 | - echo '<script>window._wca = window._wca || [];</script>' . "\r\n"; | |
| 64 | + add_action( 'woocommerce_created_customer', array( $this, 'capture_created_customer' ), 10, 2 ); | |
| 75 | 65 | } |
| 76 | 66 | |
| 77 | 67 | /** |
| 78 | - * Place script to call s.js, Store Analytics. | |
| 79 | - */ | |
| 80 | - public function enqueue_tracking_script() { | |
| 81 | - $filename = sprintf( | |
| 82 | - 'https://stats.wp.com/s-%d.js', | |
| 83 | - gmdate( 'YW' ) | |
| 84 | - ); | |
| 85 | - | |
| 86 | - Assets::enqueue_async_script( 'woocommerce-analytics', esc_url( $filename ), esc_url( $filename ), array(), null, false ); | |
| 87 | - } | |
| 88 | - | |
| 89 | - /** | |
| 90 | 68 | * On product lists or other non-product pages, add an event listener to "Add to Cart" button click |
| 69 | + * | |
| 70 | + * @deprecated 13.3 | |
| 91 | 71 | */ |
| 92 | 72 | public function loop_session_events() { |
| 93 | 73 | // Check for previous events queued in session data. |
| 94 | 74 | if ( is_object( WC()->session ) ) { |
| @@ -110,12 +90,20 @@ | ||
| 110 | 90 | } |
| 111 | 91 | |
| 112 | 92 | /** |
| 113 | 93 | * On the cart page, add an event listener for removal of product click |
| 94 | + * | |
| 95 | + * @deprecated 13.3 | |
| 114 | 96 | */ |
| 115 | 97 | public function remove_from_cart() { |
| 116 | - $common_props = $this->render_properties_as_js( | |
| 117 | - $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 | |
| 118 | 106 | ); |
| 119 | 107 | |
| 120 | 108 | // We listen at div.woocommerce because the cart 'form' contents get forcibly |
| 121 | 109 | // updated and subsequent removals from cart would then not have this click |
| @@ -123,19 +111,13 @@ | ||
| 123 | 111 | wc_enqueue_js( |
| 124 | 112 | "jQuery( 'div.woocommerce' ).on( 'click', 'a.remove', function() { |
| 125 | 113 | var productID = jQuery( this ).data( 'product_id' ); |
| 126 | 114 | var quantity = jQuery( this ).parent().parent().find( '.qty' ).val() |
| 127 | - var productDetails = { | |
| 128 | - 'id': productID, | |
| 129 | - 'quantity': quantity ? quantity : '1', | |
| 130 | - }; | |
| 131 | - _wca.push( { | |
| 132 | - '_en': 'woocommerceanalytics_remove_from_cart', | |
| 133 | - 'pi': productDetails.id, | |
| 134 | - 'pq': productDetails.quantity, " . | |
| 135 | - $common_props . ' | |
| 136 | - } ); | |
| 137 | - } );' | |
| 115 | + var common_props = $common_props; | |
| 116 | + common_props.pi = productID; | |
| 117 | + common_props.pq = quantity ? quantity : '1'; | |
| 118 | + _wca.push( common_props ); | |
| 119 | + } );" | |
| 138 | 120 | ); |
| 139 | 121 | } |
| 140 | 122 | |
| 141 | 123 | /** |
| @@ -140,15 +122,17 @@ | ||
| 140 | 122 | |
| 141 | 123 | /** |
| 142 | 124 | * Adds the product ID to the remove product link (for use by remove_from_cart above) if not present |
| 143 | 125 | * |
| 126 | + * @deprecated 13.3 | |
| 127 | + * | |
| 144 | 128 | * @param string $url Full HTML a tag of the link to remove an item from the cart. |
| 145 | 129 | * @param string $key Unique Key ID for a cart item. |
| 146 | 130 | * |
| 147 | - * @return mixed. | |
| 131 | + * @return string | |
| 148 | 132 | */ |
| 149 | 133 | public function remove_from_cart_attributes( $url, $key ) { |
| 150 | - if ( false !== strpos( $url, 'data-product_id' ) ) { | |
| 134 | + if ( str_contains( $url, 'data-product_id' ) ) { | |
| 151 | 135 | return $url; |
| 152 | 136 | } |
| 153 | 137 | |
| 154 | 138 | $item = WC()->cart->get_cart_item( $key ); |
| @@ -163,43 +147,13 @@ | ||
| 163 | 147 | return $url; |
| 164 | 148 | } |
| 165 | 149 | |
| 166 | 150 | /** |
| 167 | - * Gather relevant product information | |
| 168 | - * | |
| 169 | - * @param array $product product. | |
| 170 | - * @return array | |
| 171 | - */ | |
| 172 | - public function get_product_details( $product ) { | |
| 173 | - return array( | |
| 174 | - 'id' => $product->get_id(), | |
| 175 | - 'name' => $product->get_title(), | |
| 176 | - 'category' => $this->get_product_categories_concatenated( $product ), | |
| 177 | - 'price' => $product->get_price(), | |
| 178 | - 'type' => $product->get_type(), | |
| 179 | - ); | |
| 180 | - } | |
| 181 | - | |
| 182 | - /** | |
| 183 | - * Track a product page view | |
| 184 | - */ | |
| 185 | - public function capture_product_view() { | |
| 186 | - global $product; | |
| 187 | - if ( ! $product instanceof WC_Product ) { | |
| 188 | - return; | |
| 189 | - } | |
| 190 | - | |
| 191 | - $this->record_event( | |
| 192 | - 'woocommerceanalytics_product_view', | |
| 193 | - array(), | |
| 194 | - $product->get_id() | |
| 195 | - ); | |
| 196 | - } | |
| 197 | - | |
| 198 | - /** | |
| 199 | 151 | * Get the selected shipping option for a cart item. If the name cannot be found in the options table, the method's |
| 200 | 152 | * ID will be used. |
| 201 | 153 | * |
| 154 | + * @deprecated 13.3 | |
| 155 | + * | |
| 202 | 156 | * @param string $cart_item_key the cart item key. |
| 203 | 157 | * |
| 204 | 158 | * @return mixed|bool |
| 205 | 159 | */ |
| @@ -240,16 +194,16 @@ | ||
| 240 | 194 | } |
| 241 | 195 | |
| 242 | 196 | /** |
| 243 | 197 | * On the Checkout page, trigger an event for each product in the cart |
| 198 | + * | |
| 199 | + * @deprecated 13.3 | |
| 244 | 200 | */ |
| 245 | 201 | public function checkout_process() { |
| 246 | - $cart = WC()->cart->get_cart(); | |
| 247 | - $this->get_additional_blocks_on_page(); | |
| 202 | + global $post; | |
| 203 | + $checkout_page_id = wc_get_page_id( 'checkout' ); | |
| 204 | + $cart = WC()->cart->get_cart(); | |
| 248 | 205 | |
| 249 | - $guest_checkout = ucfirst( get_option( 'woocommerce_enable_guest_checkout', 'No' ) ); | |
| 250 | - $create_account = ucfirst( get_option( 'woocommerce_enable_signup_and_login_from_checkout', 'No' ) ); | |
| 251 | - | |
| 252 | 206 | $enabled_payment_options = array_filter( |
| 253 | 207 | WC()->payment_gateways->get_available_payment_gateways(), |
| 254 | 208 | function ( $payment_gateway ) { |
| 255 | 209 | if ( ! $payment_gateway instanceof WC_Payment_Gateway ) { |
| @@ -260,18 +214,14 @@ | ||
| 260 | 214 | } |
| 261 | 215 | ); |
| 262 | 216 | |
| 263 | 217 | $enabled_payment_options = array_keys( $enabled_payment_options ); |
| 264 | - $include_express_payment = false; | |
| 265 | 218 | |
| 266 | - $wcpay_version = get_option( 'woocommerce_woocommerce_payments_version' ); | |
| 267 | - $has_required_wcpay_version = version_compare( $wcpay_version, '2.9.0', '>=' ); | |
| 268 | - // Check express payment availablity only if WC Pay is enabled and express checkout (payment request) is enabled. | |
| 269 | - if ( in_array( 'woocommerce_payments', $enabled_payment_options, true ) && $has_required_wcpay_version ) { | |
| 270 | - $wcpay_settings = get_option( 'woocommerce_woocommerce_payments_settings', array() ); | |
| 271 | - if ( array_key_exists( 'payment_request', $wcpay_settings ) && 'yes' === $wcpay_settings['payment_request'] ) { | |
| 272 | - $include_express_payment = true; | |
| 273 | - } | |
| 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(); | |
| 274 | 224 | } |
| 275 | 225 | |
| 276 | 226 | foreach ( $cart as $cart_item_key => $cart_item ) { |
| 277 | 227 | /** |
| @@ -282,75 +232,79 @@ | ||
| 282 | 232 | if ( ! $product || ! $product instanceof WC_Product ) { |
| 283 | 233 | continue; |
| 284 | 234 | } |
| 285 | 235 | |
| 286 | - $cart = wc()->cart->get_cart(); | |
| 287 | - $products_count = is_array( $cart ) ? count( $cart ) : '0'; | |
| 288 | - $coupons = wc()->cart->applied_coupons; | |
| 289 | - $coupon_used = is_array( $coupons ) && count( $coupons ) > 0 ? '1' : '0'; | |
| 290 | - $order_totals = wc()->cart->get_totals(); | |
| 291 | - $order_value = '0'; | |
| 292 | - if ( isset( $order_totals['total'] ) ) { | |
| 293 | - $order_value = $order_totals['total']; | |
| 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'] ); | |
| 294 | 242 | } |
| 295 | 243 | |
| 296 | - if ( true === $include_express_payment ) { | |
| 297 | - $properties = $this->process_event_properties( | |
| 298 | - 'woocommerceanalytics_product_checkout', | |
| 299 | - array( | |
| 300 | - 'pq' => $cart_item['quantity'], | |
| 301 | - 'payment_options' => $enabled_payment_options, | |
| 302 | - 'device' => wp_is_mobile() ? 'mobile' : 'desktop', | |
| 303 | - 'guest_checkout' => 'Yes' === $guest_checkout ? 'Yes' : 'No', | |
| 304 | - 'create_account' => 'Yes' === $create_account ? 'Yes' : 'No', | |
| 305 | - 'express_checkout' => 'null', | |
| 306 | - 'shipping_option' => $this->get_shipping_option_for_item( $cart_item_key ), | |
| 307 | - 'products_count' => $products_count, | |
| 308 | - 'coupon_used' => $coupon_used, | |
| 309 | - 'order_value' => $order_value, | |
| 310 | - 'store_currency' => get_woocommerce_currency(), | |
| 311 | - 'additional_blocks' => $this->additional_blocks_on_page, | |
| 312 | - 'template_used' => $this->cart_checkout_templates_in_use ? '1' : '0', | |
| 313 | - ), | |
| 314 | - $product->get_id() | |
| 315 | - ); | |
| 316 | - wc_enqueue_js( | |
| 317 | - " | |
| 318 | - // wcpay.payment-request.availability event gets fired twice. | |
| 319 | - // make sure we push only one event. | |
| 320 | - var cartItem_{$cart_item_key}_logged = false; | |
| 321 | - wp.hooks.addAction('wcpay.payment-request.availability', 'wcpay', function(args) { | |
| 322 | - if ( true === cartItem_{$cart_item_key}_logged ) { | |
| 323 | - return; | |
| 324 | - } | |
| 325 | - var properties = {$properties}; | |
| 326 | - properties.express_checkout = args.paymentRequestType; | |
| 327 | - _wca.push(properties); | |
| 328 | - cartItem_{$cart_item_key}_logged = true; | |
| 329 | - }); | |
| 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( | |
| 330 | 257 | " |
| 331 | - ); | |
| 332 | - } else { | |
| 333 | - $this->record_event( | |
| 334 | - 'woocommerceanalytics_product_checkout', | |
| 335 | - array( | |
| 336 | - 'pq' => $cart_item['quantity'], | |
| 337 | - 'payment_options' => $enabled_payment_options, | |
| 338 | - 'device' => wp_is_mobile() ? 'mobile' : 'desktop', | |
| 339 | - 'guest_checkout' => 'Yes' === $guest_checkout ? 'Yes' : 'No', | |
| 340 | - 'create_account' => 'Yes' === $create_account ? 'Yes' : 'No', | |
| 341 | - 'express_checkout' => 'null', | |
| 342 | - 'shipping_option' => $this->get_shipping_option_for_item( $cart_item_key ), | |
| 343 | - 'products_count' => $products_count, | |
| 344 | - 'coupon_used' => $coupon_used, | |
| 345 | - 'order_value' => $order_value, | |
| 346 | - 'store_currency' => get_woocommerce_currency(), | |
| 347 | - 'additional_blocks' => $this->additional_blocks_on_page, | |
| 348 | - 'template_used' => $this->cart_checkout_templates_in_use ? '1' : '0', | |
| 349 | - ), | |
| 350 | - $product->get_id() | |
| 351 | - ); | |
| 352 | - } | |
| 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 | + | |
| 353 | 307 | } |
| 354 | 308 | } |
| 355 | 309 | |
| 356 | 310 | /** |
| @@ -355,8 +309,10 @@ | ||
| 355 | 309 | |
| 356 | 310 | /** |
| 357 | 311 | * After the checkout process, fire an event for each item in the order |
| 358 | 312 | * |
| 313 | + * @deprecated 13.3 | |
| 314 | + * | |
| 359 | 315 | * @param string $order_id Order Id. |
| 360 | 316 | */ |
| 361 | 317 | public function order_process( $order_id ) { |
| 362 | 318 | $order = wc_get_order( $order_id ); |
| @@ -370,14 +326,17 @@ | ||
| 370 | 326 | |
| 371 | 327 | $payment_option = $order->get_payment_method(); |
| 372 | 328 | |
| 373 | 329 | if ( is_object( WC()->session ) ) { |
| 374 | - $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 | + | |
| 375 | 333 | } else { |
| 376 | - $create_account = 'N'; | |
| 334 | + $create_account = 'No'; | |
| 335 | + $checkout_page_used = 'No'; | |
| 377 | 336 | } |
| 378 | 337 | |
| 379 | - $guest_checkout = $order->get_user() ? 'N' : 'Y'; | |
| 338 | + $guest_checkout = $order->get_user() ? 'No' : 'Yes'; | |
| 380 | 339 | |
| 381 | 340 | $express_checkout = 'null'; |
| 382 | 341 | // When the payment option is woocommerce_payment |
| 383 | 342 | // See if Google Pay or Apple Pay was used. |
| @@ -389,10 +348,21 @@ | ||
| 389 | 348 | $express_checkout = array( 'apple_pay' ); |
| 390 | 349 | } |
| 391 | 350 | } |
| 392 | 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 | + | |
| 393 | 362 | // loop through products in the order and queue a purchase event. |
| 394 | 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. | |
| 395 | 365 | $product_id = is_callable( array( $order_item, 'get_product_id' ) ) ? $order_item->get_product_id() : -1; |
| 396 | 366 | |
| 397 | 367 | $order_items = $order->get_items(); |
| 398 | 368 | $order_items_count = 0; |
| @@ -406,21 +376,20 @@ | ||
| 406 | 376 | } |
| 407 | 377 | $this->record_event( |
| 408 | 378 | 'woocommerceanalytics_product_purchase', |
| 409 | 379 | array( |
| 410 | - 'oi' => $order->get_order_number(), | |
| 411 | - 'pq' => $order_item->get_quantity(), | |
| 412 | - 'device' => wp_is_mobile() ? 'mobile' : 'desktop', | |
| 413 | - 'payment_option' => $payment_option, | |
| 414 | - 'create_account' => $create_account, | |
| 415 | - 'guest_checkout' => $guest_checkout, | |
| 416 | - 'express_checkout' => $express_checkout, | |
| 417 | - 'products_count' => $order_items_count, | |
| 418 | - 'coupon_used' => $order_coupons_count, | |
| 419 | - 'order_value' => $order->get_total(), | |
| 420 | - 'store_currency' => get_woocommerce_currency(), | |
| 421 | - 'additional_blocks' => $this->additional_blocks_on_page, | |
| 422 | - 'template_used' => $this->cart_checkout_templates_in_use ? '1' : '0', | |
| 380 | + 'oi' => $order->get_order_number(), | |
| 381 | + 'pq' => $order_item->get_quantity(), | |
| 382 | + 'payment_option' => $payment_option, | |
| 383 | + 'create_account' => $create_account, | |
| 384 | + 'guest_checkout' => $guest_checkout, | |
| 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, | |
| 423 | 392 | ), |
| 424 | 393 | $product_id |
| 425 | 394 | ); |
| 426 | 395 | } |
| @@ -428,12 +397,20 @@ | ||
| 428 | 397 | |
| 429 | 398 | /** |
| 430 | 399 | * Listen for clicks on the "Update Cart" button to know if an item has been removed by |
| 431 | 400 | * updating its quantity to zero |
| 401 | + * | |
| 402 | + * @deprecated 13.3 | |
| 432 | 403 | */ |
| 433 | 404 | public function remove_from_cart_via_quantity() { |
| 434 | - $common_props = $this->render_properties_as_js( | |
| 435 | - $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 | |
| 436 | 413 | ); |
| 437 | 414 | |
| 438 | 415 | wc_enqueue_js( |
| 439 | 416 | " |
| @@ -442,28 +419,21 @@ | ||
| 442 | 419 | cartItems.each( function( item ) { |
| 443 | 420 | var qty = jQuery( this ).find( 'input.qty' ); |
| 444 | 421 | if ( qty && qty.val() === '0' ) { |
| 445 | 422 | var productID = jQuery( this ).find( '.product-remove a' ).data( 'product_id' ); |
| 446 | - _wca.push( { | |
| 447 | - '_en': 'woocommerceanalytics_remove_from_cart', | |
| 448 | - 'pi': productID, " . | |
| 449 | - $common_props . ' | |
| 450 | - } ); | |
| 423 | + var common_props = $common_props; | |
| 424 | + common_props.pi = productID; | |
| 425 | + _wca.push( common_props ); | |
| 451 | 426 | } |
| 452 | 427 | } ); |
| 453 | - } );' | |
| 428 | + } );" | |
| 454 | 429 | ); |
| 455 | 430 | } |
| 456 | 431 | |
| 457 | 432 | /** |
| 458 | - * Tracks any additional blocks loaded on the Cart/Checkout page. | |
| 433 | + * Gets the inner blocks of a block. | |
| 459 | 434 | * |
| 460 | - * @var array | |
| 461 | - */ | |
| 462 | - private $additional_blocks_on_page = array(); | |
| 463 | - | |
| 464 | - /** | |
| 465 | - * Gets the inner blocks of a block. | |
| 435 | + * @deprecated 13.3 | |
| 466 | 436 | * |
| 467 | 437 | * @param array $inner_blocks The inner blocks. |
| 468 | 438 | * |
| 469 | 439 | * @return array |
| @@ -479,68 +449,11 @@ | ||
| 479 | 449 | return $block_names; |
| 480 | 450 | } |
| 481 | 451 | |
| 482 | 452 | /** |
| 483 | - * Gets the IDs of additional blocks on the Cart/Checkout pages or templates. | |
| 453 | + * Track adding items to the cart. | |
| 484 | 454 | * |
| 485 | - * @return void | |
| 486 | - */ | |
| 487 | - public function get_additional_blocks_on_page() { | |
| 488 | - global $post; | |
| 489 | - $content = $post->post_content; | |
| 490 | - | |
| 491 | - $cart_checkout_templates_in_use = class_exists( 'Automattic\WooCommerce\Blocks\Package' ) && version_compare( Automattic\WooCommerce\Blocks\Package::get_version(), '10.6.0', '>=' ); | |
| 492 | - | |
| 493 | - if ( $cart_checkout_templates_in_use ) { | |
| 494 | - if ( function_exists( 'get_block_template' ) ) { | |
| 495 | - $content = get_block_template( 'woocommerce/woocommerce//page-checkout' )->content; | |
| 496 | - } | |
| 497 | - | |
| 498 | - if ( function_exists( 'gutenberg_get_block_template' ) ) { | |
| 499 | - $content = gutenberg_get_block_template( 'woocommerce/woocommerce//page-checkout' )->content; | |
| 500 | - } | |
| 501 | - } | |
| 502 | - | |
| 503 | - $parsed_blocks = parse_blocks( $content ); | |
| 504 | - $other_blocks = array_filter( | |
| 505 | - $parsed_blocks, | |
| 506 | - function ( $block ) { | |
| 507 | - if ( ! isset( $block['blockName'] ) ) { | |
| 508 | - return false; | |
| 509 | - } | |
| 510 | - if ( is_checkout() && 'woocommerce/checkout' !== $block['blockName'] ) { | |
| 511 | - return true; | |
| 512 | - } | |
| 513 | - if ( is_cart() && 'woocommerce/cart' !== $block['blockName'] ) { | |
| 514 | - return true; | |
| 515 | - } | |
| 516 | - return false; | |
| 517 | - } | |
| 518 | - ); | |
| 519 | - | |
| 520 | - $all_inner_blocks = array(); | |
| 521 | - | |
| 522 | - // Loop over each "block group". In templates the blocks are grouped up. | |
| 523 | - foreach ( $other_blocks as $block ) { | |
| 524 | - | |
| 525 | - // This check is necessary because sometimes this is null when using templates. | |
| 526 | - if ( ! empty( $block['blockName'] ) ) { | |
| 527 | - $all_inner_blocks[] = $block['blockName']; | |
| 528 | - } | |
| 529 | - | |
| 530 | - if ( ! isset( $block['innerBlocks'] ) || ! is_array( $block['innerBlocks'] ) || 0 === count( $block['innerBlocks'] ) ) { | |
| 531 | - continue; | |
| 532 | - } | |
| 533 | - | |
| 534 | - foreach ( $block['innerBlocks'] as $inner_content ) { | |
| 535 | - $all_inner_blocks = array_merge( $all_inner_blocks, $this->get_inner_blocks( $inner_content ) ); | |
| 536 | - } | |
| 537 | - } | |
| 538 | - $this->additional_blocks_on_page = $all_inner_blocks; | |
| 539 | - } | |
| 540 | - | |
| 541 | - /** | |
| 542 | - * Track adding items to the cart. | |
| 455 | + * @deprecated 13.3 | |
| 543 | 456 | * |
| 544 | 457 | * @param string $cart_item_key Cart item key. |
| 545 | 458 | * @param int $product_id Product added to cart. |
| 546 | 459 | * @param int $quantity Quantity added to cart. |
| @@ -562,8 +475,10 @@ | ||
| 562 | 475 | |
| 563 | 476 | /** |
| 564 | 477 | * Track in-session data. |
| 565 | 478 | * |
| 479 | + * @deprecated 13.3 | |
| 480 | + * | |
| 566 | 481 | * @param int $product_id Product ID. |
| 567 | 482 | * @param int $quantity Quantity. |
| 568 | 483 | * @param string $event Fired event. |
| 569 | 484 | */ |
| @@ -599,49 +514,41 @@ | ||
| 599 | 514 | WC()->session->set( 'wca_session_data', $data ); |
| 600 | 515 | } |
| 601 | 516 | |
| 602 | 517 | /** |
| 603 | - * Gets product categories or varation attributes as a formatted concatenated string | |
| 518 | + * Save createaccount post data to be used in $this->order_process. | |
| 604 | 519 | * |
| 605 | - * @param object $product WC_Product. | |
| 606 | - * @return string | |
| 520 | + * @deprecated 13.3 | |
| 521 | + * | |
| 522 | + * @param array $data post data from the checkout page. | |
| 523 | + * | |
| 524 | + * @return array | |
| 607 | 525 | */ |
| 608 | - public function get_product_categories_concatenated( $product ) { | |
| 609 | - | |
| 610 | - if ( ! $product instanceof WC_Product ) { | |
| 611 | - return ''; | |
| 612 | - } | |
| 613 | - | |
| 614 | - $variation_data = $product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $product->get_id() ) : ''; | |
| 615 | - if ( is_array( $variation_data ) && ! empty( $variation_data ) ) { | |
| 616 | - $line = wc_get_formatted_variation( $variation_data, true ); | |
| 617 | - } else { | |
| 618 | - $out = array(); | |
| 619 | - $categories = get_the_terms( $product->get_id(), 'product_cat' ); | |
| 620 | - if ( $categories ) { | |
| 621 | - foreach ( $categories as $category ) { | |
| 622 | - $out[] = $category->name; | |
| 623 | - } | |
| 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(); | |
| 624 | 532 | } |
| 625 | - $line = implode( '/', $out ); | |
| 626 | 533 | } |
| 627 | - return $line; | |
| 534 | + return $data; | |
| 628 | 535 | } |
| 629 | 536 | |
| 630 | 537 | /** |
| 631 | - * 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. | |
| 632 | 539 | * |
| 633 | - * @param array $data post data from the checkout page. | |
| 540 | + * @deprecated 13.3 | |
| 634 | 541 | * |
| 635 | - * @return array | |
| 542 | + * @param int $customer_id Customer ID. | |
| 543 | + * @param array $new_customer_data New customer data. | |
| 636 | 544 | */ |
| 637 | - public function save_checkout_post_data( array $data ) { | |
| 545 | + public function capture_created_customer( $customer_id, $new_customer_data ) { | |
| 638 | 546 | $session = WC()->session; |
| 639 | 547 | if ( is_object( $session ) ) { |
| 640 | - if ( isset( $data['createaccount'] ) && ! empty( $data['createaccount'] ) ) { | |
| 548 | + if ( str_contains( $new_customer_data['source'], 'store-api' ) ) { | |
| 641 | 549 | $session->set( 'wc_checkout_createaccount_used', true ); |
| 642 | 550 | $session->save_data(); |
| 643 | 551 | } |
| 644 | 552 | } |
| 645 | - return $data; | |
| 646 | 553 | } |
| 647 | 554 | } |