跳到內容
安裝應用程式
繁體中文

應用程式函式

應用程式定義了多個函式,讓您可以在各處使用回呼與自訂功能。

AI 概覽

列出您可以從自訂佈景主題程式碼呼叫的應用程式函式,用於重新渲染預購元素,以及讀取商品設定檔、付款選項和訊息。

  • 務必檢查 Globo 和 Globo.Preorder 是否存在,或以 try-catch 包住程式碼,這樣即使應用程式被解除安裝,自訂程式碼也不會出錯。
  • Globo.Preorder.initPreorder() 會在變更款式後重新渲染商品頁面;Globo.Preorder.initCollection() 會在篩選、排序或分頁後重新渲染。
  • Globo.Preorder.searchProductByJson() 會依 ID 或 handle 擷取商品,讓您可以加入這些商品並重新渲染商品系列頁面。
  • Globo.Preorder.getResolvedProfile() 需要 productJson 和 variantId,並會告知商品是否為預購、即將推出,或已依日期或數量結束。
  • 設定檔也會回傳付款選項、折扣,以及目前語言的翻譯訊息。

摘要由 AI 產生,可能有誤,請以下方完整指南為準。

* 正確呼叫函式

為確保自訂程式碼始終能正常運作,即使您解除安裝應用程式,新增的程式碼也不會影響佈景主題。這一點非常重要!

// Always check if the application is installed before calling the app function.
if(typeof Globo != 'undefined' && typeof Globo.Preorder != 'undefined'){
// code here
}
  • 您也可以使用 try-catch 區塊包住自訂程式碼,以避免發生例外錯誤。
try{
// code here
} catch (error) {}

1. 在商品頁面重新渲染

在某些情況下,變更子類後,應用程式的佈景主題不會重新渲染。

// after variant changed 
if(typeof Globo != 'undefined' && typeof Globo.Preorder != 'undefined'){
 Globo.Preorder.initPreorder();
}

2. 在商品系列頁面重新渲染

在某些情況下,篩選、排序、分頁商品系列後,應用程式的佈景主題不會重新渲染

// after filtering, paginate, sort 
if(typeof Globo != 'undefined' && typeof Globo.Preorder != 'undefined'){
 Globo.Preorder.initCollection();
}
  • 若要在頁尾推薦商品,請以類似方式使用此程式碼。

3. 輔助函式

1. 搜尋商品

  • Globo.Preorder.searchProductByJson(chunkProducts = [])
  • 範例
const chunkProducts = [
'id:"gid://shopify/Product/123"',
// ...,
'handle:"puma-runner"',
]
const products = await Globo.Preorder.searchProductByJson(chunkProducts)
if(products.length > 0){
// Push data to list product & re-render on collection page
const currentProductIds = new Set(
Globo.Preorder.settings.products.map(product => product.id)
);
const newProducts = products.filter(
product => product && !currentProductIds.has(product.id)
);
Globo.Preorder.settings.products.push(...newProducts);
Globo.Preorder.initCollection();
}

2. 取得商品資料

  • 必須提供 productJson variantId
  • Globo.Preorder.getResolvedProfile(productJson, variantId)
  • 取得商品資料並檢查商品狀態的範例
const profile = Globo.Preorder.getResolvedProfile(productJson, variantId);
if(!profile){
// The current product is not included any profile, or the profile status is draft.
} else if(profile && (typeof profile.isPreorder == 'undefined' || (!profile.isPreorder && (profile?.isComingSoon ?? false)))){
// profile is coming soon add to cart
} else {
// profile is pre-order and
if(!profile.isPreorder && profile.endedByCondition == 'date'){
// Product is ended preorder by date
} else if (!profile.isPreorder && profile.endedByCondition == 'quantity') {
// Product is ended preorder by quantity condition
} else if (!profile.isPreorder && profile.startCondition == 'date' ) {
// Product is preorder and coming soon preorder
} else if (!profile.isPreorder && (profile.startCondition == 'quantity' || profile.startCondition == 'stock')){
// Product is preorder and coming soon preorder
} else if (profile.isPreorder) {
// Product is preorder
}
}

3. 取得預購商品的付款選項與折扣

