get_customer_id() ) { return; } $user_id = $order->get_customer_id(); try { $customer = new WC_Customer( $user_id ); } catch ( Exception $e ) { return; } $pending = (bool) get_user_meta( $user_id, self::SYNC_META, true ); $empty = '' === $customer->get_billing_address_1() && '' === $customer->get_shipping_address_1(); if ( ! $pending && ! $empty ) { return; } $copied = array(); foreach ( array( 'billing', 'shipping' ) as $type ) { $address = $order->get_address( $type ); if ( empty( $address['address_1'] ) || '' === trim( $address['address_1'] ) ) { continue; } foreach ( $address as $key => $value ) { $setter = "set_{$type}_{$key}"; if ( '' !== (string) $value && is_callable( array( $customer, $setter ) ) ) { $customer->{$setter}( $value ); } } $copied[] = $type; } // No address on the order yet: keep waiting for a save that has one. if ( ! $copied ) { return; } $customer->save(); delete_user_meta( $user_id, self::SYNC_META ); $this->log( 'addresses_saved', sprintf( 'Saved the %1$s address from order #%2$s to the customer\'s account.', implode( ' and ', $copied ), $order->get_order_number() ), 'user', $user_id ); } private function is_order_screen() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen ) { return false; } $ids = array( 'shop_order' ); if ( function_exists( 'wc_get_page_screen_id' ) ) { $ids[] = wc_get_page_screen_id( 'shop-order' ); } return in_array( $screen->id, $ids, true ); } public function assets() { if ( ! $this->is_order_screen() || ! Bylt_Customers::current_user_can_create() ) { return; } wp_enqueue_script( 'bylt-customer-create', $this->url( 'assets/customer-create.js' ), array( 'jquery' ), BYLT_VERSION, true ); wp_localize_script( 'bylt-customer-create', 'byltCustomerCreate', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'bylt_create_customer' ), 'i18n' => array( 'creating' => __( 'Creating…', 'byltcommerce-toolkit' ), 'create' => __( 'Create customer', 'byltcommerce-toolkit' ), 'failed' => __( 'Something went wrong. Try again.', 'byltcommerce-toolkit' ), 'useThis' => __( 'Use this customer', 'byltcommerce-toolkit' ), /* translators: %s customer label */ 'created' => __( 'Created %s and selected them for this order.', 'byltcommerce-toolkit' ), ), ) ); } /** * @param WC_Order $order */ public function render( $order ) { if ( ! Bylt_Customers::current_user_can_create() ) { return; } ?>
__( 'You do not have permission to create customers.', 'byltcommerce-toolkit' ) ), 403 ); } // phpcs:disable WordPress.Security.NonceVerification $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; $data = array( 'first_name' => isset( $_POST['first_name'] ) ? sanitize_text_field( wp_unslash( $_POST['first_name'] ) ) : '', 'last_name' => isset( $_POST['last_name'] ) ? sanitize_text_field( wp_unslash( $_POST['last_name'] ) ) : '', 'phone' => isset( $_POST['phone'] ) ? sanitize_text_field( wp_unslash( $_POST['phone'] ) ) : '', ); $send = ! empty( $_POST['send_link'] ) && 'false' !== $_POST['send_link']; // phpcs:enable $user_id = Bylt_Customers::create( $email, $data, $send ); if ( is_wp_error( $user_id ) ) { $payload = array( 'message' => $user_id->get_error_message() ); $extra = $user_id->get_error_data(); if ( 'bylt_email_exists' === $user_id->get_error_code() && ! empty( $extra['user_id'] ) ) { $payload['existing'] = array( 'id' => (int) $extra['user_id'], 'label' => html_entity_decode( Bylt_Customers::label( $extra['user_id'] ), ENT_QUOTES, 'UTF-8' ), ); } wp_send_json_error( $payload ); } update_user_meta( $user_id, self::SYNC_META, 1 ); $this->log( 'customer_created', sprintf( 'Created customer %1$s from the order screen%2$s.', $email, $send ? ' and emailed a set-password link' : '' ), 'user', $user_id ); wp_send_json_success( array( 'id' => $user_id, 'label' => html_entity_decode( Bylt_Customers::label( $user_id ), ENT_QUOTES, 'UTF-8' ), 'first_name' => $data['first_name'], 'last_name' => $data['last_name'], 'email' => $email, 'phone' => $data['phone'], ) ); } }