Start typing to see products you are looking for.
Select more than one item for comparison.
;但本段已可單段運作。
/* ---------- ① Bootloader(跨頁轉場:在新頁一載入就顯示深色轉圈圈) ---------- */
(function bootloader(){
try {
if (sessionStorage.getItem('金年会中国官方网站_RQ_LOADING') === '1') {
var css = '#sci-preboot{position:fixed;inset:0;z-index:2147483647;background:rgba(0,0,0,.6);display:flex;align-items:center;justify-content:center}#sci-preboot .spin{width:56px;height:56px;border:5px solid rgba(255,255,255,.35);border-top-color:#fff;border-radius:50%;animation:sciSpin 1s linear infinite;margin:0 auto}@keyframes sciSpin{to{transform:rotate(360deg)}}#sci-preboot .txt{color:#fff;font:600 15px/1.4 system-ui,-apple-system,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif;margin-top:12px;text-align:center}';
var st = document.createElement('style'); st.id='sci-preboot-style'; st.textContent = css; (document.head||document.documentElement).appendChild(st);
var wrap = document.createElement('div'); wrap.id = 'sci-preboot';
wrap.innerHTML = 'The page is loading, please wait...
';
(document.body || document.documentElement).appendChild(wrap);
window.addEventListener('load', function(){
setTimeout(function(){ var w=document.getElementById('sci-preboot'); if(w) w.remove(); var s=document.getElementById('sci-preboot-style'); if(s) s.remove(); sessionStorage.removeItem('金年会中国官方网站_RQ_LOADING'); }, 200);
});
setTimeout(function(){ var w=document.getElementById('sci-preboot'); if(w) w.remove(); var s=document.getElementById('sci-preboot-style'); if(s) s.remove(); sessionStorage.removeItem('金年会中国官方网站_RQ_LOADING'); }, 10000);
}
} catch(e) { console.error("Bootloader Error:", e); }
})();
/* ---------- ② 主流程:比較頁批量加入詢價 (? 最終優化版) ---------- */
(function ($) {
document.addEventListener('DOMContentLoaded', function () {
const isCompare = window.location.pathname.includes('/compare');
const addBtn = document.getElementById('addAllToQuoteBtn');
if (!isCompare || !addBtn) return;
try { const pre = document.createElement('link'); pre.rel = 'prefetch'; pre.href = '/request-quote/'; document.head.appendChild(pre); } catch(_) {}
/* --- Overlay UI --- */
function ensureStyles() {
if (document.getElementById('sci-overlay-style')) return;
const style = document.createElement('style');
style.id = 'sci-overlay-style';
style.textContent = `
#sci-progress-overlay{position:fixed;inset:0;background:rgba(0,0,0,.6);
display:flex;flex-direction:column;align-items:center;justify-content:center;
z-index:2147483646;text-align:center}
#sci-progress-text{color:#fff;font:600 16px/1.5 system-ui,-apple-system,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif}
.sci-spinner{border:5px solid rgba(255,255,255,.3);border-top:5px solid #fff;border-radius:50%;width:56px;height:56px;animation:sciSpin 1s linear infinite;margin:10px auto 0}
@keyframes sciSpin{to{transform:rotate(360deg)}}
`;
document.head.appendChild(style);
}
function showOverlay(innerHTML) {
ensureStyles();
let wrap = document.getElementById('sci-progress-overlay');
if (!wrap) {
wrap = document.createElement('div');
wrap.id = 'sci-progress-overlay';
document.body.appendChild(wrap);
}
wrap.innerHTML = innerHTML;
}
function showSpinner(msg) {
const spinnerHTML = `
${msg || '頁面跳轉中...'}
`;
showOverlay(spinnerHTML);
}
// ? 移除進度條相關函式 (showProgressOverlay, updateProgress),因為不再需要
/* --- 取商品 ID(DOM 優先;REST 備援) --- */
async function getProductIds() {
try {
const root = document.querySelector('.wd-compare-table') || document;
const nodes = Array.from(root.querySelectorAll('[data-product_id],[data-product-id],[data-id]'));
const ids = nodes.map(el => el.getAttribute('data-product_id') || el.getAttribute('data-product-id') || el.getAttribute('data-id'))
.filter(Boolean).map(x => parseInt(x,10)).filter(n => Number.isInteger(n) && n>0);
const uniqueIds = Array.from(new Set(ids));
if (uniqueIds.length) return uniqueIds;
const anchors = Array.from(document.querySelectorAll('.wd-compare-table a'));
const urls = anchors.map(a => a.href).filter(h => h && h.includes('/product/'));
const slugs = Array.from(new Set(urls.map(u => { const m=u.match(/\/product\/([^\/]+)/); return m?decodeURIComponent(m[1]):null; }).filter(Boolean)));
if (!slugs.length) return [];
const response = await fetch(`/wp-json/wp/v2/product?slug=${encodeURIComponent(slugs.join(','))}&per_page=100`);
if (!response.ok) return [];
const arr = await response.json();
return arr.map(p => p.id);
} catch (err) {
console.error("Error getting product IDs:", err);
return [];
}
}
/* --- 單筆加入(三層保險 & 加固版)--- */
async function addOne(id) {
try {
const yithParams = window.yith_ywraq_general_params || {};
const ajaxurl = yithParams.ajaxurl || '/wp-admin/admin-ajax.php';
const bag = yithParams.nonce || yithParams.nonces || {};
const nonces = [bag.add_to_quote, bag.add_item, yithParams.security, yithParams.ajax_nonce].filter(Boolean);
if (window.jQuery) {
const isAdded = await new Promise(resolve => {
const $ = window.jQuery;
const $btn = $('', {
href: '#', 'class': 'add-request-quote-button button yith-wcqv-button yith_ywraq_add_item',
'data-product_id': id, 'data-quantity': 1
}).css({position: 'absolute', left: '-9999px', top: '-9999px'}).appendTo('body');
let settled = false;
const settle = ok => {
if (settled) return;
settled = true;
clearTimeout(timer);
$(document).off('ajaxComplete', ajaxHandler);
$btn.remove();
resolve(!!ok);
};
const timer = setTimeout(() => settle(true), 400);
const ajaxHandler = () => settle(true);
$(document).one('ajaxComplete', ajaxHandler);
setTimeout(() => { $btn.trigger('click'); }, 0);
});
if (isAdded) return true;
}
const baseData = { action: 'yith_ywraq_action', ywraq_action: 'add_item', product_id: id, quantity: 1, context: 'frontend' };
for (const nonce of nonces) {
const data = { ...baseData, security: nonce };
try {
const response = await fetch(ajaxurl, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }, body: new URLSearchParams(data) });
if (response.ok) return true;
} catch (_) {}
}
const wcResponse = await fetch('/?wc-ajax=add_to_quote', {
method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
body: `ywraq_action=add_item&product_id=${encodeURIComponent(id)}&quantity=1`
});
return wcResponse.ok;
} catch (err) {
console.error(`Error adding product ID ${id}:`, err);
return false;
}
}
/* --- 主流程 (最終版:全程轉圈動畫 + 錯誤處理) --- */
addBtn.addEventListener('click', function (e) {
e.preventDefault();
showSpinner('Processing, please wait...');
setTimeout(async () => {
try {
const ids = await getProductIds();
if (!ids.length) {
const box = document.getElementById('sci-progress-text');
if (box) box.textContent = '?? 找不到可加入詢價單的產品。';
setTimeout(() => { document.getElementById('sci-progress-overlay')?.remove(); }, 1200);
return;
}
if (window.yith_ywraq_general_params) {
window.yith_ywraq_general_params.redirect_after_add_to_quote = 'no';
}
const PACE = 800;
for (const id of ids) {
await addOne(id);
await new Promise(r => setTimeout(r, PACE));
}
sessionStorage.setItem('金年会中国官方网站_RQ_LOADING', '1');
showSpinner('Quote list ready. Redirecting...');
requestAnimationFrame(() => requestAnimationFrame(() => {
window.location.assign('/request-quote/');
}));
} catch (err) {
console.error("Add to Quote script failed:", err);
const box = document.getElementById('sci-progress-text');
if (box) box.textContent = '?? 處理時發生錯誤,請聯絡我們。';
setTimeout(() => { document.getElementById('sci-progress-overlay')?.remove(); }, 3000);
}
}, 50);
});
window.addEventListener('beforeunload', function(){
if (document.getElementById('sci-progress-overlay')) {
sessionStorage.setItem('金年会中国官方网站_RQ_LOADING','1');
}
});
});
})(window.jQuery || undefined);
网站地图XML