get_meta( self::META_KEY ) ) { return; } $items = array(); foreach ( $order->get_items() as $item ) { $items[] = $item->get_quantity() . ' × ' . $item->get_name(); } $fields = array( array( 'label' => __( 'First name', 'dealroot' ), 'name' => 'billing_first_name', 'type' => 'text', 'value' => $order->get_billing_first_name(), ), array( 'label' => __( 'Last name', 'dealroot' ), 'name' => 'billing_last_name', 'type' => 'text', 'value' => $order->get_billing_last_name(), ), array( 'label' => __( 'Email', 'dealroot' ), 'name' => 'billing_email', 'type' => 'email', 'value' => $order->get_billing_email(), ), array( 'label' => __( 'Phone', 'dealroot' ), 'name' => 'billing_phone', 'type' => 'tel', 'value' => $order->get_billing_phone(), ), array( 'label' => __( 'Company', 'dealroot' ), 'name' => 'billing_company', 'type' => 'text', 'value' => $order->get_billing_company(), ), array( 'label' => __( 'Postcode', 'dealroot' ), 'name' => 'billing_postcode', 'type' => 'text', 'value' => $order->get_billing_postcode(), ), array( 'label' => __( 'City', 'dealroot' ), 'name' => 'billing_city', 'type' => 'text', 'value' => $order->get_billing_city(), ), array( 'label' => __( 'Country', 'dealroot' ), 'name' => 'billing_country', 'type' => 'text', 'value' => $order->get_billing_country(), ), array( 'label' => __( 'Products', 'dealroot' ), 'name' => 'items', 'type' => 'textarea', 'value' => implode( "\n", $items ), ), ); $paid = $order->is_paid(); $lead_id = Capture::submission( self::key(), (string) $order->get_id(), /* translators: %s: order number. */ sprintf( __( 'Order #%s', 'dealroot' ), $order->get_order_number() ), $fields, array( 'status' => $paid ? 'won' : 'new', 'value' => (float) $order->get_total(), 'currency' => $order->get_currency(), 'won_at' => $paid ? current_time( 'mysql', true ) : null, ) ); if ( $lead_id ) { $order->update_meta_data( self::META_KEY, $lead_id ); $order->save_meta_data(); // A checkout form of the theme may have been saved as an enquiry just before. \Dealroot\Orders::merge_attempts( $lead_id ); } } /** * Mirrors the order status on the lead. * * @param int $order_id Order ID. * @param string $from Old status. * @param string $to New status. * @param \WC_Order $order Order. */ public function sync_status( $order_id, $from, $to, $order ) { if ( ! $order instanceof \WC_Order ) { return; } $lead_id = (int) $order->get_meta( self::META_KEY ); if ( ! $lead_id ) { return; } $map = array( 'processing' => 'won', 'completed' => 'won', 'cancelled' => 'lost', 'failed' => 'lost', 'refunded' => 'lost', ); if ( isset( $map[ $to ] ) ) { Leads::update( $lead_id, array( 'status' => $map[ $to ], 'value' => (float) $order->get_total(), ) ); } } }