( function ( $ ) {
CartFlowsHelper = {
getUrlParameter: function ( param ) {
var page_url = decodeURIComponent(
window.location.search.substring( 1 )
),
url_variables = page_url.split( '&' ),
parameter_name,
i;
for ( i = 0; i < url_variables.length; i++ ) {
parameter_name = url_variables[ i ].split( '=' );
if ( parameter_name[ 0 ] === param ) {
return parameter_name[ 1 ] === undefined
? true
: parameter_name[ 1 ];
}
}
},
};
var wcf_process_offer = function ( ajax_data ) {
ajax_data._nonce = cartflows_offer[ ajax_data.action + '_nonce' ];
$.ajax( {
url: cartflows.ajax_url,
data: ajax_data,
dataType: 'json',
type: 'POST',
success: function ( data ) {
var msg = data.message;
var msg_class = 'wcf-payment-' + data.status;
$( 'body' ).trigger( 'wcf-update-msg', [ msg, msg_class ] );
setTimeout( function () {
window.location.href = data.redirect;
}, 500 );
},
} );
};
var wcf_offer_button_action = function () {
$( 'a[href*="wcf-up-offer-yes"]' ).each( function ( e ) {
var $this = $( this );
if ( e === 0 ) {
$this.attr( 'id', 'wcf-upsell-offer' );
} else {
$this.attr( 'id', 'wcf-upsell-offer-' + e );
}
} );
$( 'a[href*="wcf-down-offer-yes"]' ).each( function ( e ) {
var $this = $( this );
if ( e === 0 ) {
$this.attr( 'id', 'wcf-downsell-offer' );
} else {
$this.attr( 'id', 'wcf-downsell-offer-' + e );
}
} );
$( document ).on(
'click',
'a[href*="wcf-up-offer"], a[href*="wcf-down-offer"]',
function ( e ) {
e.preventDefault();
var $this = $( this ),
href = $this.attr( 'href' ),
offer_action = 'yes',
offer_type = 'upsell',
step_id = cartflows_offer.step_id,
product_id = cartflows_offer.product_id,
order_id = cartflows_offer.order_id,
order_key = cartflows_offer.order_key,
variation_id = 0,
input_qty = 0,
flow_id = cartflows.current_flow;
if ( href.indexOf( 'wcf-up-offer' ) !== -1 ) {
offer_type = 'upsell';
if ( href.indexOf( 'wcf-up-offer-yes' ) !== -1 ) {
offer_action = 'yes';
} else {
offer_action = 'no';
}
}
if ( href.indexOf( 'wcf-down-offer' ) !== -1 ) {
offer_type = 'downsell';
if ( href.indexOf( 'wcf-down-offer-yes' ) !== -1 ) {
offer_action = 'yes';
} else {
offer_action = 'no';
}
}
if ( 'yes' === offer_action ) {
var variation_wrapper = $( '.wcf-offer-product-variation' );
if ( variation_wrapper.length > 0 ) {
var variation_form = variation_wrapper.find(
'.variations_form'
),
variation_input = variation_form.find(
'input.variation_id'
);
// Set variation id here.
variation_id = parseInt( variation_input.val() );
if (
$( '.var_not_selected' ).length > 0 ||
'' === variation_id ||
0 === variation_id
) {
// variation_form.find('.variations select').addClass('var_not_selected');
variation_form
.find( '.variations select' )
.each( function () {
if ( $( this ).val().length == 0 ) {
$( this ).addClass(
'var_not_selected'
);
}
} );
$( [
document.documentElement,
document.body,
] ).animate(
{
scrollTop:
variation_form
.find( '.variations select' )
.offset().top - 100,
},
1000
);
return false;
}
}
}
if (
'yes' === cartflows_offer.skip_offer &&
'yes' === offer_action
) {
return;
}
$( 'body' ).trigger( 'wcf-show-loader', offer_action );
if ( 'yes' === offer_action ) {
action = 'wcf_' + offer_type + '_accepted';
} else {
action = 'wcf_' + offer_type + '_rejected';
}
var quantity_wrapper = $( '.wcf-offer-product-quantity' );
if ( quantity_wrapper.length > 0 ) {
var quantity_input = quantity_wrapper.find(
'input[name="quantity"]'
);
var quantity_value = parseInt( quantity_input.val() );
if ( quantity_value > 0 ) {
input_qty = quantity_value;
}
}
var ajax_data = {
action: '',
offer_action: offer_action,
offer_type: offer_type,
step_id: step_id,
product_id: product_id,
variation_id: variation_id,
input_qty: input_qty,
order_id: order_id,
order_key: order_key,
flow_id: flow_id,
stripe_sca_payment: false,
stripe_intent_id: '',
_nonce: '',
};
if ( 'yes' === offer_action ) {
if ( 'stripe' === cartflows_offer.payment_method ) {
ajax_data.action = 'wcf_stripe_sca_check';
ajax_data._nonce =
cartflows_offer.wcf_stripe_sca_check_nonce;
$.ajax( {
url: cartflows.ajax_url,
data: ajax_data,
dataType: 'json',
type: 'POST',
success: function ( response ) {
if (
response.hasOwnProperty( 'intent_secret' )
) {
var stripe = Stripe( response.stripe_pk );
stripe
.handleCardPayment(
response.intent_secret
)
.then( function ( response ) {
if ( response.error ) {
throw response.error;
}
if (
'requires_capture' !==
response.paymentIntent
.status &&
'succeeded' !==
response.paymentIntent
.status
) {
return;
}
ajax_data.action = action;
ajax_data.stripe_sca_payment = true;
ajax_data.stripe_intent_id =
response.paymentIntent.id;
wcf_process_offer( ajax_data );
} )
.catch( function ( error ) {
window.location.reload();
} );
} else {
ajax_data.action = action;
wcf_process_offer( ajax_data );
}
},
} );
} else if (
'mollie_wc_gateway_creditcard' ===
cartflows_offer.payment_method ||
'mollie_wc_gateway_ideal' ===
cartflows_offer.payment_method
) {
wcf_handle_mollie_payment_gateways(
ajax_data,
cartflows_offer.payment_method
);
} else {
ajax_data.action = action;
wcf_process_offer( ajax_data );
}
} else {
ajax_data.action = action;
wcf_process_offer( ajax_data );
}
return false;
}
);
};
var wcf_handle_mollie_payment_gateways = function ( ajax_data, gateway ) {
if ( 'mollie_wc_gateway_creditcard' === gateway ) {
ajax_data.action = 'wcf_mollie_creditcard_process';
ajax_data._nonce =
cartflows_offer.wcf_mollie_creditcard_process_nonce;
}
if ( 'mollie_wc_gateway_ideal' === gateway ) {
ajax_data.action = 'wcf_mollie_ideal_process';
ajax_data._nonce = cartflows_offer.wcf_mollie_ideal_process_nonce;
}
$.ajax( {
url: cartflows.ajax_url,
data: ajax_data,
dataType: 'json',
type: 'POST',
success: function ( response ) {
console.log( response );
if ( 'success' === response.result ) {
window.location.href = response.redirect;
} else {
ajax_data.action = action;
wcf_process_offer( ajax_data );
}
},
} );
};
var wcf_offer_quantity_shortcode = function () {
/* Offer Quantity Shortcode */
var offer_quantity = $( '.wcf-offer-product-quantity' );
if ( offer_quantity.length > 0 ) {
/* Sync all variation and quanity */
$( '.wcf-offer-product-quantity .qty' ).on( 'change', function () {
var qty = $( this ).val();
$( '.wcf-offer-product-quantity .qty' ).val( qty );
} );
}
};
var wcf_offer_variation_shortcode = function () {
/* Offer Variation Shortcode */
var offer_variation = $(
'.wcf-offer-product-variation .variations select'
);
if ( offer_variation.length > 0 ) {
$( '.wcf-variable-price-range' ).show().siblings().remove();
// $('.wcf-offer-product-variation input.variation_id').change(function(){
$( '.wcf-offer-product-variation .variations select' ).on(
'change',
function () {
var variation_form = $(
'.wcf-offer-product-variation'
).find( '.variations_form' );
var $this = $( this );
var attr_val = $this.data( 'attribute_name' );
if ( 0 != $this.length ) {
var selected_option = $this.val();
$(
'.wcf-offer-product-variation [data-attribute_name ="' +
attr_val +
'"]'
).each( function () {
$( this ).val( selected_option );
if (
'' == selected_option ||
0 == selected_option
) {
$( this ).addClass( 'var_not_selected' );
} else {
$( this ).removeClass( 'var_not_selected' );
}
} );
}
}
);
Loading ...