oont-contents/themes/oont-themev2/assets/js/wc.js
2025-02-08 15:10:23 +01:00

426 lines
14 KiB
JavaScript

var softobaModule = {};
;(function($){
"use strict";
softobaModule.$window = $(window);
softobaModule.$document = $(document);
softobaModule.$body = $('body');
softobaModule.windowWidth = softobaModule.$window.width();
// softobaModule.woocommerceQuantity = function() {
// if (!String.prototype.getDecimals) {
// String.prototype.getDecimals = function() {
// var num = this,
// match = ('' + num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
// if (!match) {
// return 0;
// }
// return Math.max(0, (match[1] ? match[1].length : 0) - (match[2] ? +match[2] : 0));
// };
// }
// softobaModule.$document.on('change', 'form.woocommerce-cart-form .plus, form.woocommerce-cart-form .minus', function() {
// var $this = $(this),
// $qty = $this.closest('.quantity').find('.qty'),
// currentVal = parseFloat($qty.val()),
// max = parseFloat($qty.attr('max')),
// min = parseFloat($qty.attr('min')),
// step = $qty.attr('step');
// console.log($qty);
// if (!currentVal || currentVal === '' || currentVal === 'NaN') {
// currentVal = 0;
// }
// if (max === '' || max === 'NaN') {
// max = '';
// }
// if (min === '' || min === 'NaN') {
// min = 0;
// }
// if (step === 'any' || step === '' || step === undefined || parseFloat(step) == 'NaN') {
// step = '1';
// }
// if ($this.is('.plus')) {
// if (max && (currentVal >= max)) {
// $qty.val(max);
// } else {
// $qty.val((currentVal + parseFloat(step)).toFixed(step.getDecimals()));
// }
// } else {
// if (min && (currentVal <= min)) {
// $qty.val(min);
// } else if (currentVal > 0) {
// $qty.val((currentVal - parseFloat(step)).toFixed(step.getDecimals()));
// }
// }
// // Trigger the 'change' event using both jQuery and vanilla JavaScript
// $this.closest('.quantity').find('.qty').trigger('change');
// // Create a new "change" event
// var changeEvent = new Event('change', { bubbles: true });
// // Get the DOM element from the jQuery object
// var qtyElement = $qty.get(0);
// // Dispatch the "change" event on the element
// qtyElement.dispatchEvent(changeEvent);
// });
// };
if( typeof oont_wc_handle == 'undefined' )
return;
var PS_instance,
xhr,
popup = $('.softoba-static-sidebar'),
overlay = $( '.softoba-overlay'),
close = popup.find( '.softoba-sidebar-close a'),
mini_cart_content = undefined,
close_popup = function(){
// remove class to html
$('html').removeClass( 'softoba_minicart_active' );
$( '.softoba-overlay').removeClass('active');
// remove class open
popup.removeClass( 'oont-active' );
// after 2 sec remove content
setTimeout(function () {
PS_instance.destroy();
}, 1000);
},
handle_add_to_cart_single = function( data, form, button ) {
// $(document).trigger( 'oont_wc_handle_adding_cart_single' );
button.addClass('loading')
.removeClass('added');
$.ajax({
url: window.location,
data: data,
contentType: false,
processData: false,
dataType: 'json',
type: 'POST',
success: function( response ) {
// add error notice if any
if( typeof response.error !== 'undefined' ) {
// add mess and scroll to Top
form.parents( 'div.product' ).before( response.error );
$('body, html').animate({
scrollTop: 0
}, 500);
} else if( typeof response.html !== 'undefined' ) {
// handle_popup_open( response.html );
// mini_cart_content = response.cart_html;
// refresh fragments
$( document.body ).trigger( 'wc_fragment_refresh' ).trigger( 'wacp_single_added_to_cart' );
// update_cart_count( response.items );
} else {
// Trigger event added to cart.
$( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, button ] );
};
},
complete: function(){
// remove disabled from submit button
button.removeAttr( 'disabled')
.removeClass( 'loading')
.addClass('added');
}
});
};
/*######################################
ADD TO CART AJAX IN SINGLE PRODUCT PAGE
########################################*/
$(document).on( 'submit', oont_wc_handle.form_selectors, function( ev ) {
var form = $(this),
button = form.find( 'button[type="submit"]'),
productId = form.find( '[name="add-to-cart"]').val();
if( typeof wc_cart_fragments_params === 'undefined' || $(this).parents('.product-type-external').length === 1 || button.hasClass( 'wcsatt-add-to-subscription-button' ) ) {
return;
}
ev.preventDefault();
// Process Form
var dataForm = new FormData();
$.each( form.find( "input[type='file']" ), function( i, tag ) {
$.each( $(tag)[0].files, function( i, file ) {
dataForm.append( tag.name, file );
});
});
$.each( form.serializeArray(), function( i, val ) {
dataForm.append( val.name, val.value );
});
dataForm.append( 'context', 'frontend' );
dataForm.append( 'action', oont_wc_handle.actionAdd );
dataForm.append( 'softoba_is_single', 'yes' );
// Make sure add-to-cart and product_id are set (useful for simple products)
( ! dataForm.getAll( 'add-to-cart' ).length && productId ) && dataForm.append( 'add-to-cart', productId );
( ! dataForm.getAll( 'product_id' ).length && productId ) && dataForm.append( 'product_id', productId );
handle_add_to_cart_single( dataForm, form, button );
});
// Close sidebar
$(document).on('click', '.softoba-sidebar-close a, .softoba-overlay.active', function(e) {
$('.softoba-static-sidebar').fadeIn(200).removeClass('oont-active');
$('.softoba-overlay').removeClass('active');
});
$(document).ready(function() {
// softobaModule.woocommerceQuantity();
});
// $( document.body ).on( 'click', 'form.woocommerce-cart-form .plus, form.woocommerce-cart-form .minus', function( evt ) {
// // Get values.
// var $qty = $( this ).siblings( '.qty' ),
// currentVal = parseFloat( $qty.val() ),
// max = parseFloat( $qty.attr( 'max' ) ),
// min = parseFloat( $qty.attr( 'min' ) ),
// step = $qty.attr( 'step' );
// // Format values.
// if ( ! currentVal || currentVal === '' || currentVal === 'NaN' ) {
// currentVal = 0;
// }
// if ( max === '' || max === 'NaN' ) {
// max = '';
// }
// if ( min === '' || min === 'NaN' ) {
// min = 0;
// }
// if ( step === 'any' || step === '' || step === undefined || parseFloat( step ) === 'NaN' ) {
// step = 1;
// }
// // Change the value.
// if ( $( this ).is( '.plus' ) ) {
// // alert( currentVal );
// $qty.trigger( 'change' );
// if ( max && max === currentVal ) {
// evt.preventDefault();
// } else {
// if ( max && currentVal > max ) {
// $qty.val( max );
// } else {
// $qty.val( currentVal + parseFloat( step ) );
// }
// $qty.trigger( 'change' );
// }
// } else {
// // alert( 'Minus' );
// $qty.trigger( 'change' );
// if ( min && min === currentVal ) {
// evt.preventDefault();
// } else {
// if ( min && currentVal < min ) {
// $qty.val( min );
// } else if ( currentVal > 0 ) {
// $qty.val( currentVal - parseFloat( step ) );
// }
// $qty.trigger( 'change' );
// }
// }
// } );
// $( document.body ).on( 'blur', 'form.woocommerce-cart-form .quantity .qty', function() {
// var $qty = $( this ),
// currentVal = parseFloat( $qty.val() ),
// max = parseFloat( $qty.attr( 'max' ) );
// console.log(max);
// if ( max !== '' && max !== 'NaN' && currentVal > max ) {
// $( this ).val( max );
// }
// } );
})(jQuery);
jQuery(document).ready(function($){
// 'use strict';
var softobaUpdateTimeout = null;
softobaChange = function(qtyInput) {
// when qty is set to zero, then fires default woocommerce remove link
if ( qtyInput.val() == 0 ) {
var removeLink = qtyInput.closest('.cart_item').find('.product-remove a');
removeLink.trigger('click');
return false;
}
// clear previous timeout, if exists
if ( softobaUpdateTimeout !== null ) {
clearTimeout(softobaUpdateTimeout);
}
softobaUpdateTimeout = setTimeout(function(){
softobaRefreshCart(qtyInput);
}, 500 );
return true;
};
softobaRefreshCart = function(qtyInput) {
// some themes creates multiple cart forms so it will try to find the correct update button
if ( qtyInput.closest('.woocommerce-cart-form').length ) {
var mainSelector = qtyInput.closest('.woocommerce-cart-form');
}
else {
var mainSelector = $(document);
}
// deal with update cart button
var updateButton = mainSelector.find("button[name='update_cart']:not(.dgfw-add-gift-button),input[name='update_cart']:not(.dgfw-add-gift-button)");
updateButton.removeAttr('disabled')
.trigger('click')
.val( 'Updating' )
.prop('disabled', true);
// change the Update cart button
$("a.checkout-button.wc-forward").addClass('disabled').html( 'Updating' );
};
var softobaListenQtyChange = function() {
$(document).on('change', 'form.woocommerce-cart-form .quantity .qty', function(e){
// prevent troubles with WooCommerce Product Bundles plugin
if ( $('.bundle_add_to_cart_button').length ) {
return true;
}
// prevent to set invalid quantity on select
if ( $(this).is('select') && ( $(this).attr('max') > 0 ) &&
( parseInt($(this).val()) > parseInt($(this).attr('max')) ) ) {
$(this).val( $(this).attr('max') );
e.preventDefault();
return false;
}
return softobaChange( $(this) );
});
};
var softobaFindInputQty = function(btn) {
var inputQty = btn.parent().parent().parent().find('.qty');
if ( !inputQty.length ) {
inputQty = btn.closest('.cart').find('.qty');
}
if ( !inputQty.length ) {
inputQty = btn.closest('.product-quantity').find('.qty');
}
return inputQty;
};
softobaListenQtyButtons = function() {
var fnIncrement = function(e){
if ( $(this).hasClass('disabled') ) {
e.preventDefault();
return false;
}
var inputQty = softobaFindInputQty( $(this) );
if ( inputQty.attr('max') != 'undefined' && parseFloat(inputQty.val()) >= parseFloat(inputQty.attr('max')) ) {
return false;
}
inputQty.val( function(i, oldval) { return ++oldval; });
inputQty.trigger('change');
return false;
};
var fnDecrement = function(e){
if ( $(this).hasClass('disabled') ) {
e.preventDefault();
return false;
}
var inputQty = softobaFindInputQty( $(this) );
var oldVal = inputQty.val();
if ( inputQty.attr('min') != 'undefined' && parseInt(inputQty.attr('min')) >= oldVal ) {
return false;
}
inputQty.val( --oldVal );
inputQty.trigger('change');
return false;
};
if ( $('form.woocommerce-cart-form').length ) {
$('form.woocommerce-cart-form .plus').off('click.plus').on('click.plus', fnIncrement);
$('form.woocommerce-cart-form .minus').off('click.minus').on('click.minus', fnDecrement);
if ( $('form.woocommerce-cart-form').length ) {
setTimeout(softobaListenQtyButtons, 500);
}
}
$('form.woocommerce-cart-form .plus').off('click.plus').on('click.plus', fnIncrement);
$('form.woocommerce-cart-form .minus').off('click.minus').on('click.minus', fnDecrement);
};
// onload calls
softobaListenQtyChange();
softobaListenQtyButtons();
$(document.body).on('wc_fragments_refreshed', function(){
softobaListenQtyButtons();
});
});
jQuery(document).ready(function() {
// Find the "Add to Cart" button by class and attach a click event listener
jQuery('.single_add_to_cart_button .button .alt .added').on('click', function() {
// Find the "View Cart" button by class and hide it
jQuery('.added_to_cart wc-forward').hide();
});
});
jQuery(document).ready(function($) {
// Find the label by its `for` attribute
var label = $('#billing_address_1_field label');
// Check if the label exists
if (label.length) {
// Change the label text
label.text('Your Custom Label');
}
});
jQuery(document).ready(function($) {
// Replace 'billing_address_1' with the actual field name for the "Street Address" field.
var streetAddressField = $('input#billing_address_1');
if (streetAddressField.length) {
// Replace 'Your Custom Placeholder Text' with your desired placeholder text.
streetAddressField.attr('placeholder', 'Your Custom Placeholder Text');
}
});