@@ -219,6 +219,49 @@ public function get_customer_id_from_buyer( $buyer_id ) {
219
219
return ! empty ( $ customer_id ) ? intval ( $ customer_id ) : false ;
220
220
}
221
221
222
+ public function set_customer_id_for_buyer ( $ buyer_id , $ customer_id ) {
223
+ global $ wpdb ;
224
+ $ this ->maybe_create_index_table ();
225
+
226
+ $ inserted = $ wpdb ->insert (
227
+ "{$ wpdb ->prefix }woocommerce_amazon_buyer_index " ,
228
+ array (
229
+ 'buyer_id ' => $ buyer_id ,
230
+ 'customer_id ' => $ customer_id ,
231
+ )
232
+ );
233
+
234
+ if ( ! $ inserted ) {
235
+ return false ;
236
+ }
237
+
238
+ return true ;
239
+ }
240
+
241
+ public function signal_account_hijack () {
242
+ add_filter ( 'woocommerce_checkout_customer_id ' , array ( $ this , 'handle_account_registration ' ) );
243
+ }
244
+
245
+ public function handle_account_registration ( $ customer_id ) {
246
+ // unhook ourselves, since we only need this after checkout started, not every time
247
+ remove_filter ( 'woocommerce_checkout_customer_id ' , array ( $ this , 'handle_account_registration ' ) );
248
+
249
+ $ checkout = WC ()->checkout ();
250
+ $ data = $ checkout ->get_posted_data ();
251
+
252
+ if ( $ customer_id && ! empty ( $ data ['amazon_link ' ] ) ) {
253
+ $ checkout_session = $ this ->get_checkout_session ();
254
+ $ buyer_id = $ checkout_session ->buyer ->buyerId ;
255
+
256
+ $ buyer_user_id = $ this ->get_customer_id_from_buyer ( $ buyer_id );
257
+ if ( ! $ buyer_user_id ) {
258
+ $ this ->set_customer_id_for_buyer ( $ buyer_id , $ customer_id );
259
+ }
260
+ }
261
+
262
+ return $ customer_id ;
263
+ }
264
+
222
265
public function checkout_init ( $ checkout ) {
223
266
224
267
/**
@@ -238,6 +281,8 @@ public function checkout_init( $checkout ) {
238
281
return ;
239
282
}
240
283
284
+ add_action ( 'woocommerce_checkout_process ' , array ( $ this , 'signal_account_hijack ' ) );
285
+
241
286
// If all prerequisites are meet to be an amazon checkout.
242
287
do_action ( 'woocommerce_amazon_checkout_init ' );
243
288
@@ -572,6 +617,34 @@ public function display_amazon_customer_info() {
572
617
<div id="wc-apa-account-fields-anchor"></div>
573
618
574
619
<?php endif ; ?>
620
+
621
+ <?php if ( is_user_logged_in () ) : ?>
622
+ <?php
623
+ $ checkout_session = $ this ->get_checkout_session ();
624
+ $ buyer_id = $ checkout_session ->buyer ->buyerId ;
625
+ $ buyer_email = $ checkout_session ->buyer ->email ;
626
+
627
+ $ buyer_user_id = $ this ->get_customer_id_from_buyer ( $ buyer_id );
628
+ ?>
629
+ <?php if ( ! $ buyer_user_id ) : ?>
630
+ <div class="woocommerce-account-fields">
631
+ <div class="link-account">
632
+ <?php
633
+ $ key = 'amazon_link ' ;
634
+ woocommerce_form_field (
635
+ $ key ,
636
+ array (
637
+ 'type ' => 'checkbox ' ,
638
+ 'label ' => __ ( 'Link Amazon Pay Account ' , 'woocommerce-gateway-amazon-payments-advanced ' ),
639
+ ),
640
+ $ checkout ->get_value ( $ key )
641
+ );
642
+ ?>
643
+ <div class="clear"></div>
644
+ </div>
645
+ </div>
646
+ <?php endif ; ?>
647
+ <?php endif ; ?>
575
648
</div>
576
649
</div>
577
650
@@ -632,6 +705,10 @@ public function use_checkout_session_data( $data ) {
632
705
633
706
$ data = array_merge ( $ data , array_intersect_key ( $ formatted_session_data , $ data ) ); // only set data that exists in data
634
707
708
+ if ( isset ( $ _REQUEST ['amazon_link ' ] ) ) {
709
+ $ data ['amazon_link ' ] = $ _REQUEST ['amazon_link ' ];
710
+ }
711
+
635
712
return $ data ;
636
713
}
637
714
@@ -656,6 +733,11 @@ public function use_checkout_session_data_single( $ret, $input ) {
656
733
}
657
734
658
735
switch ( $ input ) {
736
+ case 'amazon_link ' :
737
+ if ( isset ( $ _REQUEST [ $ input ] ) ) {
738
+ return $ _REQUEST [ $ input ];
739
+ }
740
+ break ;
659
741
default :
660
742
$ session = $ this ->get_woocommerce_data ();
661
743
0 commit comments