Skip to content

Commit

Permalink
Pre-fill contact information in PayPal Guest Checkout (#477)
Browse files Browse the repository at this point in the history
* Send phone number from order to PayPal

* Send email address from order to PayPal

* Send email address from customer to PayPal, when initiating flow prior to order creation

* Set and retrieve phone and email on WC()->customer, for pre-filling with SPB

Adds phone and email to the set of values set on the customer during Smart Payment Button initialization, and makes sure phone number is retrieved from customer object to be passed to PayPal. Also slight refactor moving the 'country' lines up to match order of fields.

* Access order and customer properties directly for WC <3.0 case

Supports phone, email, first name, and last name even when accessors are not available on the object.
  • Loading branch information
dechov authored Dec 21, 2018
1 parent 876863b commit e786f36
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
3 changes: 2 additions & 1 deletion includes/class-wc-gateway-ppec-address.php
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,8 @@ public function getAddressParams( $prefix = '' ) {
$prefix . 'CITY' => $this->_city,
$prefix . 'STATE' => $this->_state,
$prefix . 'ZIP' => $this->_zip,
$prefix . 'COUNTRYCODE' => $this->_country
$prefix . 'COUNTRYCODE' => $this->_country,
$prefix . 'PHONENUM' => $this->_phoneNumber,
);

return $params;
Expand Down
23 changes: 17 additions & 6 deletions includes/class-wc-gateway-ppec-cart-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,59 +194,70 @@ protected function set_customer_data( $data ) {

$billing_first_name = empty( $data[ 'billing_first_name' ] ) ? '' : wc_clean( $data[ 'billing_first_name' ] );
$billing_last_name = empty( $data[ 'billing_last_name' ] ) ? '' : wc_clean( $data[ 'billing_last_name' ] );
$billing_country = empty( $data[ 'billing_country' ] ) ? '' : wc_clean( $data[ 'billing_country' ] );
$billing_address_1 = empty( $data[ 'billing_address_1' ] ) ? '' : wc_clean( $data[ 'billing_address_1' ] );
$billing_address_2 = empty( $data[ 'billing_address_2' ] ) ? '' : wc_clean( $data[ 'billing_address_2' ] );
$billing_city = empty( $data[ 'billing_city' ] ) ? '' : wc_clean( $data[ 'billing_city' ] );
$billing_state = empty( $data[ 'billing_state' ] ) ? '' : wc_clean( $data[ 'billing_state' ] );
$billing_postcode = empty( $data[ 'billing_postcode' ] ) ? '' : wc_clean( $data[ 'billing_postcode' ] );
$billing_country = empty( $data[ 'billing_country' ] ) ? '' : wc_clean( $data[ 'billing_country' ] );
$billing_phone = empty( $data[ 'billing_phone' ] ) ? '' : wc_clean( $data[ 'billing_phone' ] );
$billing_email = empty( $data[ 'billing_email' ] ) ? '' : wc_clean( $data[ 'billing_email' ] );

if ( isset( $data['ship_to_different_address'] ) ) {
$shipping_first_name = empty( $data[ 'shipping_first_name' ] ) ? '' : wc_clean( $data[ 'shipping_first_name' ] );
$shipping_last_name = empty( $data[ 'shipping_last_name' ] ) ? '' : wc_clean( $data[ 'shipping_last_name' ] );
$shipping_country = empty( $data[ 'shipping_country' ] ) ? '' : wc_clean( $data[ 'shipping_country' ] );
$shipping_address_1 = empty( $data[ 'shipping_address_1' ] ) ? '' : wc_clean( $data[ 'shipping_address_1' ] );
$shipping_address_2 = empty( $data[ 'shipping_address_2' ] ) ? '' : wc_clean( $data[ 'shipping_address_2' ] );
$shipping_city = empty( $data[ 'shipping_city' ] ) ? '' : wc_clean( $data[ 'shipping_city' ] );
$shipping_state = empty( $data[ 'shipping_state' ] ) ? '' : wc_clean( $data[ 'shipping_state' ] );
$shipping_postcode = empty( $data[ 'shipping_postcode' ] ) ? '' : wc_clean( $data[ 'shipping_postcode' ] );
$shipping_country = empty( $data[ 'shipping_country' ] ) ? '' : wc_clean( $data[ 'shipping_country' ] );
} else {
$shipping_first_name = $billing_first_name;
$shipping_last_name = $billing_last_name;
$shipping_country = $billing_country;
$shipping_address_1 = $billing_address_1;
$shipping_address_2 = $billing_address_2;
$shipping_city = $billing_city;
$shipping_state = $billing_state;
$shipping_postcode = $billing_postcode;
$shipping_country = $billing_country;
}

$customer->set_shipping_country( $shipping_country );
$customer->set_shipping_address( $shipping_address_1 );
$customer->set_shipping_address_2( $shipping_address_2 );
$customer->set_shipping_city( $shipping_city );
$customer->set_shipping_state( $shipping_state );
$customer->set_shipping_postcode( $shipping_postcode );
$customer->set_shipping_country( $shipping_country );

if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
$customer->shipping_first_name = $shipping_first_name;
$customer->shipping_last_name = $shipping_last_name;
$customer->billing_first_name = $billing_first_name;
$customer->billing_last_name = $billing_last_name;

$customer->set_country( $billing_country );
$customer->set_address( $billing_address_1 );
$customer->set_address_2( $billing_address_2 );
$customer->set_city( $billing_city );
$customer->set_state( $billing_state );
$customer->set_postcode( $billing_postcode );
$customer->set_country( $billing_country );
$customer->billing_phone = $billing_phone;
$customer->billing_email = $billing_email;
} else {
$customer->set_shipping_first_name( $shipping_first_name );
$customer->set_shipping_last_name( $shipping_last_name );
$customer->set_billing_first_name( $billing_first_name );
$customer->set_billing_last_name( $billing_last_name );

$customer->set_billing_country( $billing_country );
$customer->set_billing_address_1( $billing_address_1 );
$customer->set_billing_address_2( $billing_address_2 );
$customer->set_billing_city( $billing_city );
$customer->set_billing_state( $billing_state );
$customer->set_billing_postcode( $billing_postcode );
$customer->set_billing_country( $billing_country );
$customer->set_billing_phone( $billing_phone );
$customer->set_billing_email( $billing_email );
}
}

