Spaces:
Running
Running
Add order-button.js: Search dropdown Add button + Product page Add button
Browse files- order-button.js +125 -0
order-button.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* V.AI STUDIO — Order Button Injection
|
| 3 |
+
* 1. Adds "Thêm đơn" button to search dropdown results
|
| 4 |
+
* 2. Adds floating "Thêm đơn" button on product detail pages
|
| 5 |
+
*/
|
| 6 |
+
(function(){
|
| 7 |
+
'use strict';
|
| 8 |
+
|
| 9 |
+
// Wait for D and VAI_ORDERS to be ready
|
| 10 |
+
function waitReady(cb){
|
| 11 |
+
var t=setInterval(function(){
|
| 12 |
+
if(typeof D!=='undefined'&&D.length>100){clearInterval(t);cb();}
|
| 13 |
+
},2000);
|
| 14 |
+
setTimeout(function(){clearInterval(t);},20000);
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
function addToLastOrder(product){
|
| 18 |
+
if(!window.VAI_ORDERS||!window.VAI_ORDERS.getAll)return false;
|
| 19 |
+
var orders=window.VAI_ORDERS.getAll();
|
| 20 |
+
if(!orders.length)return false;
|
| 21 |
+
var latest=orders[0];
|
| 22 |
+
var name=product.name||product.n||'';
|
| 23 |
+
var price=product.priceNum||product.pn||0;
|
| 24 |
+
if(!price)price=parseInt((product.price||product.p||'').toString().replace(/[^\d]/g,''))||0;
|
| 25 |
+
latest.items=latest.items||[];
|
| 26 |
+
// Check duplicate
|
| 27 |
+
var sku=product.sku||product.model||product.mod||'';
|
| 28 |
+
for(var i=0;i<latest.items.length;i++){if(latest.items[i].model===sku)return'dup';}
|
| 29 |
+
latest.items.push({name:name,model:sku,image:product.image||product.img||product.i||'',price:price,discPrice:price,qty:1,total:price,note:''});
|
| 30 |
+
latest.grandTotal=(latest.grandTotal||0)+price;
|
| 31 |
+
latest.savedAt=new Date().toISOString();
|
| 32 |
+
window.VAI_ORDERS.add(latest);
|
| 33 |
+
return true;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
/**
|
| 37 |
+
* Override/enhance search dropdown to add "Thêm đơn" buttons
|
| 38 |
+
*/
|
| 39 |
+
function enhanceSearchDropdown(){
|
| 40 |
+
// Watch for dropdown appearance and add buttons
|
| 41 |
+
var observer=new MutationObserver(function(){
|
| 42 |
+
var dropdown=document.getElementById('vai-search-dropdown');
|
| 43 |
+
if(!dropdown||dropdown.style.display==='none')return;
|
| 44 |
+
// Add buttons if not already added
|
| 45 |
+
var items=dropdown.querySelectorAll('.vai-dd-item:not([data-btn-added])');
|
| 46 |
+
items.forEach(function(item){
|
| 47 |
+
item.setAttribute('data-btn-added','1');
|
| 48 |
+
var slug=item.getAttribute('data-slug');
|
| 49 |
+
if(!slug)return;
|
| 50 |
+
// Add "Thêm đơn" button
|
| 51 |
+
var btn=document.createElement('button');
|
| 52 |
+
btn.className='vai-dd-add-btn';
|
| 53 |
+
btn.textContent='+ Đơn';
|
| 54 |
+
btn.style.cssText='padding:4px 8px;background:#003f62;color:#fff;border:none;border-radius:4px;font-size:10px;font-weight:700;cursor:pointer;white-space:nowrap;margin-left:6px;flex-shrink:0';
|
| 55 |
+
btn.addEventListener('click',function(e){
|
| 56 |
+
e.stopPropagation();
|
| 57 |
+
// Find product by slug
|
| 58 |
+
for(var i=0;i<D.length;i++){
|
| 59 |
+
if(D[i]&&D[i].slug===slug){
|
| 60 |
+
var result=addToLastOrder(D[i]);
|
| 61 |
+
if(result===true){btn.textContent='✓';btn.style.background='#059669';}
|
| 62 |
+
else if(result==='dup'){btn.textContent='Đã có';btn.style.background='#94a3b8';}
|
| 63 |
+
else{btn.textContent='Chưa có đơn';btn.style.background='#dc2626';}
|
| 64 |
+
setTimeout(function(){btn.textContent='+ Đơn';btn.style.background='#003f62';},2000);
|
| 65 |
+
break;
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
});
|
| 69 |
+
item.appendChild(btn);
|
| 70 |
+
});
|
| 71 |
+
});
|
| 72 |
+
observer.observe(document.body,{childList:true,subtree:true,attributes:true});
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
/**
|
| 76 |
+
* Add floating "Thêm vào đơn" button on product detail pages
|
| 77 |
+
*/
|
| 78 |
+
function addProductPageButton(){
|
| 79 |
+
var url=window.location.pathname;
|
| 80 |
+
var slugMatch=url.match(/\/san-pham\/([^/]+)/);
|
| 81 |
+
if(!slugMatch)return;
|
| 82 |
+
var slug=slugMatch[1];
|
| 83 |
+
|
| 84 |
+
// Wait for page to load
|
| 85 |
+
setTimeout(function(){
|
| 86 |
+
// Don't add if already exists
|
| 87 |
+
if(document.getElementById('vai-add-order-btn'))return;
|
| 88 |
+
|
| 89 |
+
// Find product
|
| 90 |
+
var product=null;
|
| 91 |
+
for(var i=0;i<D.length;i++){
|
| 92 |
+
if(D[i]&&D[i].slug===slug){product=D[i];break;}
|
| 93 |
+
}
|
| 94 |
+
if(!product)return;
|
| 95 |
+
|
| 96 |
+
// Create floating button
|
| 97 |
+
var btn=document.createElement('button');
|
| 98 |
+
btn.id='vai-add-order-btn';
|
| 99 |
+
btn.innerHTML='🛒 Thêm vào đơn';
|
| 100 |
+
btn.style.cssText='position:fixed;bottom:80px;right:16px;z-index:999;padding:12px 20px;background:linear-gradient(135deg,#003f62,#0369a1);color:#fff;border:none;border-radius:30px;font-size:14px;font-weight:700;cursor:pointer;box-shadow:0 4px 16px rgba(0,63,98,.4);transition:transform .2s,box-shadow .2s';
|
| 101 |
+
btn.onmouseover=function(){this.style.transform='scale(1.05)';this.style.boxShadow='0 6px 20px rgba(0,63,98,.5)';};
|
| 102 |
+
btn.onmouseout=function(){this.style.transform='';this.style.boxShadow='0 4px 16px rgba(0,63,98,.4)';};
|
| 103 |
+
btn.addEventListener('click',function(){
|
| 104 |
+
var result=addToLastOrder(product);
|
| 105 |
+
if(result===true){
|
| 106 |
+
btn.innerHTML='✅ Đã thêm!';btn.style.background='linear-gradient(135deg,#059669,#10b981)';
|
| 107 |
+
setTimeout(function(){btn.innerHTML='🛒 Thêm vào đơn';btn.style.background='linear-gradient(135deg,#003f62,#0369a1)';},2500);
|
| 108 |
+
}else if(result==='dup'){
|
| 109 |
+
btn.innerHTML='ℹ️ Đã có trong đơn';btn.style.background='#64748b';
|
| 110 |
+
setTimeout(function(){btn.innerHTML='🛒 Thêm vào đơn';btn.style.background='linear-gradient(135deg,#003f62,#0369a1)';},2000);
|
| 111 |
+
}else{
|
| 112 |
+
btn.innerHTML='⚠️ Chưa có đơn nào';btn.style.background='#dc2626';
|
| 113 |
+
setTimeout(function(){btn.innerHTML='🛒 Thêm vào đơn';btn.style.background='linear-gradient(135deg,#003f62,#0369a1)';},2000);
|
| 114 |
+
}
|
| 115 |
+
});
|
| 116 |
+
document.body.appendChild(btn);
|
| 117 |
+
},2000);
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
waitReady(function(){
|
| 121 |
+
enhanceSearchDropdown();
|
| 122 |
+
addProductPageButton();
|
| 123 |
+
console.log('✅ Order buttons ready');
|
| 124 |
+
});
|
| 125 |
+
})();
|