$options See $options property. */ public function __construct( array $options ) { $this->options = wp_parse_args( $options, array( 'mode' => self::MODE_AUTO, 'link_existing' => false, 'send_email' => true, 'copy_address' => true, 'link_past_orders' => true, ) ); } /** * Convert one order. * * @since 1.0.0 * @param WC_Order $order Order. * @return array{result: string, reason: string, user_id: int} */ public function convert( WC_Order $order ) { if ( $order->get_customer_id() ) { return $this->result( self::RESULT_SKIPPED, __( 'Already has a customer.', 'admin-powerups-for-woocommerce' ) ); } $email = sanitize_email( $order->get_billing_email() ); if ( ! is_email( $email ) ) { return $this->result( self::RESULT_SKIPPED, __( 'No valid email.', 'admin-powerups-for-woocommerce' ) ); } // On multisite this finds network users too, so a non-member's email is never re-created. $existing = get_user_by( 'email', $email ); if ( $existing ) { return $this->convert_existing( $order, $existing ); } $user_id = $this->create_customer( $order, $email ); if ( is_wp_error( $user_id ) ) { return $this->result( self::RESULT_SKIPPED, $user_id->get_error_message() ); } $this->attach( $order, $user_id, true ); return $this->result( self::RESULT_CREATED, '', $user_id ); } /** * Handle an order whose email already belongs to an account. * * WooCommerce never checks that people own the email they register with, so anyone can * create an account with a customer's email. Scheduled conversions therefore never attach an * order to an account that already exists; only staff may do that, from the bulk tool, with * "link existing" on. * * @since 1.0.0 * @param WC_Order $order Order. * @param \WP_User $existing Account with the order's billing email. * @return array{result: string, reason: string, user_id: int} */ private function convert_existing( WC_Order $order, \WP_User $existing ) { if ( self::MODE_MANUAL !== $this->options['mode'] ) { $this->note_once( $order, __( 'Admin Powerups: no account created. An account with this email already exists, so the order stays a guest order.', 'admin-powerups-for-woocommerce' ) ); return $this->result( self::RESULT_SKIPPED, __( 'An account with this email already exists.', 'admin-powerups-for-woocommerce' ) ); } if ( ! $this->options['link_existing'] ) { return $this->result( self::RESULT_SKIPPED, __( 'An account with this email already exists. Linking is off.', 'admin-powerups-for-woocommerce' ) ); } // Never attach guest orders to staff accounts: anyone can type a staff email at checkout. if ( user_can( $existing, 'edit_posts' ) || user_can( $existing, 'manage_woocommerce' ) || is_super_admin( $existing->ID ) ) { return $this->result( self::RESULT_SKIPPED, __( 'Email belongs to a staff account.', 'admin-powerups-for-woocommerce' ) ); } // Multisite: user_can() above only sees this site's roles. Never add network users here. if ( is_multisite() && ! is_user_member_of_blog( $existing->ID, get_current_blog_id() ) ) { return $this->result( self::RESULT_SKIPPED, __( 'Email belongs to an account that is not a member of this site.', 'admin-powerups-for-woocommerce' ) ); } $this->attach( $order, $existing->ID, false ); return $this->result( self::RESULT_LINKED, '', $existing->ID ); } /** * Add a private order note unless the order already has it (a status change can queue the * conversion again). * * @since 1.0.0 * @param WC_Order $order Order. * @param string $text Note. * @return void */ private function note_once( WC_Order $order, $text ) { foreach ( wc_get_order_notes( array( 'order_id' => $order->get_id(), 'type' => 'internal', ) ) as $note ) { if ( $note->content === $text ) { return; } } $order->add_order_note( $text ); } /** * Create the WordPress user. * * @since 1.0.0 * @param WC_Order $order Order. * @param string $email Email. * @return int|\WP_Error */ private function create_customer( WC_Order $order, $email ) { if ( ! $this->options['send_email'] ) { self::$quiet[ strtolower( $email ) ] = true; } $user_id = wc_create_new_customer( $email, '', '', array( 'first_name' => $order->get_billing_first_name(), 'last_name' => $order->get_billing_last_name(), ) ); unset( self::$quiet[ strtolower( $email ) ] ); return $user_id; } /** * Emails of accounts being created right now without a welcome email. * * @var array */ private static $quiet = array(); /** * `woocommerce_created_customer` (priority 1, before WooCommerce sends at 10): mark accounts * that must not get the "New account" email. The mark lives in user meta so it still works * when WooCommerce defers transactional emails to a later request. * * @since 1.0.0 * @param int $user_id New customer ID. * @return void */ public static function mark_quiet( $user_id ) { $user = get_userdata( $user_id ); if ( $user && isset( self::$quiet[ strtolower( $user->user_email ) ] ) ) { update_user_meta( $user_id, self::QUIET_META, 1 ); } } /** * `woocommerce_email_enabled_customer_new_account`: honour the mark. * * @since 1.0.0 * @param bool $enabled Enabled. * @param mixed $user Email object (WP_User for this email). * @return bool */ public static function filter_new_account_email( $enabled, $user = null ) { if ( $user instanceof \WP_User && get_user_meta( $user->ID, self::QUIET_META, true ) ) { return false; } return $enabled; } /** * Link the order (and optionally earlier guest orders) to the account. * * @since 1.0.0 * @param WC_Order $order Order. * @param int $user_id Customer ID. * @param bool $is_new Whether the account was just created. * @return void */ private function attach( WC_Order $order, $user_id, $is_new ) { $order->set_customer_id( $user_id ); $order->add_order_note( $is_new /* translators: %d: user ID. */ ? sprintf( __( 'Admin Powerups: customer account #%d created from this guest order.', 'admin-powerups-for-woocommerce' ), $user_id ) /* translators: %d: user ID. */ : sprintf( __( 'Admin Powerups: guest order linked to existing customer account #%d.', 'admin-powerups-for-woocommerce' ), $user_id ) ); $order->save(); if ( $is_new && $this->options['copy_address'] ) { CustomerAddress::copy_from_order( $order, $user_id, 'billing', true ); if ( $order->has_shipping_address() ) { CustomerAddress::copy_from_order( $order, $user_id, 'shipping', true ); } } // Past guest orders go to accounts created here (their owner gets the login email), or to // an existing account only when staff chose to link it from the bulk tool. if ( $this->options['link_past_orders'] && ( $is_new || self::MODE_MANUAL === $this->options['mode'] ) ) { wc_update_new_customer_past_orders( $user_id ); } /** * Fires after a guest order was attached to a customer account. * * @since 1.0.0 * @param int $user_id Customer ID. * @param WC_Order $order Order. * @param bool $is_new Whether the account was created. */ do_action( 'apwc_guest_order_converted', $user_id, $order, $is_new ); } /** * Build a result array. * * @since 1.0.0 * @param string $result Result code. * @param string $reason Reason (for skips). * @param int $user_id User ID. * @return array{result: string, reason: string, user_id: int} */ private function result( $result, $reason = '', $user_id = 0 ) { return array( 'result' => $result, 'reason' => $reason, 'user_id' => (int) $user_id, ); } }