Last active
June 15, 2020 18:28
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Gets the custom fields for the label template in Lightspeed Retail: | |
// Function to get custon field values | |
const requestBuilder = async (domain, RAD, endpoint, ID, parameters, isDisplay) => { | |
const url = isDisplay | |
? endpoint == "Label" | |
?`https://${domain}/API/Account/${RAD}/DisplayTemplate/ItemAs${endpoint}/${ID}.json?${parameters}` | |
:`https://${domain}/API/Account/${RAD}/DisplayTemplate/${endpoint}/${ID}.json?${parameters}` | |
:`https://${domain}/API/Account/${RAD}/${endpoint}/${ID}.json?${parameters}` | |
const makeReq = await fetch(url, { credentials: "same-origin" }) | |
return await makeReq.json(); | |
} | |
const getCustomFields = async () => { | |
const domain = window.location.host; | |
const [RAD, labelID] = window.location.href.match(/\d+/g); | |
const { | |
Label: { | |
Item: { | |
itemID: itemID | |
} | |
} | |
} = await requestBuilder(domain, RAD, 'Label', labelID, '', true); | |
const itemWithCustomFields = await requestBuilder(domain, RAD, 'Item', itemID, 'load_relations=["CustomFieldValues"]', false) | |
const customFields = itemWithCustomFields.Item.CustomFieldValues.CustomFieldValue; | |
if (!Array.isArray(itemWithCustomFields.Item.CustomFieldValues.CustomFieldValue)) { | |
customFields = [itemWithCustomFields.Item.CustomFieldValues.CustomFieldValue] | |
} | |
const customFieldResults = customFields.map(data => { | |
return { | |
nameOfField: data.name, | |
valueOfField: data.value | |
} | |
}) | |
console.log(customFieldResults) | |
} | |
// Function to get brand name | |
const getBrandName = async () =>{ | |
const domain = window.location.host; | |
const [RAD, labelID] = window.location.href.match(/\d+/g); | |
const { | |
Label: { | |
Item: { | |
itemID: itemID | |
} | |
} | |
} = await requestBuilder(domain, RAD, 'Label', labelID, '', true); | |
const itemWithBrandDetails = await requestBuilder(domain, RAD, 'Item', itemID, 'load_relations=["Manufacturer"]', false) | |
const brandName = itemWithBrandDetails.Item.hasOwnProperty('Manufacturer') | |
?itemWithBrandDetails.Item.Manufacturer.name: ""; | |
console.log(`The brand name is ${brandName}`) | |
} | |
getCustomFields() | |
getBrandName() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment