class-platform-checkout-save-user.php
3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* Class PlatformCheckoutSaveUser
*
* @package WooCommerce\Payments\PlatformCheckout
*/
defined( 'ABSPATH' ) || exit;
use WCPay\Platform_Checkout\Platform_Checkout_Utilities;
/**
* Class that adds a section to save new user for platform checkout on the frontend.
*/
class Platform_Checkout_Save_User {
/**
* Platform checkout utilities.
*
* @var Platform_Checkout_Utilities
*/
protected $platform_checkout_util;
/**
* Constructor.
*/
public function __construct() {
$this->platform_checkout_util = new Platform_Checkout_Utilities();
add_action( 'wp_enqueue_scripts', [ $this, 'register_checkout_page_scripts' ] );
add_filter( 'wcpay_metadata_from_order', [ $this, 'maybe_add_userdata_to_metadata' ], 10, 2 );
add_action( 'woocommerce_payment_complete', [ $this, 'maybe_clear_session_key' ], 10, 2 );
}
/**
* Load scripts and styles for checkout page.
*/
public function register_checkout_page_scripts() {
// Don't enqueue checkout page scripts when WCPay isn't available.
$gateways = WC()->payment_gateways->get_available_payment_gateways();
if ( ! isset( $gateways['woocommerce_payments'] ) ) {
return;
}
$script_src_url = plugins_url( 'dist/platform-checkout.js', WCPAY_PLUGIN_FILE );
$style_url = plugins_url( 'dist/platform-checkout.css', WCPAY_PLUGIN_FILE );
$script_asset_path = WCPAY_ABSPATH . 'dist/platform-checkout.asset.php';
$script_asset = file_exists( $script_asset_path ) ? require $script_asset_path : [ 'dependencies' => [] ];
wp_register_style(
'WCPAY_PLATFORM_CHECKOUT',
$style_url,
[ 'wp-components' ],
\WC_Payments::get_file_version( 'dist/platform-checkout.css' )
);
wp_register_script(
'WCPAY_PLATFORM_CHECKOUT',
$script_src_url,
$script_asset['dependencies'],
\WC_Payments::get_file_version( 'dist/platform-checkout.js' ),
true
);
wp_enqueue_style( 'WCPAY_PLATFORM_CHECKOUT' );
wp_enqueue_script( 'WCPAY_PLATFORM_CHECKOUT' );
}
/**
* Checks if the user chose to save their data for platform checkout and adds appropriate metadata.
*
* @param array $metadata Metadata to be saved.
* @param \WC_Order $order Order object.
*
* @return array
*/
public function maybe_add_userdata_to_metadata( $metadata, $order ) {
$should_save_platform_customer = $this->platform_checkout_util->should_save_platform_customer();
$platform_checkout_phone = $this->platform_checkout_util->get_platform_checkout_phone();
if ( $should_save_platform_customer && $platform_checkout_phone ) {
// Add the metadata.
$metadata['platform_checkout_primary_first_name'] = wc_clean( $order->get_billing_first_name() );
$metadata['platform_checkout_primary_last_name'] = wc_clean( $order->get_billing_last_name() );
$metadata['platform_checkout_primary_phone'] = wc_clean( $order->get_billing_phone() );
$metadata['platform_checkout_primary_company'] = wc_clean( $order->get_billing_company() );
$metadata['platform_checkout_secondary_first_name'] = wc_clean( $order->get_shipping_first_name() );
$metadata['platform_checkout_secondary_last_name'] = wc_clean( $order->get_shipping_last_name() );
$metadata['platform_checkout_secondary_phone'] = wc_clean( $order->get_shipping_phone() );
$metadata['platform_checkout_secondary_company'] = wc_clean( $order->get_shipping_company() );
$metadata['platform_checkout_phone'] = $platform_checkout_phone;
}
return $metadata;
}
/**
* Clears if platform checkout user data is set.
*
* @return void
*/
public function maybe_clear_session_key() {
$session_data = WC()->session->get( Platform_Checkout_Extension::PLATFORM_CHECKOUT_SESSION_KEY );
if ( ! empty( $session_data ) ) {
WC()->session->__unset( Platform_Checkout_Extension::PLATFORM_CHECKOUT_SESSION_KEY );
}
}
}