square_api_method = 'createCustomer'; // set the customer email as the WP user email, if available try { if ( ! $order->get_user_id() ) { throw new \Exception( 'No user account' ); } $customer = new \WC_Customer( $order->get_user_id() ); $email = $customer->get_email(); } catch ( \Exception $exception ) { // otherwise, use the order billing email $email = $order->get_billing_email(); } $customer_request = new \Square\Models\CreateCustomerRequest(); $customer_request->setGivenName( $order->get_billing_first_name() ); $customer_request->setFamilyName( $order->get_billing_last_name() ); $customer_request->setCompanyName( $order->get_billing_company() ); $customer_request->setEmailAddress( $email ); $customer_request->setPhoneNumber( $order->get_billing_phone() ); if ( $order->get_user_id() ) { $customer_request->setReferenceId( (string) $order->get_user_id() ); } $customer_request->setAddress( self::get_address_from_order( $order ) ); $this->square_request = $customer_request; $this->square_api_args = array( $this->square_request, ); } /** * Gets a billing address model from a WC order. * * @since 2.0.0 * * @param \WC_Order $order order object * @return \Square\Models\Address */ public static function get_address_from_order( \WC_Order $order ) { $address = new \Square\Models\Address(); $address->setFirstName( $order->get_billing_first_name() ); $address->setLastName( $order->get_billing_last_name() ); $address->setAddressLine1( $order->get_billing_address_1() ); $address->setAddressLine2( $order->get_billing_address_2() ); $address->setLocality( $order->get_billing_city() ); $address->setAdministrativeDistrictLevel1( $order->get_billing_state() ); if ( ! empty( $order->payment->postcode ) ) { $address->setPostalCode( $order->payment->postcode ); } else { $address->setPostalCode( $order->get_billing_postcode() ); } if ( $order->get_billing_country() ) { $address->setCountry( $order->get_billing_country() ); } return $address; } }