|
|
@@ -14,6 +14,7 @@
|
|
|
var uploadedAttachmentId = null;
|
|
|
var uploadedFileRecordId = null;
|
|
|
var isUploading = false;
|
|
|
+ var originalGalleryImages = {}; // Store original src/srcset to restore on remove
|
|
|
|
|
|
// Upload area elements
|
|
|
var $dropzone = $uploadWrap.find('#studiou-fpp-dropzone');
|
|
|
@@ -27,6 +28,45 @@
|
|
|
var $removeBtn = $uploadWrap.find('.studiou-fpp-remove-file');
|
|
|
var $messages = $uploadWrap.find('.studiou-fpp-messages');
|
|
|
|
|
|
+ // Block all add-to-cart actions until photo is uploaded
|
|
|
+ // Intercept form submissions
|
|
|
+ $(document).on('submit', 'form.cart, form.variations_form', function (e) {
|
|
|
+ if (!uploadedAttachmentId) {
|
|
|
+ e.preventDefault();
|
|
|
+ showMessage(studiouWcfppFront.i18n.uploadFile, 'error');
|
|
|
+ scrollToUpload();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // Intercept add-to-cart button clicks (covers AJAX and non-AJAX buttons)
|
|
|
+ $(document).on('click', '.single_add_to_cart_button, .add_to_cart_button, button[name="add-to-cart"], input[name="add-to-cart"]', function (e) {
|
|
|
+ if (!uploadedAttachmentId) {
|
|
|
+ e.preventDefault();
|
|
|
+ e.stopImmediatePropagation();
|
|
|
+ showMessage(studiouWcfppFront.i18n.uploadFile, 'error');
|
|
|
+ scrollToUpload();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // Intercept add-to-cart links (e.g. ?add-to-cart=ID)
|
|
|
+ $(document).on('click', 'a[href*="add-to-cart"]', function (e) {
|
|
|
+ if (!uploadedAttachmentId) {
|
|
|
+ e.preventDefault();
|
|
|
+ e.stopImmediatePropagation();
|
|
|
+ showMessage(studiouWcfppFront.i18n.uploadFile, 'error');
|
|
|
+ scrollToUpload();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ function scrollToUpload() {
|
|
|
+ $('html, body').animate({
|
|
|
+ scrollTop: $uploadWrap.offset().top - 100
|
|
|
+ }, 400);
|
|
|
+ }
|
|
|
+
|
|
|
// Reset upload after successful WC add-to-cart
|
|
|
$(document.body).on('added_to_cart', function () {
|
|
|
resetUpload();
|
|
|
@@ -144,6 +184,7 @@
|
|
|
);
|
|
|
|
|
|
showPreview(response.data.thumbnail_url, response.data.file_name);
|
|
|
+ updateProductGallery(response.data.preview_url || response.data.thumbnail_url);
|
|
|
$progress.hide();
|
|
|
isUploading = false;
|
|
|
} else {
|
|
|
@@ -195,6 +236,88 @@
|
|
|
$fileInput.val('');
|
|
|
}
|
|
|
|
|
|
+ function updateProductGallery(imageUrl) {
|
|
|
+ if (!imageUrl) return;
|
|
|
+
|
|
|
+ // Find the main product image (try common WC / theme selectors)
|
|
|
+ var selectors = [
|
|
|
+ '.woocommerce-product-gallery__image img',
|
|
|
+ '.product .wp-post-image',
|
|
|
+ '.product-image img',
|
|
|
+ '.product-thumbnail img',
|
|
|
+ '.entry-summary img.attachment-woocommerce_thumbnail',
|
|
|
+ '.product-gallery img:first'
|
|
|
+ ];
|
|
|
+
|
|
|
+ var $img = null;
|
|
|
+ for (var i = 0; i < selectors.length; i++) {
|
|
|
+ $img = $(selectors[i]).first();
|
|
|
+ if ($img.length) break;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$img || !$img.length) return;
|
|
|
+
|
|
|
+ // Save original attributes for restoration
|
|
|
+ if (!originalGalleryImages.src) {
|
|
|
+ originalGalleryImages.src = $img.attr('src');
|
|
|
+ originalGalleryImages.srcset = $img.attr('srcset') || '';
|
|
|
+ originalGalleryImages.sizes = $img.attr('sizes') || '';
|
|
|
+ // Also save the parent link href if it's a gallery link
|
|
|
+ var $parentLink = $img.closest('a');
|
|
|
+ if ($parentLink.length) {
|
|
|
+ originalGalleryImages.href = $parentLink.attr('href');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Replace with uploaded image
|
|
|
+ $img.attr('src', imageUrl);
|
|
|
+ $img.removeAttr('srcset');
|
|
|
+ $img.removeAttr('sizes');
|
|
|
+
|
|
|
+ var $parentLink = $img.closest('a');
|
|
|
+ if ($parentLink.length) {
|
|
|
+ $parentLink.attr('href', imageUrl);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function restoreProductGallery() {
|
|
|
+ if (!originalGalleryImages.src) return;
|
|
|
+
|
|
|
+ var selectors = [
|
|
|
+ '.woocommerce-product-gallery__image img',
|
|
|
+ '.product .wp-post-image',
|
|
|
+ '.product-image img',
|
|
|
+ '.product-thumbnail img',
|
|
|
+ '.entry-summary img.attachment-woocommerce_thumbnail',
|
|
|
+ '.product-gallery img:first'
|
|
|
+ ];
|
|
|
+
|
|
|
+ var $img = null;
|
|
|
+ for (var i = 0; i < selectors.length; i++) {
|
|
|
+ $img = $(selectors[i]).first();
|
|
|
+ if ($img.length) break;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$img || !$img.length) return;
|
|
|
+
|
|
|
+ $img.attr('src', originalGalleryImages.src);
|
|
|
+ if (originalGalleryImages.srcset) {
|
|
|
+ $img.attr('srcset', originalGalleryImages.srcset);
|
|
|
+ }
|
|
|
+ if (originalGalleryImages.sizes) {
|
|
|
+ $img.attr('sizes', originalGalleryImages.sizes);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (originalGalleryImages.href) {
|
|
|
+ var $parentLink = $img.closest('a');
|
|
|
+ if ($parentLink.length) {
|
|
|
+ $parentLink.attr('href', originalGalleryImages.href);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ originalGalleryImages = {};
|
|
|
+ }
|
|
|
+
|
|
|
function showPreview(thumbnailUrl, fileName) {
|
|
|
if (thumbnailUrl) {
|
|
|
$previewImg.attr('src', thumbnailUrl).show();
|
|
|
@@ -228,6 +351,7 @@
|
|
|
uploadedAttachmentId = null;
|
|
|
uploadedFileRecordId = null;
|
|
|
clearSession();
|
|
|
+ restoreProductGallery();
|
|
|
$preview.hide();
|
|
|
$previewImg.attr('src', '');
|
|
|
$previewName.text('');
|