*/ public static function paid( \WC_Order $order ): array { $decimals = wc_get_price_decimals(); $items = array(); foreach ( $order->get_items() as $item ) { if ( ! $item instanceof \WC_Order_Item_Product ) { continue; } $line = array( 'p' => (int) $item->get_product_id(), 'qty' => (int) $item->get_quantity(), 'total' => wc_format_decimal( $item->get_total(), $decimals ), ); if ( $item->get_variation_id() > 0 ) { $line['vid'] = (int) $item->get_variation_id(); } $items[] = $line; } $created = $order->get_date_created(); $payload = self::envelope( 'order.paid' ); $payload['order'] = array( 'id' => (int) $order->get_id(), 'created_at' => $created ? gmdate( 'Y-m-d\TH:i:s\Z', $created->getTimestamp() ) : '', 'status' => (string) $order->get_status(), 'total' => wc_format_decimal( $order->get_total(), $decimals ), 'currency' => (string) $order->get_currency(), 'coupon' => array() !== $order->get_coupon_codes(), 'items' => $items, ); $attribution = self::attribution( $order ); if ( null !== $attribution ) { $payload['attribution'] = $attribution; } return $payload; } /** * @return array */ public static function refunded( \WC_Order $order, \WC_Order_Refund $refund ): array { $payload = self::envelope( 'order.refunded' ); $payload['order'] = array( 'id' => (int) $order->get_id(), 'currency' => (string) $order->get_currency(), ); $payload['refund'] = array( 'id' => (int) $refund->get_id(), 'amount' => wc_format_decimal( $refund->get_amount(), wc_get_price_decimals() ), ); return $payload; } /** * @return array */ private static function envelope( string $event ): array { return array( 'v' => 2, 'event' => $event, 'tenant_id' => Options::tenant_id(), 'pixel_id' => Options::pixel_id(), 'ts' => time(), ); } /** * @return array|null */ private static function attribution( \WC_Order $order ): ?array { $meta = static function ( string $field ) use ( $order ): string { return trim( (string) $order->get_meta( self::ATTRIBUTION_PREFIX . $field ) ); }; $source_type = $meta( 'source_type' ); if ( '' === $source_type ) { return null; } $out = array( 'source_type' => mb_substr( $source_type, 0, 32 ) ); $utm = array(); foreach ( array( 's' => 'utm_source', 'm' => 'utm_medium', 'c' => 'utm_campaign', 't' => 'utm_term', 'n' => 'utm_content', ) as $key => $field ) { $value = $meta( $field ); if ( '' !== $value && '(none)' !== $value ) { $utm[ $key ] = mb_substr( $value, 0, 64 ); } } if ( array() !== $utm ) { $out['utm'] = $utm; } $referrer = (string) wp_parse_url( $meta( 'referrer' ), PHP_URL_HOST ); if ( '' !== $referrer ) { $out['referrer_host'] = mb_substr( $referrer, 0, 253 ); } $entry = (string) wp_parse_url( $meta( 'session_entry' ), PHP_URL_PATH ); if ( '' !== $entry ) { $out['entry_path'] = mb_substr( $entry, 0, 512 ); } foreach ( array( 'session_pages', 'session_count' ) as $field ) { if ( is_numeric( $meta( $field ) ) ) { $out[ $field ] = (int) $meta( $field ); } } $device = $meta( 'device_type' ); if ( in_array( $device, self::DEVICE_TYPES, true ) ) { $out['device_type'] = $device; } return $out; } }