Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit a487dcf

Browse files
authored
Merge pull request #482 from woocommerce/update/store-checkout-form-data-in-customer-session
Store checkout form data in customer session, for pre-filling PayPal form
2 parents 9825dd2 + f52e141 commit a487dcf

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

includes/class-wc-gateway-ppec-cart-handler.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function wc_ajax_start_checkout() {
147147
}
148148

149149
/**
150-
* Report validation errors if any, or else proceed with checkout flow.
150+
* Report validation errors if any, or else save form data in session and proceed with checkout flow.
151151
*
152152
* @since 1.6.4
153153
*/
@@ -161,6 +161,7 @@ public function maybe_start_checkout( $data, $errors = null ) {
161161
}
162162

163163
if ( empty( $error_messages ) ) {
164+
$this->set_customer_data( $_POST );
164165
$this->start_checkout();
165166
} else {
166167
wp_send_json_error( array( 'messages' => $error_messages ) );
@@ -182,6 +183,72 @@ protected function start_checkout() {
182183
}
183184
}
184185

186+
/**
187+
* Store checkout form data in customer session.
188+
*
189+
* @since 1.6.4
190+
*/
191+
protected function set_customer_data( $data ) {
192+
$customer = WC()->customer;
193+
194+
$billing_first_name = empty( $data[ 'billing_first_name' ] ) ? '' : wc_clean( $data[ 'billing_first_name' ] );
195+
$billing_last_name = empty( $data[ 'billing_last_name' ] ) ? '' : wc_clean( $data[ 'billing_last_name' ] );
196+
$billing_address_1 = empty( $data[ 'billing_address_1' ] ) ? '' : wc_clean( $data[ 'billing_address_1' ] );
197+
$billing_address_2 = empty( $data[ 'billing_address_2' ] ) ? '' : wc_clean( $data[ 'billing_address_2' ] );
198+
$billing_city = empty( $data[ 'billing_city' ] ) ? '' : wc_clean( $data[ 'billing_city' ] );
199+
$billing_state = empty( $data[ 'billing_state' ] ) ? '' : wc_clean( $data[ 'billing_state' ] );
200+
$billing_postcode = empty( $data[ 'billing_postcode' ] ) ? '' : wc_clean( $data[ 'billing_postcode' ] );
201+
$billing_country = empty( $data[ 'billing_country' ] ) ? '' : wc_clean( $data[ 'billing_country' ] );
202+
203+
if ( isset( $data['ship_to_different_address'] ) ) {
204+
$shipping_first_name = empty( $data[ 'shipping_first_name' ] ) ? '' : wc_clean( $data[ 'shipping_first_name' ] );
205+
$shipping_last_name = empty( $data[ 'shipping_last_name' ] ) ? '' : wc_clean( $data[ 'shipping_last_name' ] );
206+
$shipping_address_1 = empty( $data[ 'shipping_address_1' ] ) ? '' : wc_clean( $data[ 'shipping_address_1' ] );
207+
$shipping_address_2 = empty( $data[ 'shipping_address_2' ] ) ? '' : wc_clean( $data[ 'shipping_address_2' ] );
208+
$shipping_city = empty( $data[ 'shipping_city' ] ) ? '' : wc_clean( $data[ 'shipping_city' ] );
209+
$shipping_state = empty( $data[ 'shipping_state' ] ) ? '' : wc_clean( $data[ 'shipping_state' ] );
210+
$shipping_postcode = empty( $data[ 'shipping_postcode' ] ) ? '' : wc_clean( $data[ 'shipping_postcode' ] );
211+
$shipping_country = empty( $data[ 'shipping_country' ] ) ? '' : wc_clean( $data[ 'shipping_country' ] );
212+
} else {
213+
$shipping_first_name = $billing_first_name;
214+
$shipping_last_name = $billing_last_name;
215+
$shipping_address_1 = $billing_address_1;
216+
$shipping_address_2 = $billing_address_2;
217+
$shipping_city = $billing_city;
218+
$shipping_state = $billing_state;
219+
$shipping_postcode = $billing_postcode;
220+
$shipping_country = $billing_country;
221+
}
222+
223+
$customer->set_shipping_address( $shipping_address_1 );
224+
$customer->set_shipping_address_2( $shipping_address_2 );
225+
$customer->set_shipping_city( $shipping_city );
226+
$customer->set_shipping_state( $shipping_state );
227+
$customer->set_shipping_postcode( $shipping_postcode );
228+
$customer->set_shipping_country( $shipping_country );
229+
230+
if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
231+
$customer->set_address( $billing_address_1 );
232+
$customer->set_address_2( $billing_address_2 );
233+
$customer->set_city( $billing_city );
234+
$customer->set_state( $billing_state );
235+
$customer->set_postcode( $billing_postcode );
236+
$customer->set_country( $billing_country );
237+
} else {
238+
$customer->set_shipping_first_name( $shipping_first_name );
239+
$customer->set_shipping_last_name( $shipping_last_name );
240+
$customer->set_billing_first_name( $billing_first_name );
241+
$customer->set_billing_last_name( $billing_last_name );
242+
243+
$customer->set_billing_address_1( $billing_address_1 );
244+
$customer->set_billing_address_2( $billing_address_2 );
245+
$customer->set_billing_city( $billing_city );
246+
$customer->set_billing_state( $billing_state );
247+
$customer->set_billing_postcode( $billing_postcode );
248+
$customer->set_billing_country( $billing_country );
249+
}
250+
}
251+
185252
/**
186253
* Display paypal button on the product page.
187254
*

0 commit comments

Comments
 (0)