[Shopify]Checkout UI Extensionで設定項目の値を取得
const { banner_status } = useSettings();
const { banner_status } = useSettings();
let j,productId; const cartLines = useCartLines(); const appMetafields = useAppMetafields(); if(appMetafields.length){ cartLines.forEach((cartLine,i)=>{ productId = cartLine.merchandise.product.id.split("/").pop(); for(j=0;j<appMetafields.length;j++){ if(appMetafields[j].target.id === productId){ if(appMetafields[j].metafield.key === "metafieldKey" && appMetafields[j].metafield.value === "metafieldValue"){ //処理 } break; } } }); }
1
const tmp = {name:"abc"}; console.log(tmp.address?.length);
undefined
2
const tmp = {name:"abc"}; console.log(tmp.address?.length+tmp.city?.length);
NaN
3
const tmp = {name:"abc"}; console.log(tmp.address?.length+tmp.city?.length > 4);
false
4
const tmp = {name:"abc"}; console.log(tmp.address?.length+tmp.city?.length == 0);
false
5
const tmp = {name:""}; console.log(tmp.name?.length+tmp.city?.length <= 1);
false
6
const tmp = {name:""}; console.log(tmp.name?.length <= 1);
true
triggerEvent(element, eventName){ const evt = new CustomEvent(eventName, {bubbles:true,cancelable:true}); return element.dispatchEvent(evt); }
isBrowserBack(){ const perfEntries = performance.getEntriesByType("navigation"); let result = false; perfEntries.forEach((perfEntry) => { if(perfEntry.type == 'back_forward'){ result = true; } }); return result; }
getCart(){ return new Promise((resolve,reject) => { const url = 'url'; fetch(url) .then((response)=>{ if (!response.ok) { throw new Error(); } return response.json(); }) .then((json)=>{ resolve(json); }) .catch((error)=>{ reject(error); }); }); } this.getCart() .then((json)=>{ }) .catch((error)=>{ });
loop: for(i=0;i<10;i++){ for(key in item.properties){ if(key === 'break'){ break loop; } } }
文字列から漢字抽出
const str = 'ひらがな小林カタカナ漢字'; const pattern = /(\p{scx=Han}+)/ug; const matches = str.match(pattern); console.log(matches);
出力
(2) ['小林', '漢字']
let sendData = { updates : {} }; this.deleteItems.forEach((item,index)=>{ sendData.updates[item.key] = 0; }); let options = { method : 'POST', credentials: 'same-origin', headers : { 'Content-Type': 'application/json' }, body : JSON.stringify(sendData) }; fetch(window.Shopify.routes.root+'cart/update.js',options) .then((response)=>{ if (!response.ok) { throw new Error(); } return response.json; }) .then((json)=>{ this.finishFetch(2); }) .catch((error)=>{ console.log(error); });
let sendData = { id : item.key, quantity : item.quantity, properties : {} }; item.properties.forEach((property,index)=>{ sendData.properties['プロパティ'+(index+1)] = property.value; }); let options = { method : 'POST', credentials : 'same-origin', headers : { 'Content-Type' : 'application/json' }, body : JSON.stringify(sendData) }; fetch(window.Shopify.routes.root+'cart/change.js',options) .then((response)=>{ if (!response.ok) { throw new Error(); } return response.json; }) .then((json)=>{ this.finishFetch(); }) .catch((error)=>{ console.log(error); });