Remove jQuery dependency from the checkout frontend

This commit is contained in:
Tomek Wytrębowicz 2023-11-22 13:43:58 +01:00 committed by Justin Palmer
parent 58f703211c
commit dd66ba0d39
No known key found for this signature in database
GPG Key ID: ACAB7C35AA2577AF
2 changed files with 12 additions and 6 deletions

View File

@ -1,4 +1,4 @@
( function ( $, wc_order_source_attribution ) {
( function ( wc_order_source_attribution ) {
'use strict';
const params = wc_order_source_attribution.params;
@ -6,6 +6,8 @@
const cookieLifetime = Number( params.lifetime );
const sessionLength = Number( params.session );
const $ = document.querySelector.bind( document );
/**
* Flattens the sbjs.get object into a schema compatible object.
*
@ -56,7 +58,7 @@
if ( sbjs.get ) {
for( const [ key, value ] of Object.entries( wc_order_source_attribution.sbjsDataToSchema( sbjs.get ) ) ) {
$( `input[name="${prefix}${key}"]` ).val( value );
$( `input[name="${prefix}${key}"]` ).value = value;
}
}
};
@ -64,12 +66,16 @@
/**
* Add source values to checkout.
*/
$( document.body ).on( 'init_checkout', () => { setFields(); } );
const previousInitCheckout = document.body.oninit_checkout;
document.body.oninit_checkout = () => {
setFields();
previousInitCheckout();
};
/**
* Add source values to register.
*/
if ( $( '.woocommerce form.register' ).length ) {
if ( $( '.woocommerce form.register' ) !== null ) {
setFields();
}
}
@ -112,4 +118,4 @@
// Run init.
wc_order_source_attribution.initOrderTracking();
}( jQuery, window.wc_order_source_attribution ) );
}( window.wc_order_source_attribution ) );

View File

@ -190,7 +190,7 @@ class SourceAttributionController implements RegisterHooksInterface {
wp_enqueue_script(
'wc-order-source-attribution',
plugins_url( "assets/js/frontend/order-source-attribution{$this->get_script_suffix()}.js", WC_PLUGIN_FILE ),
array( 'jquery', 'sourcebuster-js' ),
array( 'sourcebuster-js' ),
Constants::get_constant( 'WC_VERSION' ),
true
);