is_paid(); } return false; } public static function checkout( $order ) { $order = is_object( $order ) ? $order : wc_get_order( $order ); if ( $order ) { $id = self::sync( $order ); if ( $id ) { Nodebook_Tracker::identify( $id ); } } } public static function status_changed( $order_id, $from, $to, $order = null ) { $order = $order ? $order : wc_get_order( $order_id ); if ( $order ) { self::sync( $order ); } } public static function refunded( $order_id, $refund_id ) { $order = wc_get_order( $order_id ); $refund = wc_get_order( $refund_id ); if ( ! $order ) { return; } $cid = self::sync( $order ); if ( $cid && $refund && ! Nodebook_Activities::find( 'refund', $refund_id ) ) { Nodebook_Activities::add( $cid, 'refund', /* translators: 1: order number, 2: amount */ sprintf( __( 'Refund on order #%1$s: %2$s', 'nodebook' ), $order->get_order_number(), wp_strip_all_tags( wc_price( abs( (float) $refund->get_amount() ), array( 'currency' => $order->get_currency() ) ) ) ), array( 'object_id' => $refund_id, 'user_id' => 0, 'data' => array( 'order_id' => $order->get_id(), 'amount' => (float) $refund->get_amount(), 'reason' => $refund->get_reason(), ), ) ); } } /** * Create/update the contact for an order and record the order on the timeline. * * @param WC_Order $order Order. * @return int Contact ID. */ public static function sync( $order, $force = false ) { if ( ! $order || ! is_a( $order, 'WC_Order' ) || is_a( $order, 'WC_Order_Refund' ) ) { return 0; } $email = $order->get_billing_email(); if ( ! $email ) { return 0; } // New buyers only when the setting allows it now (a later status change, e.g. paid, can still add them). if ( ! $force && ! self::may_create( $order ) && ! Nodebook_Contacts::find_by_email( $email ) ) { return 0; } $data = array( 'email' => $email, 'first_name' => $order->get_billing_first_name(), 'last_name' => $order->get_billing_last_name(), 'phone' => $order->get_billing_phone(), 'company' => $order->get_billing_company(), 'address' => trim( $order->get_billing_address_1() . ' ' . $order->get_billing_address_2() ), 'city' => $order->get_billing_city(), 'state' => $order->get_billing_state(), 'postcode' => $order->get_billing_postcode(), 'country' => $order->get_billing_country(), 'source' => 'WooCommerce', ); // Link the WordPress account only when it is the buyer's own: orders entered by staff (admin, POS) // often carry the staff member as "customer", which would tie unrelated buyers to one account. $uid = (int) $order->get_customer_id(); if ( $uid ) { $user = get_userdata( $uid ); if ( $user && ( strtolower( (string) $user->user_email ) === strtolower( (string) $email ) || ! Nodebook_Repair::is_staff( $uid ) ) ) { $data['wp_user_id'] = $uid; } } $result = Nodebook_Contacts::upsert( $data, Nodebook_Settings::get( 'update_policy' ) ); $cid = $result['id']; if ( ! $cid ) { return 0; } if ( $result['created'] ) { Nodebook_Activities::add( $cid, 'created', __( 'Contact created from WooCommerce', 'nodebook' ), array( 'user_id' => 0 ) ); // A new contact can still be an old customer: bring in their earlier orders so the timeline and "first order" are right. if ( Nodebook_Settings::get( 'woo_history' ) ) { self::history( $cid, $email, $order->get_id() ); } } // No first touch yet (e.g. order placed before Nodebook, or cookies blocked)? Use WooCommerce's own attribution. Nodebook_Attribution::apply_wc_order( $cid, $order ); $items = array(); foreach ( $order->get_items() as $item ) { $items[] = array( 'name' => $item->get_name(), 'qty' => (float) $item->get_quantity(), 'total' => (float) $item->get_total(), 'product_id' => (int) $item->get_product_id(), ); } $status = $order->get_status(); $created = $order->get_date_created(); $title = sprintf( /* translators: 1: order number, 2: total, 3: status */ __( 'Order #%1$s · %2$s · %3$s', 'nodebook' ), $order->get_order_number(), wp_strip_all_tags( wc_price( (float) $order->get_total(), array( 'currency' => $order->get_currency() ) ) ), wc_get_order_status_name( $status ) ); $payload = array( 'number' => $order->get_order_number(), 'status' => $status, 'total' => (float) $order->get_total(), 'currency' => $order->get_currency(), 'items' => $items, 'value' => (float) $order->get_total(), 'item' => $items ? (int) $items[0]['product_id'] : 0, ); $existing = Nodebook_Activities::find( 'order', $order->get_id() ); if ( $existing ) { Nodebook_Activities::update( $existing['id'], array( 'contact_id' => $cid, 'title' => $title, 'data' => $payload, ) ); } else { Nodebook_Activities::add( $cid, 'order', $title, array( 'object_id' => $order->get_id(), 'user_id' => 0, 'created_at' => $created ? gmdate( 'Y-m-d H:i:s', $created->getTimestamp() ) : '', 'data' => $payload, ) ); } self::recalculate( $cid, $email ); return $cid; } /** * Orders count and lifetime value from paid orders, net of refunds. */ public static function recalculate( $contact_id, $email ) { $orders = array(); // Include former addresses of merged contacts. foreach ( array_unique( array_filter( array_merge( array( strtolower( (string) $email ) ), Nodebook_Contacts::emails( $contact_id ) ) ) ) as $address ) { foreach ( wc_get_orders( array( 'billing_email' => $address, 'status' => wc_get_is_paid_statuses(), 'limit' => -1, 'type' => 'shop_order', ) ) as $o ) { $orders[ $o->get_id() ] = $o; } } // Totals stay in the shop currency; orders in other currencies (multi-currency plugins) are kept apart. $main = Nodebook_Conversions::currency(); $total = 0.0; $others = array(); foreach ( $orders as $o ) { $net = (float) $o->get_total() - (float) $o->get_total_refunded(); $cur = strtoupper( (string) $o->get_currency() ); if ( '' === $cur || '' === $main || $cur === $main ) { $total += $net; } else { $others[ $cur ] = ( $others[ $cur ] ?? 0.0 ) + $net; } } // Paid bookings from booking plugins count too (a rental site may sell extras in WooCommerce). list( $b_count, $b_value, $b_others ) = Nodebook_Conversions::bookings_total( $contact_id, $main ); foreach ( $b_others as $cur => $v ) { $others[ $cur ] = ( $others[ $cur ] ?? 0.0 ) + $v; } $contact = Nodebook_Contacts::get( $contact_id ); $data = array( 'orders_count' => count( $orders ) + $b_count, 'total_spent' => $total + $b_value, 'other_spent' => $others ? $others : '', ); if ( $contact && count( $orders ) > 0 && in_array( $contact['status'], array( 'subscriber', 'lead', 'prospect', '' ), true ) ) { $data['status'] = 'customer'; Nodebook_Activities::add( $contact_id, 'status', __( 'Became a customer', 'nodebook' ), array( 'user_id' => 0 ) ); } Nodebook_Contacts::update( $contact_id, $data ); } /** * Batch import of past orders (oldest first). Returns progress. */ /** * Import a customer's earlier orders (by billing email) into the timeline. Runs once per contact * on creation, and on the profile when the order count says orders are missing. */ public static function history( $contact_id, $email, $skip_id = 0 ) { static $running = false; if ( $running || ! self::active() || ! function_exists( 'wc_get_orders' ) || ! is_email( $email ) ) { return 0; } $running = true; $orders = wc_get_orders( array( 'type' => 'shop_order', 'billing_email' => $email, 'limit' => 100, 'orderby' => 'date', 'order' => 'ASC', 'exclude' => $skip_id ? array( (int) $skip_id ) : array(), // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- one order, on a query limited to one customer's orders. ) ); $n = 0; foreach ( (array) $orders as $o ) { if ( ! Nodebook_Activities::find( 'order', $o->get_id() ) ) { self::sync( $o ); ++$n; } } $running = false; return $n; } public static function backfill( $page, $per_page = 25 ) { $result = wc_get_orders( array( 'type' => 'shop_order', 'limit' => $per_page, 'paged' => max( 1, (int) $page ), 'orderby' => 'date', 'order' => 'ASC', 'paginate' => true, ) ); foreach ( $result->orders as $order ) { self::sync( $order, true ); } if ( (int) $page >= (int) $result->max_num_pages ) { update_option( 'nodebook_woo_backfilled', time(), false ); } return array( 'done' => (int) $page >= (int) $result->max_num_pages, 'total' => (int) $result->total, 'page' => (int) $page, ); } /* Order screen: link to the CRM contact ----------------------------- */ public static function meta_box() { if ( ! self::active() ) { return; } $screen = function_exists( 'wc_get_page_screen_id' ) ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order'; add_meta_box( 'nodebook-contact', __( 'Nodebook', 'nodebook' ), array( __CLASS__, 'render_meta_box' ), $screen, 'side', 'default' ); } public static function render_meta_box( $post_or_order ) { $order = $post_or_order instanceof WC_Order ? $post_or_order : wc_get_order( $post_or_order->ID ); $c = $order ? Nodebook_Contacts::find_by_email( $order->get_billing_email() ) : null; if ( ! $c ) { echo '
' . esc_html__( 'No contact yet. It is created when the order is placed or its status changes.', 'nodebook' ) . '
'; return; } $url = admin_url( 'admin.php?page=nodebook&action=view&id=' . $c['id'] ); echo '' . esc_html( Nodebook_Contacts::display_name( $c ) ) . '
'; echo '' . esc_html( sprintf( /* translators: 1: orders count, 2: visits count */ __( '%1$d paid orders · %2$d visits', 'nodebook' ), $c['orders_count'], $c['visits_count'] ) ) . '
'; } }