get_parent_id() ? wc_get_product( $product->get_parent_id() ) : null; $terms = get_the_terms( $parent ? $parent->get_id() : $product->get_id(), 'product_cat' ); $item = array( 'item_id' => '' !== (string) $product->get_sku() ? (string) $product->get_sku() : (string) $product->get_id(), 'item_name' => wp_strip_all_tags( $parent ? $parent->get_name() : $product->get_name() ), 'price' => round( $price, 2 ), 'quantity' => $quantity, ); if ( $parent ) { $item['item_variant'] = wp_strip_all_tags( wc_get_formatted_variation( $product, true, false, true ) ); } if ( is_array( $terms ) && $terms ) { $item['item_category'] = wp_strip_all_tags( (string) $terms[0]->name ); } return $item; } /** * The cart as GA4 parameters. */ public static function cart_data(): array { $cart = WC()->cart; $items = array(); if ( $cart ) { foreach ( $cart->get_cart() as $line ) { $product = $line['data'] ?? null; $quantity = (int) ( $line['quantity'] ?? 0 ); if ( $product instanceof \WC_Product && $quantity > 0 ) { $items[] = self::item( $product, $quantity, ( (float) $line['line_subtotal'] + (float) $line['line_subtotal_tax'] ) / $quantity ); } } } $coupons = $cart ? $cart->get_applied_coupons() : array(); return array( 'currency' => get_woocommerce_currency(), 'value' => $cart ? round( (float) $cart->get_total( 'edit' ), 2 ) : 0, 'coupon' => $coupons ? implode( ',', $coupons ) : '', 'items' => $items, ); } /** * JSON in a data attribute of a hidden element. * * @param string $id Element id. * @param array $data Data. */ private static function json_tag( string $id, array $data ): string { return ''; } /** * The script and the cart, on the checkout. */ public static function enqueue(): void { if ( ! function_exists( 'is_checkout' ) || ! is_checkout() || is_order_received_page() || is_checkout_pay_page() ) { return; } // On the Checkout block the script reads the block's data stores (wp-data). $deps = PageMode::uses_block() ? array( 'jquery', 'wp-data' ) : array( 'jquery' ); wp_enqueue_script( 'aiolos-tracking', Plugin::asset( 'js/tracking.js' ), $deps, Plugin::asset_version( 'js/tracking.js' ), true ); wp_localize_script( 'aiolos-tracking', 'aiolosTracking', array( 'mode' => self::mode(), 'cart' => self::cart_data(), ) ); } /** * The first copy of the cart data, which the checkout fragments then replace. */ public static function cart_tag(): void { if ( wp_script_is( 'aiolos-tracking', 'enqueued' ) && is_checkout() && ! is_order_received_page() ) { printf( '', esc_attr( (string) wp_json_encode( self::cart_data(), JSON_UNESCAPED_UNICODE ) ) ); } } /** * Refreshes the cart data whenever WooCommerce refreshes the checkout (quantities, coupons). * * @param array $fragments Fragments. * @return array */ public static function fragment( $fragments ): array { $fragments = (array) $fragments; $fragments['div#aiolos-track-cart'] = self::json_tag( 'aiolos-track-cart', self::cart_data() ); return $fragments; } /** * The purchase event, once per order. * * @param int $order_id Order id. */ public static function purchase( $order_id ): void { $order = wc_get_order( $order_id ); if ( ! $order || $order->get_meta( '_aiolos_tracked' ) || $order->has_status( 'failed' ) ) { return; } $items = array(); foreach ( $order->get_items() as $line ) { $product = $line instanceof \WC_Order_Item_Product ? $line->get_product() : null; $quantity = (int) $line->get_quantity(); if ( $product && $quantity > 0 ) { $items[] = self::item( $product, $quantity, ( (float) $line->get_subtotal() + (float) $line->get_subtotal_tax() ) / $quantity ); } } $data = array( 'transaction_id' => (string) $order->get_order_number(), 'currency' => $order->get_currency(), 'value' => round( (float) $order->get_total(), 2 ), 'tax' => round( (float) $order->get_total_tax(), 2 ), 'shipping' => round( (float) $order->get_shipping_total() + (float) $order->get_shipping_tax(), 2 ), 'coupon' => implode( ',', $order->get_coupon_codes() ), 'items' => $items, ); $order->update_meta_data( '_aiolos_tracked', time() ); $order->save_meta_data(); wp_enqueue_script( 'aiolos-tracking', Plugin::asset( 'js/tracking.js' ), array( 'jquery' ), Plugin::asset_version( 'js/tracking.js' ), true ); wp_localize_script( 'aiolos-tracking', 'aiolosTracking', array( 'mode' => self::mode(), 'purchase' => $data, ) ); } }