Created
April 28, 2026 15:29
-
-
Save xlplugins/226d89799165232927944cfe5d280e83 to your computer and use it in GitHub Desktop.
customer new param in capi events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| defined( 'ABSPATH' ) || exit; | |
| add_filter( | |
| 'wffn_ecomm_tracking_fb_params', | |
| static function ( $data, $order = null ) { | |
| if ( ! $order instanceof WC_Order ) { | |
| return $data; | |
| } | |
| $data['is_customer_new'] = fk_capi_is_customer_new( $order ) ? 1 : 0; | |
| return $data; | |
| }, | |
| 15, | |
| 2 | |
| ); | |
| add_filter( | |
| 'wfocu_ecomm_tracking_fb_params', | |
| static function ( $data, $is_offer = false, $order = null, $package = null ) { | |
| $resolved = fk_capi_order_from_fb_slice( $data ); | |
| if ( ! $resolved instanceof WC_Order && $order instanceof WC_Order ) { | |
| $resolved = $order; | |
| } | |
| if ( ! $resolved instanceof WC_Order ) { | |
| return $data; | |
| } | |
| $data['is_customer_new'] = fk_capi_is_customer_new( $resolved ) ? 1 : 0; | |
| return $data; | |
| }, | |
| 15, | |
| 4 | |
| ); | |
| add_filter( | |
| 'wffn_capi_purchase_params', | |
| static function ( $params, $session_data ) { | |
| $order = fk_capi_order_from_tracking_session( $session_data ); | |
| if ( ! $order instanceof WC_Order ) { | |
| return $params; | |
| } | |
| $params['is_customer_new'] = fk_capi_is_customer_new( $order ) ? 1 : 0; | |
| return $params; | |
| }, | |
| 10, | |
| 2 | |
| ); | |
| add_filter( | |
| 'wfocu_capi_purchase_params', | |
| static function ( $params, $session_data ) { | |
| $order = fk_capi_order_from_tracking_session( $session_data ); | |
| if ( ! $order instanceof WC_Order ) { | |
| return $params; | |
| } | |
| $params['is_customer_new'] = fk_capi_is_customer_new( $order ) ? 1 : 0; | |
| return $params; | |
| }, | |
| 10, | |
| 2 | |
| ); | |
| /** | |
| * @param array $fb `fb` slice from tracking session or fb_params array. | |
| * @return WC_Order|null | |
| */ | |
| function fk_capi_order_from_fb_slice( $fb ) { | |
| if ( ! is_array( $fb ) ) { | |
| return null; | |
| } | |
| if ( ! empty( $fb['order_id'] ) ) { | |
| $oid = $fb['order_id']; | |
| if ( is_numeric( $oid ) ) { | |
| $o = wc_get_order( absint( $oid ) ); | |
| if ( $o instanceof WC_Order ) { | |
| return $o; | |
| } | |
| } | |
| if ( is_string( $oid ) && strpos( $oid, '-' ) !== false ) { | |
| $parts = explode( '-', $oid, 2 ); | |
| $o = wc_get_order( absint( $parts[0] ) ); | |
| if ( $o instanceof WC_Order ) { | |
| return $o; | |
| } | |
| } | |
| } | |
| if ( ! empty( $fb['is_order'] ) && is_numeric( $fb['is_order'] ) ) { | |
| $o = wc_get_order( absint( $fb['is_order'] ) ); | |
| if ( $o instanceof WC_Order ) { | |
| return $o; | |
| } | |
| } | |
| return null; | |
| } | |
| /** | |
| * @param array $session_data Session passed to CAPI filters. | |
| * @return WC_Order|null | |
| */ | |
| function fk_capi_order_from_tracking_session( $session_data ) { | |
| if ( ! is_array( $session_data ) || empty( $session_data['fb'] ) || ! is_array( $session_data['fb'] ) ) { | |
| return null; | |
| } | |
| return fk_capi_order_from_fb_slice( $session_data['fb'] ); | |
| } | |
| /** | |
| * True when this order is the first for that customer (user id or billing email) before this order date. | |
| * | |
| * @param WC_Order $order Order tied to the Purchase event. | |
| */ | |
| function fk_capi_is_customer_new( WC_Order $order ) { | |
| $override = apply_filters( 'fk_capi_is_customer_new', null, $order ); | |
| if ( null !== $override ) { | |
| return (bool) $override; | |
| } | |
| $created = $order->get_date_created(); | |
| if ( ! $created ) { | |
| return false; | |
| } | |
| $exclude = array_unique( | |
| array_filter( | |
| array( | |
| $order->get_id(), | |
| absint( $order->get_meta( '_wfocu_sibling_order', true ) ), | |
| ) | |
| ) | |
| ); | |
| $statuses = apply_filters( | |
| 'fk_capi_prior_order_statuses', | |
| array( | |
| 'wc-pending', | |
| 'wc-on-hold', | |
| 'wc-processing', | |
| 'wc-completed', | |
| 'wc-refunded', | |
| ), | |
| $order | |
| ); | |
| $query = array( | |
| 'limit' => 1, | |
| 'return' => 'ids', | |
| 'type' => 'shop_order', | |
| 'status' => $statuses, | |
| 'date_created' => '<' . $created->date( 'Y-m-d H:i:s' ), | |
| 'exclude' => $exclude, | |
| ); | |
| if ( $order->get_customer_id() > 0 ) { | |
| $query['customer_id'] = $order->get_customer_id(); | |
| } else { | |
| $email = strtolower( trim( (string) $order->get_billing_email() ) ); | |
| if ( '' === $email ) { | |
| return false; | |
| } | |
| $query['billing_email'] = $email; | |
| } | |
| $prior = wc_get_orders( $query ); | |
| return empty( $prior ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment