Skip to content

Commit 954e5c7

Browse files
committed
Log people in after logging in with amazon, if they already have an account.
1 parent d101dec commit 954e5c7

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,40 @@ public function display_amazon_pay_button_separator_html() {
185185
<?php
186186
}
187187

188+
public function maybe_create_index_table() {
189+
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
190+
global $wpdb;
191+
192+
$collate = '';
193+
194+
if ( $wpdb->has_cap( 'collation' ) ) {
195+
$collate = $wpdb->get_charset_collate();
196+
}
197+
198+
$query = "
199+
CREATE TABLE {$wpdb->prefix}woocommerce_amazon_buyer_index (
200+
buyer_id varchar(100) NOT NULL,
201+
customer_id bigint(20) NOT NULL,
202+
PRIMARY KEY (buyer_id,customer_id),
203+
UNIQUE KEY customer_id (customer_id)
204+
) $collate;";
205+
206+
$queries = dbDelta( $query, false );
207+
208+
if ( ! empty( $queries ) ) {
209+
dbDelta( $query );
210+
}
211+
}
212+
213+
public function get_customer_id_from_buyer( $buyer_id ) {
214+
global $wpdb;
215+
$this->maybe_create_index_table();
216+
217+
$customer_id = $wpdb->get_var( $wpdb->prepare( "SELECT customer_id FROM {$wpdb->prefix}woocommerce_amazon_buyer_index WHERE buyer_id = %s", $buyer_id ) );
218+
219+
return ! empty( $customer_id ) ? intval( $customer_id ) : false;
220+
}
221+
188222
public function checkout_init( $checkout ) {
189223

190224
/**
@@ -279,6 +313,20 @@ public function maybe_handle_apa_action() {
279313
if ( isset( $_GET['amazon_login'] ) && isset( $_GET['amazonCheckoutSessionId'] ) ) {
280314
WC()->session->set( 'amazon_checkout_session_id', $_GET['amazonCheckoutSessionId'] );
281315
$this->unset_force_refresh();
316+
WC()->session->save_data();
317+
318+
if ( ! is_user_logged_in() ) {
319+
$checkout_session = $this->get_checkout_session();
320+
$buyer_id = $checkout_session->buyer->buyerId;
321+
$buyer_email = $checkout_session->buyer->email;
322+
323+
$buyer_user_id = $this->get_customer_id_from_buyer( $buyer_id );
324+
325+
if ( ! empty( $buyer_user_id ) ) {
326+
wc_set_customer_auth_cookie( $buyer_user_id );
327+
}
328+
}
329+
282330
wp_safe_redirect( $redirect_url );
283331
exit;
284332
}

0 commit comments

Comments
 (0)