| 1 |
jQuery(document).ready(function ($) { |
| 2 |
function getAjaxErrorMessage(xhr, fallbackMessage) { |
| 3 |
if (xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message) { |
| 4 |
return xhr.responseJSON.data.message; |
| 5 |
} |
| 6 |
|
| 7 |
if (xhr.responseText) { |
| 8 |
try { |
| 9 |
var parsed = JSON.parse(xhr.responseText); |
| 10 |
if (parsed && parsed.data && parsed.data.message) { |
| 11 |
return parsed.data.message; |
| 12 |
} |
| 13 |
} catch (e) {} |
| 14 |
} |
| 15 |
|
| 16 |
return fallbackMessage; |
| 17 |
} |
| 18 |
|
| 19 |
$(document).on("change", ".category-option", function () { |
| 20 |
if ($(this).is(":checked")) { |
| 21 |
var catID = $(this).val(); |
| 22 |
var nonce = $("#basalam_get_category_attrs_nonce").val(); |
| 23 |
$(".basalam-action-button").attr("data-cat-id", catID); |
| 24 |
$("#sync_basalam_category_attributes") |
| 25 |
.html("در حال دریافت ویژگیها...") |
| 26 |
.removeClass("basalam--hidden"); |
| 27 |
|
| 28 |
$.ajax({ |
| 29 |
url: ajaxurl, |
| 30 |
method: "POST", |
| 31 |
data: { |
| 32 |
action: "basalam_get_category_attrs", |
| 33 |
catID: catID.toString().split(",")[0], |
| 34 |
_wpnonce: nonce, |
| 35 |
}, |
| 36 |
success: function (res) { |
| 37 |
var html = ""; |
| 38 |
if ( |
| 39 |
res.success && |
| 40 |
res.data.attributes |
| 41 |
) { |
| 42 |
var attributesData = JSON.parse(res.data.attributes); |
| 43 |
if (attributesData.data && attributesData.data.length && attributesData.data[0].attributes.length) { |
| 44 |
html += ` |
| 45 |
<div class="basalam-attributes-header">ویژگیهای دستهبندی:</div> |
| 46 |
<div class="basalam-attributes-container"> |
| 47 |
`; |
| 48 |
attributesData.data[0].attributes.forEach(function (attr) { |
| 49 |
html += ` |
| 50 |
<div class="basalam-copy-attr"> |
| 51 |
${attr.title} |
| 52 |
</div> |
| 53 |
`; |
| 54 |
}); |
| 55 |
|
| 56 |
html += "</div>"; |
| 57 |
} else { |
| 58 |
html = "ویژگیای برای این دستهبندی پیدا نشد."; |
| 59 |
} |
| 60 |
} else { |
| 61 |
html = "ویژگیای برای این دستهبندی پیدا نشد."; |
| 62 |
} |
| 63 |
|
| 64 |
$("#sync_basalam_category_attributes").html(html); |
| 65 |
|
| 66 |
$(".basalam-copy-attr").on("click", function () { |
| 67 |
const text = $(this).text().trim(); |
| 68 |
|
| 69 |
if (navigator.clipboard) { |
| 70 |
navigator.clipboard |
| 71 |
.writeText(text) |
| 72 |
.then(function () {}) |
| 73 |
.catch(function (err) { |
| 74 |
console.error("خطا در کپی:", err); |
| 75 |
}); |
| 76 |
} else { |
| 77 |
var tempInput = document.createElement("input"); |
| 78 |
tempInput.value = text; |
| 79 |
document.body.appendChild(tempInput); |
| 80 |
tempInput.select(); |
| 81 |
document.execCommand("copy"); |
| 82 |
document.body.removeChild(tempInput); |
| 83 |
} |
| 84 |
}); |
| 85 |
}, |
| 86 |
error: function (xhr, status, error) { |
| 87 |
var message = getAjaxErrorMessage(xhr, "خطا در دریافت ویژگیها."); |
| 88 |
$("#sync_basalam_category_attributes").html(message); |
| 89 |
}, |
| 90 |
}); |
| 91 |
} else { |
| 92 |
$(".basalam-action-button").removeAttr("data-cat-id"); |
| 93 |
$("#sync_basalam_category_attributes").html(""); |
| 94 |
} |
| 95 |
}); |
| 96 |
|
| 97 |
$("#basalam_fetch_categories_btn").on("click", function () { |
| 98 |
var productTitle = $("#title").val(); |
| 99 |
|
| 100 |
if (!productTitle || productTitle.trim() === "") { |
| 101 |
window.BasalamToast.warning("لطفاً ابتدا عنوان � |
| 102 |
حصول را وارد کنید."); |
| 103 |
$("#title").focus(); |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
var nonce = $("#basalam_get_category_ids_nonce").val(); |
| 108 |
var $btn = $(this); |
| 109 |
var originalText = $btn.text(); |
| 110 |
|
| 111 |
$btn.prop("disabled", true).text("در حال دریافت..."); |
| 112 |
$("#sync_basalam_category_id") |
| 113 |
.html("") |
| 114 |
.removeClass("basalam--hidden"); |
| 115 |
|
| 116 |
$.ajax({ |
| 117 |
url: ajaxurl, |
| 118 |
method: "POST", |
| 119 |
data: { |
| 120 |
action: "basalam_get_category_ids", |
| 121 |
productTitle: productTitle, |
| 122 |
_wpnonce: nonce, |
| 123 |
}, |
| 124 |
success: function (res) { |
| 125 |
if (res.success) { |
| 126 |
var categories = res.data; |
| 127 |
var html = ` |
| 128 |
<div class="basalam-categories-header">دستهبندیهای باسلا� |
| 129 |
:</div> |
| 130 |
<div class="basalam-categories-container"> |
| 131 |
`; |
| 132 |
|
| 133 |
categories.forEach(function (category) { |
| 134 |
html += ` |
| 135 |
<label for="cat_${category.cat_id}" class="basalam-category-label"> |
| 136 |
<input type="radio" class="category-option basalam-category-option" name="selected_category" value="${category.cat_id}" id="cat_${category.cat_id}" /> |
| 137 |
${category.cat_title} |
| 138 |
</label> |
| 139 |
`; |
| 140 |
}); |
| 141 |
|
| 142 |
html += `</div>`; |
| 143 |
$("#sync_basalam_category_id").html(html); |
| 144 |
} else { |
| 145 |
$("#sync_basalam_category_id").html( |
| 146 |
res.data && res.data.message |
| 147 |
? res.data.message |
| 148 |
: "خطا در دریافت دستهبندیها." |
| 149 |
); |
| 150 |
} |
| 151 |
$btn.prop("disabled", false).text(originalText); |
| 152 |
}, |
| 153 |
error: function (xhr, status, error) { |
| 154 |
var message = getAjaxErrorMessage(xhr, "خطا در دریافت دستهبندی خودکار � |
| 155 |
حصول."); |
| 156 |
$("#sync_basalam_category_id").html(message); |
| 157 |
$btn.prop("disabled", false).text(originalText); |
| 158 |
}, |
| 159 |
}); |
| 160 |
}); |
| 161 |
}); |
| 162 |
|