const profile = Globo.Preorder.getProductProfile(productJson, variantId);
let discountPayment = profile.discountPayment;
// data format discountPayment
// {
// "display": {
// hidePaymentOptions: false, // [true, false]
// showComparePrice: true // [true, false]
// },
// "fullPayment": {
// "enable": true, // [true, false]
// "discountValue": "0", // number
// "discountType": "percentage" // ["percentage", "fixed"]
// },
// "partialPayment": {
// "enable": false, // [true, false]
// "value": "10", // number
// "valueType": "percentage", // ["percentage", "fixed"]
// "discountValue": "0", // number
// "discountType": "percentage", // ["percentage", "fixed"]
// "remainingBalanceChargeTrigger": "EXACT_TIME", // ["MANUAL", "EXACT_TIME", "TIME_AFTER_CHECKOUT"]
// "remainingBalanceChargeExactTime": "2026-08-30", // specific date string - set when remainingBalanceChargeTrigger == "EXACT_TIME"
// "remainingBalanceChargeTimeAfterCheckoutType": "week", // ["day", "week", "month"] - set when remainingBalanceChargeTrigger == "TIME_AFTER_CHECKOUT"
// },
// "display": {
// "hidePaymentOptions": false,
// "showComparePrice": true
// }
// }

4. 取得訊息

  • 依預設翻譯取得訊息,使用 Globo.Preorder.settings.translation
  • 依資料取得訊息,這是依目前語系翻譯後的最終文字
const profile = Globo.Preorder.getResolvedProfile(productJson, variantId);
const message = profile.message;
// data message example
// {
// "preorderText": "Pre Order",
// "messageAboveText": "Don't miss out - Grab yours now before we sell out again!",
// "messageBellowText": "We will fulfill the item as soon as it becomes available",
// "naMessageText": "Not available for Pre order",
// "comingSoonText": "Coming soon",
// "comingSoonAddToCartText": "Coming soon",
// "soldoutText": "Sold out",
// "preorderLimitMessage": "Only :stock items left in stock",
// "outOfStockMessage": "Inventory level is :stock.It is less than you are trying to purchase. Some of the items will be pre-ordered. Continue?",
// "labelCountdownStart": "Coming soon",
// "labelCountdownEnd": "Pre-Order ending in",
// "labelCountdownDays": "Days",
// "labelCountdownHours": "Hours",
// "labelCountdownMinutes": "Minutes",
// "labelCountdownSeconds": "Second",
// "preorderBadge": "Pre Order",
// "preorderLabel": "Pre-order",
// "preorderedItemlabel": "Pre-order item",
// "preorderContactLink": "Reach out for more information →",
// "cartWarningTitle": "Warning: you have pre-order and in-stock in the same cart",
// "cartWarningContent": "Shipment of your in-stock items may be delayed until your pre-order item is ready for shipping.",
// "cartWarningCheckboxLabel": "Don't show this again",
// "partialPaymentLabel": "Payment option",
// "partialPaymentOptionFull": "Full Payment",
// "partialPaymentOptionPart": "Partial Payment",
// "preorderPrice": "PreOrder Price",
// "partialPaymentTitle": "Prepaid amount for {{product.title}} ({{variant.title}})",
// "cartWarningLimitMessage": "There are still :stock pre-order items that can be added to the cart",
// "cartWarningEndLimitMessage": "The number of products that can be pre-ordered has reached the limit",
// "cartWarningAlertMessage": "There are still :stock pre-order items that can be added to the cart. You can purchase up to :max products",
// "bisTextButton": "Email me when available",
// "bisTitleForm": "Email me when available",
// "bisHeaderContent": "Register your email address below to receive an email as soon as this becomes available again",
// "bisPlaceHolderEmail": "Enter your email",
// "bisSubscribeTextButton": "SUBSCRIBE",
// "bisFooterContent": "We will send you an email once the product becomes available. Your email address will not be shared with anyone else",
// "bisSubscribeSuccess": "Your notification has been registered",
// "bisSubscribeFail": "The email address you entered is invalid",
// "bisSubscribeRegistered": "Your email address is registered on this product",
// "bisSubscribeRequired": "The email address is required",
// "bisPhoneRegistered": "Your phone is registered on this product",
// "bisEmailAlertTitle": "Email",
// "bisSmsAlertTitle": "SMS",
// "partialPaymentSaveLavel": "Save",
// "emailPlaceholderBisForm": "Enter your email",
// "partialPaymentReleased": "You will be charged the remaining balance when the product is released on {{ released }}",
// "unsubscribeTitle": "<b>{{ email }}</b><br>will be unsubscribe from the <b>{{ shop_name }}</b> shop listings",
// "unsubscribeButton": "Unsubscribe",
// "unsubscribeSuccess": "You have successfully unsubscribed. Please re-register the product to receive notification",
// "unsubscribeProductUnsubscribed": "You have unsubscribed from this product"
// }

5

這個頁面有幫助嗎?

回報問題