get_woocommerce_currency() ); } public static function product_added( WC_Product $product, $quantity, string $cart_id ): array { return self::without_empty( self::product_fields( $product ) + array( 'quantity' => $quantity, 'quantity_semantics' => 'item_delta', 'cart_id' => $cart_id, 'currency' => get_woocommerce_currency(), ) ); } public static function product_removed( WC_Product $product, $quantity, string $cart_id ): array { return self::without_empty( self::product_fields( $product ) + array( 'quantity' => $quantity, 'quantity_semantics' => 'item_delta', 'cart_id' => $cart_id, ) ); } public static function product_list_viewed( array $products, string $category, string $list_id ): ?array { $lines = array(); foreach ( array_values( $products ) as $index => $product ) { $lines[] = self::product_fields( $product ) + array( 'position' => $index + 1 ); } if ( array() === $lines ) { return null; } return self::without_empty( array( 'products' => $lines, 'category' => $category, 'list_id' => $list_id, ) ); } public static function products_searched( string $query ): ?array { $query = trim( $query ); return '' === $query ? null : array( 'query' => $query ); } public static function cart_viewed( WC_Cart $cart, string $cart_id ): ?array { $products = self::cart_lines( $cart ); if ( array() === $products || '' === $cart_id ) { return null; } return array( 'cart_id' => $cart_id, 'products' => $products, ); } public static function cart_emptied( string $cart_id ): array { return array( 'cart_id' => $cart_id, 'products' => array(), 'quantity_semantics' => 'cart_snapshot', ); } public static function checkout_started( WC_Cart $cart, string $cart_id ): ?array { $products = self::cart_lines( $cart ); if ( array() === $products || '' === $cart_id ) { return null; } $discount = self::money( $cart->get_discount_total() ); return self::without_empty( array( 'order_id' => $cart_id, 'cart_id' => $cart_id, 'products' => $products, 'value' => self::money( $cart->get_total( 'edit' ) ), 'revenue' => self::money( (float) $cart->get_subtotal() - $discount ), 'currency' => get_woocommerce_currency(), 'coupon' => implode( ',', $cart->get_applied_coupons() ), 'discount' => $discount, 'shipping' => self::money( $cart->get_shipping_total() ), 'tax' => self::money( $cart->get_total_tax() ), ) ); } public static function order_completed( WC_Order $order ): ?array { $products = self::order_lines( $order ); if ( array() === $products ) { return null; } $total = self::money( $order->get_total() ); $subtotal = self::money( $order->get_subtotal() ); $discount = self::money( $order->get_discount_total() ); return self::without_empty( array( 'order_id' => (string) $order->get_id(), 'transaction_id' => (string) $order->get_id(), 'customer' => self::without_empty( array( 'id' => $order->get_customer_id() > 0 ? (string) $order->get_customer_id() : '', 'email' => $order->get_billing_email(), 'phone' => $order->get_billing_phone(), 'firstname' => $order->get_billing_first_name(), 'lastname' => $order->get_billing_last_name(), ) ), 'products' => $products, 'value' => $total, 'revenue' => $total, 'total' => $total, 'subtotal' => $subtotal, 'currency' => $order->get_currency(), 'tax' => self::money( $order->get_total_tax() ), 'shipping' => self::money( $order->get_shipping_total() ), 'discount' => $discount, 'coupon' => implode( ',', $order->get_coupon_codes() ), 'payment_method' => $order->get_payment_method(), 'shipping_method' => $order->get_shipping_method(), 'order_placed_at' => self::iso_time( $order->get_date_created() ), ) ); } public static function order_refunded( WC_Order $order, WC_Order_Refund $refund ): array { return self::without_empty( array( 'order_id' => (string) $order->get_id(), 'refund_id' => (string) $refund->get_id(), 'refund_amount' => self::money( $refund->get_amount() ), 'reason' => $refund->get_reason(), 'currency' => $order->get_currency(), 'products' => self::order_lines( $refund ), 'total' => self::money( $order->get_total() ), 'payment_method' => $order->get_payment_method(), 'order_placed_at' => self::iso_time( $order->get_date_created() ), ) ); } public static function order_cancelled( WC_Order $order, string $previous_status ): array { return self::without_empty( array( 'order_id' => (string) $order->get_id(), 'status' => 'cancelled', 'previous_status' => $previous_status, 'total' => self::money( $order->get_total() ), 'currency' => $order->get_currency(), 'order_placed_at' => self::iso_time( $order->get_date_created() ), ) ); } public static function product_id( WC_Product $product ): string { return (string) ( $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id() ); } public static function product_fields( WC_Product $product ): array { $parent_id = (int) self::product_id( $product ); return self::without_empty( array( 'product_id' => (string) $parent_id, 'name' => '' !== $product->get_title() ? $product->get_title() : '#' . $parent_id, 'sku' => $product->get_sku(), 'price' => self::money( wc_get_price_to_display( $product ) ), 'category' => self::first_term( $parent_id, 'product_cat' ), 'brand' => taxonomy_exists( 'product_brand' ) ? self::first_term( $parent_id, 'product_brand' ) : '', 'variant' => $product->is_type( 'variation' ) ? $product->get_attribute_summary() : '', 'url' => (string) $product->get_permalink(), 'image_url' => (string) wp_get_attachment_url( (int) $product->get_image_id() ), ) ); } private static function cart_lines( WC_Cart $cart ): array { $lines = array(); foreach ( $cart->get_cart() as $item ) { if ( isset( $item['data'] ) && $item['data'] instanceof WC_Product ) { $lines[] = self::product_fields( $item['data'] ) + array( 'quantity' => wc_stock_amount( $item['quantity'] ) ); } } return $lines; } private static function order_lines( WC_Abstract_Order $order ): array { $lines = array(); foreach ( $order->get_items() as $item ) { if ( $item instanceof WC_Order_Item_Product ) { $lines[] = self::order_line( $order, $item ); } } return $lines; } private static function order_line( WC_Abstract_Order $order, WC_Order_Item_Product $item ): array { $product = $item->get_product(); $fields = $product instanceof WC_Product ? self::product_fields( $product ) : array( 'product_id' => self::deleted_line_product_id( $item ) ); $fields['name'] = isset( $fields['name'] ) ? $fields['name'] : $item->get_name(); $fields['quantity'] = abs( wc_stock_amount( $item->get_quantity() ) ); $fields['price'] = abs( self::money( $order->get_item_total( $item ) ) ); return self::without_empty( $fields ); } private static function deleted_line_product_id( WC_Order_Item_Product $item ): string { // WC zeroes a line's product id in memory once the product post is gone; the stored meta still has it. $stored = (int) wc_get_order_item_meta( $item->get_id(), '_product_id', true ); return (string) max( $item->get_product_id(), $stored ); } private static function first_term( int $post_id, string $taxonomy ): string { $terms = get_the_terms( $post_id, $taxonomy ); return is_array( $terms ) && isset( $terms[0] ) ? $terms[0]->name : ''; } private static function money( $amount ): float { return (float) wc_format_decimal( $amount, wc_get_price_decimals() ); } private static function iso_time( ?WC_DateTime $date ): string { return null === $date ? '' : gmdate( 'Y-m-d\TH:i:s\Z', $date->getTimestamp() ); } private static function without_empty( array $fields ): array { return array_filter( $fields, static function ( $value ) { return null !== $value && '' !== $value && array() !== $value; } ); } }