Expand Down
31 changes: 21 additions & 10 deletions includes/class-wc-gateway-ppec-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ public function get_set_express_checkout_params( array $args ) {
)
);

if ( ! empty( $details['email'] ) ) {
$params['EMAIL'] = $details['email'];
}

if ( $args['create_billing_agreement'] ) {
$params['L_BILLINGTYPE0'] = 'MerchantInitiatedBillingSingleAgreement';
$params['L_BILLINGAGREEMENTDESCRIPTION0'] = $this->_get_billing_agreement_description();
Expand Down Expand Up @@ -449,6 +453,7 @@ protected function _get_extra_discount_line_item( $amount ) {
*/
protected function _get_details_from_cart() {
$settings = wc_gateway_ppec()->settings;
$old_wc = version_compare( WC_VERSION, '3.0', '<' );

$decimals = $settings->get_number_of_decimal_digits();
$rounded_total = $this->_get_rounded_total_in_cart();
Expand All @@ -460,6 +465,7 @@ protected function _get_details_from_cart() {
'shipping' => round( WC()->cart->shipping_total, $decimals ),
'items' => $this->_get_paypal_line_items_from_cart(),
'shipping_address' => $this->_get_address_from_customer(),
'email' => $old_wc ? WC()->customer->billing_email : WC()->customer->get_billing_email(),
);

return $this->get_details( $details, $discounts, $rounded_total, WC()->cart->total );
Expand Down Expand Up @@ -703,8 +709,12 @@ protected function _get_details_from_order( $order_id ) {
}
$shipping_address->setCountry( $shipping_country );

$shipping_address->setPhoneNumber( $old_wc ? $order->billing_phone : $order->get_billing_phone() );

$details['shipping_address'] = $shipping_address;

$details['email'] = $old_wc ? $order->billing_email : $order->get_billing_email();

return $details;
}

Expand All @@ -721,8 +731,8 @@ protected function _get_address_from_customer() {
$old_wc = version_compare( WC_VERSION, '3.0', '<' );

if ( $customer->get_shipping_address() || $customer->get_shipping_address_2() ) {
$shipping_first_name = $old_wc ? '' : $customer->get_shipping_first_name();
$shipping_last_name = $old_wc ? '' : $customer->get_shipping_last_name();
$shipping_first_name = $old_wc ? $customer->shipping_first_name : $customer->get_shipping_first_name();
$shipping_last_name = $old_wc ? $customer->shipping_last_name : $customer->get_shipping_last_name();
$shipping_address_1 = $customer->get_shipping_address();
$shipping_address_2 = $customer->get_shipping_address_2();
$shipping_city = $customer->get_shipping_city();
Expand All @@ -732,14 +742,14 @@ protected function _get_address_from_customer() {
} else {
// Fallback to billing in case no shipping methods are set. The address returned from PayPal
// will be stored in the order as billing.
$shipping_first_name = $old_wc ? '' : $customer->get_billing_first_name();
$shipping_last_name = $old_wc ? '' : $customer->get_billing_last_name();
$shipping_address_1 = $old_wc ? $customer->get_address() : $customer->get_billing_address_1();
$shipping_address_2 = $old_wc ? $customer->get_address_2() : $customer->get_billing_address_2();
$shipping_city = $old_wc ? $customer->get_city() : $customer->get_billing_city();
$shipping_state = $old_wc ? $customer->get_state() : $customer->get_billing_state();
$shipping_postcode = $old_wc ? $customer->get_postcode() : $customer->get_billing_postcode();
$shipping_country = $old_wc ? $customer->get_country() : $customer->get_billing_country();
$shipping_first_name = $old_wc ? $customer->billing_first_name : $customer->get_billing_first_name();
$shipping_last_name = $old_wc ? $customer->billing_last_name : $customer->get_billing_last_name();
$shipping_address_1 = $old_wc ? $customer->get_address() : $customer->get_billing_address_1();
$shipping_address_2 = $old_wc ? $customer->get_address_2() : $customer->get_billing_address_2();
$shipping_city = $old_wc ? $customer->get_city() : $customer->get_billing_city();
$shipping_state = $old_wc ? $customer->get_state() : $customer->get_billing_state();
$shipping_postcode = $old_wc ? $customer->get_postcode() : $customer->get_billing_postcode();
$shipping_country = $old_wc ? $customer->get_country() : $customer->get_billing_country();
}

$shipping_address->setName( $shipping_first_name . ' ' . $shipping_last_name );
Expand All @@ -749,6 +759,7 @@ protected function _get_address_from_customer() {
$shipping_address->setState( $shipping_state );
$shipping_address->setZip( $shipping_postcode );
$shipping_address->setCountry( $shipping_country );
$shipping_address->setPhoneNumber( $old_wc ? $customer->billing_phone : $customer->get_billing_phone() );

return $shipping_address;
}
Expand Down

0 comments on commit e786f36

Please sign in to comment.