Skip to content

Commit 7e9dad5

Browse files
committed
Allow users to link their Amazon Pay account, on checkout.
1 parent 954e5c7 commit 7e9dad5

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

includes/class-wc-gateway-amazon-payments-advanced.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,49 @@ public function get_customer_id_from_buyer( $buyer_id ) {
219219
return ! empty( $customer_id ) ? intval( $customer_id ) : false;
220220
}
221221

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+
222265
public function checkout_init( $checkout ) {
223266

224267
/**
@@ -238,6 +281,8 @@ public function checkout_init( $checkout ) {
238281
return;
239282
}
240283

284+
add_action( 'woocommerce_checkout_process', array( $this, 'signal_account_hijack' ) );
285+
241286
// If all prerequisites are meet to be an amazon checkout.
242287
do_action( 'woocommerce_amazon_checkout_init' );
243288

@@ -572,6 +617,34 @@ public function display_amazon_customer_info() {
572617
<div id="wc-apa-account-fields-anchor"></div>
573618

574619
<?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; ?>
575648
</div>
576649
</div>
577650

@@ -632,6 +705,10 @@ public function use_checkout_session_data( $data ) {
632705

633706
$data = array_merge( $data, array_intersect_key( $formatted_session_data, $data ) ); // only set data that exists in data
634707

708+
if ( isset( $_REQUEST['amazon_link'] ) ) {
709+
$data['amazon_link'] = $_REQUEST['amazon_link'];
710+
}
711+
635712
return $data;
636713
}
637714

@@ -656,6 +733,11 @@ public function use_checkout_session_data_single( $ret, $input ) {
656733
}
657734

658735
switch ( $input ) {
736+
case 'amazon_link':
737+
if ( isset( $_REQUEST[ $input ] ) ) {
738+
return $_REQUEST[ $input ];
739+
}
740+
break;
659741
default:
660742
$session = $this->get_woocommerce_data();
661743

0 commit comments

Comments
 (0)