Skip to content

Instantly share code, notes, and snippets.

@Adrian-Samuel
Last active June 15, 2020 18:28
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Adrian-Samuel/2894d357a8f16a37676795df06f89def to your computer and use it in GitHub Desktop.
// 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