(function( $ ) {
preveriLoad();
$(document).ready(function() {
var isHidden = false; // Initialize a flag variable
var borderStyle = "3px solid " + instant_cart.addBarva;
var elementsWithClass = $('.digit-pricing-bulk');
console.log(elementsWithClass)
//Set up for bulk on load
// Loop through each element and log them
$('.digit-pricing-bulk').each(function(index) {
// 'this' refers to the current matching element in the iteration
// Find elements within the current block and apply changes
$(this).find('input[type="radio"].bulk-radio:first').prop('checked', true);
$(this).find('.bulk-radio:checked').parent('.digit-bulk-add').css('background-color', instant_cart.addBarva);
$(this).find('.digit-bulk-img-wrapper').hide();
$(this).find('[data-img="imageWrapper0"]').show();
$(this).find('.discount-mobile').addClass('mobile-green').css('background-color', instant_cart.addBarva);
$(this).find('.discount-mobile:first').removeClass('mobile-green').addClass('mobile-white').css('background-color', '#fff');
$(this).find('.digit-pricing-prefix').addClass('crn-tekst');
$(this).find('.discount-mobile:first').css('color', instant_cart.addBarva);
$(this).find('.digit-pricing-prefix:first').removeClass('crn-tekst').addClass('bel-tekst');
$(this).find('.hide-on-desktop .woocommerce-Price-amount bdi:first').addClass('bel-tekst');
$(this).find('.hide-on-desktop del .woocommerce-Price-amount bdi:first').addClass('bel-tekst');
$(this).find('.digit-pricing-bulk-item-inner:first').addClass('is-white');
$(this).find('.digit-pricing-bulk-item-inner:first').css('border', borderStyle);
});
//Listener for bulk
$('.digit-pricing-bulk-item').on('click', function() {
var radioButton = $(this).find('.bulk-radio');
var selectedRadioId = radioButton.attr('id');
// Uncheck all radio buttons and remove 'is-green' class from all '.digit-bulk-add' elements
$('.bulk-radio').prop('checked', false);
$('.digit-bulk-add').css('background-color', "#fff");
$('.discount-mobile').addClass('mobile-green').removeClass('mobile-white').css('background-color', instant_cart.addBarva); // Toggle classes for discount-mobile
$('.discount-mobile').css('color', "#fff"); // Toggle classes for discount-mobile
$('.hide-on-desktop .woocommerce-Price-amount bdi').removeClass('bel-tekst');
$('.digit-pricing-bulk-item-inner').removeClass('is-white');
$('.digit-pricing-bulk-item-inner').css('border', 'none');
$(this).find('.digit-pricing-bulk-item-inner').css('border', borderStyle);
$(selectedRadioId).each(function(){
$('.digit-pricing-bulk-item').removeClass('bulk-selected-sync');
// Add the bulk-selected class to the parent .digit-pricing-bulk-item of the selected radio button
$(this).closest('.digit-pricing-bulk-item').addClass('bulk-selected-sync');
});
// Set the clicked radio button as checked
radioButton.prop('checked', true);
// Add 'is-green' class to the parent element of the selected radio button
//radioButton.parent('.digit-bulk-add').addClass('is-green');
//$('.digit-bulk-add').css('color', '#FF0000'); // Replace '#FF0000' with your desired color value
radioButton.parent('.digit-bulk-add').css('background-color', instant_cart.addBarva);
radioButton.parent('.digit-bulk-add').css('color', '#fff');
// Hide all image wrappers
$('.digit-bulk-img-wrapper').hide();
// Replace 'radio' with 'imageWrapper' to get the corresponding image wrapper ID
var selectedImageWrapperId = selectedRadioId.replace('radio', 'imageWrapper');
// Show the selected image wrapper
$('[data-img="' + selectedImageWrapperId + '"]').show();
// Toggle classes for discount-mobile inside the selected '.digit-pricing-bulk-item'
$(this).find('.discount-mobile').removeClass('mobile-green').addClass('mobile-white').css('background-color', '#fff');
$(this).find('.discount-mobile').css('color', instant_cart.addBarva);
$(this).find('.hide-on-desktop .woocommerce-Price-amount bdi').addClass('bel-tekst');
//prefix
$('.digit-pricing-prefix').addClass('crn-tekst');
$(this).find('.digit-pricing-prefix').removeClass('crn-tekst').addClass('bel-tekst');
var firstElement = $('.digit-pricing-bulk-item-inner .digit-pricing-bulk-selling-price ins').first();
if (firstElement.length > 0) {
var insText = $(this).find('.digit-pricing-bulk-item-inner .digit-pricing-bulk-selling-price ins').first().text().trim();
}
if(insText){
$('.digit-pricing-cart-amount .cart-amount').text(insText);
}
$(this).find('.digit-pricing-bulk-item-inner').addClass('is-white');
var selectedValue = radioButton.val();
$('.bulk-radio[value="' + selectedValue + '"]').prop('checked', true);
});
$('body #bulk_atc_btn').on('click', function(e) {
// Get selected product value
var selectedProduct = $('body input[name="bulk_offer"]:checked');
console.log(selectedProduct);
// Check if a product is selected
if (selectedProduct !== undefined) {
// Get the selected form
$(selectedProduct).closest("form").submit();
// Submit the selected form
}
});
//Hide sticky when BUY button in view
$.fn.isInViewport = function() {
var elementTop = $(this).offset().top;
var elementBottom = elementTop + $(this).outerHeight();
var viewportTop = $(window).scrollTop();
var viewportBottom = viewportTop + $(window).height();
return elementBottom > viewportTop && elementTop < viewportBottom;
};
$(window).on('resize scroll', function() {
var shouldBeHidden = $('.digit-pricing-bulk-button').toArray().some(function(element) {
return $(element).isInViewport();
});
if (shouldBeHidden && !isHidden) {
// Element(s) are in the viewport, and it's not hidden, so hide it
$('.custom_sticky_add_to_cart').addClass('hidden');
isHidden = true; // Update the flag
} else if (!shouldBeHidden && isHidden) {
// Element(s) are out of the viewport, and it's hidden, so show it
$('.custom_sticky_add_to_cart').removeClass('hidden');
isHidden = false; // Update the flag
}
});
//Cart
$('.digit-pricing-add-to-cart, .sticky_add_to_cart_button').on('click', function (event) {
if ($(this).hasClass('disabled')) {
return;
}
if (typeof shouldLoadLoader !== 'undefined') {
if ($('.single-product .quantity input').val() > shouldLoadLoader[0]) {
return;
}
}
toggleSpinnerCart();
});
//Checkout
$('.checkout-button').on('click', function (event) {
if ($(this).hasClass('disabled')) {
return;
}
if (typeof shouldLoadLoader !== 'undefined') {
if ($('.single-product .quantity input').val() > shouldLoadLoader[0]) {
return;
}
}
toggleSpinnerCheckout();
});
$( 'body input.variation_id' ).change( function() {
if( '' != $(this).val() ) {
var var_id = $(this).val();
$('input[name=variation_id]').each(function() {
$(this).val(var_id);
});
}
});
$(this).on( 'found_variation', function( event, variation ) {
console.log(variation);
symbol = $('.woocommerce-Price-currencySymbol').first().text();
$('.digit-pricing-upper .digit-pricing-price-regular bdi').text(symbol + variation.display_regular_price);
$('.digit-pricing-upper .digit-pricing-price-sale bdi').text(symbol + variation.display_price);
$('.digit-pricing-special').addClass("greyed-out");
var discount_loader = $('.digit-pricing-discount-loader');
var discount_value = $('.digit-pricing-discount-value');
discount_loader.hide();
discount_value.show();
$('.variations select').each(function() {
var value = $(this).val();
$('.variation-attributes input[id="'+value+'"]').prop('checked', true);
});
jQuery.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php",
dataType: "JSON",
data: '&variation_id='+variation.variation_id+'&action=digit_get_variation_bundle_price',
success: function(data) {
if(data.regular_price && data.sale_price && data.discount) {
$('.digit-bundle-price ins').html(data.sale_price);
$('.digit-bundle-del-price del').html(data.regular_price);
discount_loader.hide();
discount_value.show();
var variation_image = $('.woocommerce-product-gallery__image.slide.is-selected').attr('data-thumb');
$('.digit-pricing-thumbnail > img').attr('src', variation_image);
$('.digit-pricing-discount .digit-pricing-discount-value').text(data.discount);
$('.digit-pricing-special').removeClass("greyed-out");
}
}
});
});
$('body .digit-pricing-partner-add-to-cart').each(function() {
$(this).on("click", function() {
var val = $(this).data("partner");
assignPartnerNumber(val, () => {
$('button.digit-pricing-add-to-cart').trigger("click");
});
});
});
const removeAccents = str =>
str.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
$('.variation-attributes > label').on("click", function() {
var attribute = $(this).parent().data("attr");
var option = $(this).find("input[type=radio]").data('val');
console.log(removeAccents(attribute))
var select = $('select[name="'+removeAccents(attribute)+'"]');
select.val(option);
select.trigger("change");
});
$(".digit-pricing-add-to-cart").on("click", function(){
$(".digit-pricing-icon img").attr('src','/wp-content/plugins/digit-pricing/public/assets/images/loading.svg');
});
$(".variant-four-add").on("click", function(){
$(".digit-pricing-icon img").attr('src','/wp-content/plugins/digit-pricing/public/assets/images/loading.svg');
});
$(".digit-pricing-bulk-button").on("click", function(){
$(".digit-pricing-icon-bulk img").attr('src','/wp-content/plugins/digit-pricing/public/assets/images/loading.svg');
});
$(".f-proceed-to-checkout").on("click", function(){
$(".f-proceed-to-checkout div img").attr('src','/wp-content/plugins/digit-pricing/public/assets/images/loading.svg');
});
$(".sticky_add_to_cart_button").on("click", function(){
$(".sticky-icon img").attr('src','/wp-content/plugins/digit-pricing/public/assets/images/loading.svg');
});
/* If browser back button was used, flush cache */
(function () {
window.onpageshow = function(event) {
if (event.persisted) {
window.location.reload();
}
};
})();
});
function assignPartnerNumber(val, _callback) {
$('body .digit-pricing-form input[name="partner"]').each(function() {
$(this).val(val);
});
_callback();
}
//Preveri ob kliku nazaj
function preveriLoad() {
//some code to modify de UI on page loaded
$(".digit-pricing-icon img").attr('src','/wp-content/plugins/digit-pricing/public/assets/images/icon-shopping-cart.svg?v=123');
}
window.onpageshow = function(event) {
preveriLoad();
};
//spinner
// product page buttons
function toggleSpinnerCart() {
$('.spinner').css('top', '0px');
var imgElement = document.querySelector('body > div.spinner > img.skeleton');
if(imgElement){
var screenWidth = $(window).width();
if (screenWidth >= 768) {
imgElement.src = instant_cart.desktopCart;
} else {
imgElement.src = instant_cart.mobileCart;
}
setTimeout(function () {
$('.spinner').css('top', '-999999px');
}, 4000)
}
}
function toggleSpinnerCheckout() {
$('.spinner').css('top', '0px');
var imgElement = document.querySelector('body.woocommerce-cart div.spinner > img.skeleton');
if(imgElement){
var screenWidth = $(window).width();
if (screenWidth >= 768) {
imgElement.src = instant_cart.desktopCheckout;
} else {
imgElement.src = instant_cart.mobileCheckout;
}
setTimeout(function () {
$('.spinner').css('top', '-999999px');
}, 4000)
}
}
})( jQuery );