Handle login and change events

This commit is contained in:
Mike Jolley 2018-06-12 15:32:01 +01:00
parent e5192cfeff
commit cfe1cb5c16
2 changed files with 11 additions and 9 deletions

View File

@ -10,7 +10,8 @@ jQuery( function( $ ) {
// Params.
this.params = $.extend( {}, {
'is_registration_required': false
'is_registration_required': false,
'is_logged_in' : false,
}, wc_tokenization_form_params );
// Bind functions to this.
@ -21,7 +22,7 @@ jQuery( function( $ ) {
this.hideSaveNewCheckbox = this.hideSaveNewCheckbox.bind( this );
// When a radio button is changed, make sure to show/hide our new CC info area.
this.$target.on( 'click', ':input.woocommerce-SavedPaymentMethods-tokenInput', { tokenizationForm: this }, this.onTokenChange );
this.$target.on( 'click change', ':input.woocommerce-SavedPaymentMethods-tokenInput', { tokenizationForm: this }, this.onTokenChange );
// OR if create account is checked.
$( 'input#createaccount' ).change( { tokenizationForm: this }, this.onCreateAccountChange );
@ -38,21 +39,21 @@ jQuery( function( $ ) {
// Don't show the "use new" radio button if we only have one method..
if ( 0 === this.$target.data( 'count' ) ) {
$( '.woocommerce-SavedPaymentMethods-new', this.$target ).hide();
$( '.woocommerce-SavedPaymentMethods-new', this.$target ).remove();
}
// Trigger change event
$( ':input.woocommerce-SavedPaymentMethods-tokenInput:checked', this.$target ).trigger( 'change' );
// Hide "save card" if "Create Account" is not checked and registration is not forced.
var hasCreateAccountCheckbox = 0 < $( 'input#createaccount' ).length,
createAccount = hasCreateAccountCheckbox && $('input#createaccount').is( ':checked' );
createAccount = hasCreateAccountCheckbox && $( 'input#createaccount' ).is( ':checked' );
if ( createAccount || this.params.is_registration_required ) {
if ( createAccount || this.params.is_logged_in || this.params.is_registration_required ) {
this.showSaveNewCheckbox();
} else {
this.hideSaveNewCheckbox();
}
// Trigger change event
$( ':input.woocommerce-SavedPaymentMethods-tokenInput:checked', this.$target ).trigger( 'change' );
};
TokenizationForm.prototype.onTokenChange = function( event ) {
@ -97,7 +98,7 @@ jQuery( function( $ ) {
return this;
};
/*
/**
* Initialize.
*/
$( document.body ).on( 'updated_checkout wc-credit-card-form-init', function() {

View File

@ -445,6 +445,7 @@ abstract class WC_Payment_Gateway extends WC_Settings_API {
wp_localize_script(
'woocommerce-tokenization-form', 'wc_tokenization_form_params', array(
'is_registration_required' => WC()->checkout()->is_registration_required(),
'is_logged_in' => is_user_logged_in(),
)
);
}