| 1 |
(function ($, elementor) { |
| 2 |
"use strict"; |
| 3 |
|
| 4 |
// ===== GRID FILTER FUNCTIONALITY ===== |
| 5 |
function setupGridFilter($scope) { |
| 6 |
$scope.find('*[data-filter="yes"]').each(function () { |
| 7 |
const $element = $(this); |
| 8 |
setupTabClickHandlers(); |
| 9 |
loadSavedGridLayout($element); |
| 10 |
}); |
| 11 |
} |
| 12 |
|
| 13 |
function setupTabClickHandlers() { |
| 14 |
$(".tab-option") |
| 15 |
.off("click") |
| 16 |
.on("click", function () { |
| 17 |
const gridColumn = $(this).data("grid-column"); |
| 18 |
localStorage.setItem("usk_grid_data", gridColumn); |
| 19 |
updateActiveTab($(this)); |
| 20 |
updateGridLayout($(this), gridColumn); |
| 21 |
}); |
| 22 |
} |
| 23 |
|
| 24 |
function updateActiveTab($clickedTab) { |
| 25 |
const $header = $clickedTab.closest(".usk-grid-header"); |
| 26 |
$header.find("li").removeClass("usk-tabs-active"); |
| 27 |
$clickedTab.parent().addClass("usk-tabs-active"); |
| 28 |
} |
| 29 |
|
| 30 |
function updateGridLayout($clickedTab, gridColumn) { |
| 31 |
const $grid = $clickedTab.closest(".usk-grid-header").parent().find(".usk-grid"); |
| 32 |
const baseClass = gridColumn !== "usk-list-2" ? "usk-grid usk-grid-layout" : "usk-grid usk-list-layout"; |
| 33 |
$grid.removeClass().addClass(baseClass + " " + gridColumn); |
| 34 |
} |
| 35 |
|
| 36 |
function loadSavedGridLayout($element) { |
| 37 |
const savedGridData = localStorage.getItem("usk_grid_data"); |
| 38 |
if (savedGridData) { |
| 39 |
const $tab = $("[data-grid-column='" + savedGridData + "']"); |
| 40 |
if ($tab.length) { |
| 41 |
$tab.parent().addClass("usk-tabs-active"); |
| 42 |
const baseClass = savedGridData !== "usk-list-2" ? "usk-grid usk-grid-layout" : "usk-grid usk-list-layout"; |
| 43 |
$element.find(".usk-grid").removeClass().addClass(baseClass + " " + savedGridData); |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
// ===== ELEMENTOR INTEGRATION ===== |
| 50 |
$(window).on("elementor/frontend/init", function () { |
| 51 |
// Register widgets |
| 52 |
elementorFrontend.hooks.addAction("frontend/element_ready/usk-shiny-grid.default", setupGridFilter); |
| 53 |
elementorFrontend.hooks.addAction("frontend/element_ready/usk-glossy-grid.default", setupGridFilter); |
| 54 |
elementorFrontend.hooks.addAction("frontend/element_ready/usk-florence-grid.default", setupGridFilter); |
| 55 |
|
| 56 |
// Setup add to cart handling for variations |
| 57 |
setupVariationAddToCart(); |
| 58 |
}); |
| 59 |
|
| 60 |
// Handle variation add to cart |
| 61 |
function setupVariationAddToCart() { |
| 62 |
// Remove any existing handlers to prevent duplicates |
| 63 |
$(document.body).off('click.uskVariationAddToCart', '.usk-button.product_type_variation.add_to_cart_button'); |
| 64 |
|
| 65 |
// Remove WooCommerce's default click handler that might be causing redirects |
| 66 |
$(document.body).off('click', '.add_to_cart_button'); |
| 67 |
|
| 68 |
// Remove href attributes from all variation buttons to prevent default browser navigation |
| 69 |
$('.usk-button.product_type_variation.add_to_cart_button').each(function() { |
| 70 |
$(this).attr('href', 'javascript:void(0)'); |
| 71 |
$(this).attr('data-prevent_redirect', 'true'); |
| 72 |
}); |
| 73 |
|
| 74 |
// Add handler for variation add to cart buttons |
| 75 |
$(document.body).on('click.uskVariationAddToCart', '.usk-button.product_type_variation.add_to_cart_button', function(e) { |
| 76 |
// Always prevent default action to avoid redirect |
| 77 |
e.preventDefault(); |
| 78 |
e.stopPropagation(); |
| 79 |
e.stopImmediatePropagation(); |
| 80 |
|
| 81 |
const $button = $(this); |
| 82 |
|
| 83 |
// Remove href to prevent any chance of redirect |
| 84 |
$button.attr('href', 'javascript:void(0)'); |
| 85 |
|
| 86 |
const productId = $button.data('product_id'); |
| 87 |
const variationId = $button.data('variation_id'); |
| 88 |
|
| 89 |
if (!productId || !variationId) { |
| 90 |
console.log('Missing product ID or variation ID'); |
| 91 |
return false; // Don't proceed without required data |
| 92 |
} |
| 93 |
|
| 94 |
// Prepare data for AJAX add to cart |
| 95 |
const data = { |
| 96 |
product_id: productId, |
| 97 |
variation_id: variationId, |
| 98 |
quantity: 1 |
| 99 |
}; |
| 100 |
|
| 101 |
// Add all attributes |
| 102 |
$.each($button[0].attributes, function(i, attr) { |
| 103 |
if (attr.name.startsWith('data-attribute_')) { |
| 104 |
const attrName = attr.name.substring(5); // Remove 'data-' |
| 105 |
data[attrName] = attr.value; |
| 106 |
} |
| 107 |
}); |
| 108 |
|
| 109 |
// Add the loading class |
| 110 |
$button.addClass('loading'); |
| 111 |
|
| 112 |
// Send AJAX request |
| 113 |
$.ajax({ |
| 114 |
type: 'POST', |
| 115 |
url: usk_ajax_config.ajax_url, |
| 116 |
data: { |
| 117 |
action: 'usk_add_to_cart', |
| 118 |
nonce: usk_ajax_config.nonce, |
| 119 |
...data |
| 120 |
}, |
| 121 |
success: function(response) { |
| 122 |
$button.removeClass('loading'); |
| 123 |
|
| 124 |
if (response.success) { |
| 125 |
// Update fragments |
| 126 |
if (response.fragments) { |
| 127 |
$.each(response.fragments, function(key, value) { |
| 128 |
$(key).replaceWith(value); |
| 129 |
}); |
| 130 |
} |
| 131 |
|
| 132 |
// Trigger events for WC compatibility |
| 133 |
$(document.body).trigger('wc_fragment_refresh'); |
| 134 |
$(document.body).trigger('added_to_cart', [response.fragments, response.cart_hash, $button]); |
| 135 |
|
| 136 |
// Show success message |
| 137 |
const $notification = $('<div class="usk-cart-success-message">Product added to cart! ✓</div>'); |
| 138 |
$button.closest(".usk-item").append($notification); |
| 139 |
|
| 140 |
setTimeout(function() { |
| 141 |
$notification.fadeOut(300, function() { |
| 142 |
$(this).remove(); |
| 143 |
}); |
| 144 |
}, 2000); |
| 145 |
} else { |
| 146 |
console.error('Error adding to cart:', response); |
| 147 |
alert(response.message || 'Error adding product to cart'); |
| 148 |
} |
| 149 |
}, |
| 150 |
error: function() { |
| 151 |
$button.removeClass('loading'); |
| 152 |
alert('Error occurred while adding to cart. Please try again.'); |
| 153 |
}, |
| 154 |
complete: function() { |
| 155 |
// Ensure the href is still void to prevent any redirects |
| 156 |
$button.attr('href', 'javascript:void(0)'); |
| 157 |
} |
| 158 |
}); |
| 159 |
|
| 160 |
return false; |
| 161 |
}); |
| 162 |
|
| 163 |
// Also handle any dynamically added buttons |
| 164 |
$(document.body).on('wc_fragments_refreshed wc_fragments_loaded added_to_cart', function() { |
| 165 |
$('.usk-button.product_type_variation.add_to_cart_button').each(function() { |
| 166 |
$(this).attr('href', 'javascript:void(0)'); |
| 167 |
$(this).attr('data-prevent_redirect', 'true'); |
| 168 |
}); |
| 169 |
}); |
| 170 |
} |
| 171 |
})(jQuery, window.elementorFrontend); |
| 172 |
|