@@ -185,6 +185,40 @@ public function display_amazon_pay_button_separator_html() {
185
185
<?php
186
186
}
187
187
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
+
188
222
public function checkout_init ( $ checkout ) {
189
223
190
224
/**
@@ -279,6 +313,20 @@ public function maybe_handle_apa_action() {
279
313
if ( isset ( $ _GET ['amazon_login ' ] ) && isset ( $ _GET ['amazonCheckoutSessionId ' ] ) ) {
280
314
WC ()->session ->set ( 'amazon_checkout_session_id ' , $ _GET ['amazonCheckoutSessionId ' ] );
281
315
$ 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
+
282
330
wp_safe_redirect ( $ redirect_url );
283
331
exit ;
284
332
}
0 commit comments