mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
✨ New CheckoutBootstrap for GooglePay
This new module uses previously stored payer details to populate the checkout form on the classic checkout page.
This commit is contained in:
parent
a51748f805
commit
c48c94e09d
3 changed files with 101 additions and 24 deletions
|
@ -0,0 +1,64 @@
|
|||
import { GooglePayStorage } from '../Helper/GooglePayStorage';
|
||||
import { setPayerData } from '../../../../ppcp-button/resources/js/modules/Helper/PayerData';
|
||||
|
||||
const CHECKOUT_FORM_SELECTOR = 'form.woocommerce-checkout';
|
||||
|
||||
export class CheckoutBootstrap {
|
||||
/**
|
||||
* @type {GooglePayStorage}
|
||||
*/
|
||||
#storage;
|
||||
|
||||
/**
|
||||
* @type {null|HTMLFormElement}
|
||||
*/
|
||||
#checkoutForm = null;
|
||||
|
||||
constructor( storage ) {
|
||||
this.#storage = storage;
|
||||
|
||||
this.onFormSubmit = this.onFormSubmit.bind( this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the WooCommerce checkout form element.
|
||||
*
|
||||
* @return {HTMLFormElement|null} The form, or null if not a checkout page.
|
||||
*/
|
||||
get checkoutForm() {
|
||||
if ( null === this.#checkoutForm ) {
|
||||
this.#checkoutForm = document.querySelector(
|
||||
CHECKOUT_FORM_SELECTOR
|
||||
);
|
||||
}
|
||||
|
||||
return this.#checkoutForm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates, if the current page contains a checkout form.
|
||||
*
|
||||
* @return {boolean} True, if a checkout form is present.
|
||||
*/
|
||||
get isPageWithCheckoutForm() {
|
||||
return this.checkoutForm instanceof HTMLElement;
|
||||
}
|
||||
|
||||
init() {
|
||||
if ( ! this.isPageWithCheckoutForm ) {
|
||||
return;
|
||||
}
|
||||
|
||||
const billingData = this.#storage.getPayer();
|
||||
|
||||
if ( billingData ) {
|
||||
setPayerData( billingData );
|
||||
|
||||
this.checkoutForm.addEventListener( 'submit', this.onFormSubmit );
|
||||
}
|
||||
}
|
||||
|
||||
onFormSubmit() {
|
||||
this.#storage.clearPayer();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue