Created
October 5, 2023 10:01
-
-
Save dev-prakhar/44dfa72f5ca2d0be7a1bef73cef6ec43 to your computer and use it in GitHub Desktop.
Cart Transform using Settings
This file contains hidden or 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
| // @ts-check | |
| // refferred to as utils.js in the gist | |
| import { every, groupBy, isEmpty } from "./utils/methods"; | |
| /** | |
| * @typedef {import("../generated/api").InputQuery} InputQuery | |
| * @typedef {import("../generated/api").FunctionResult} FunctionResult | |
| */ | |
| /** | |
| * @type {FunctionResult} | |
| */ | |
| const NO_CHANGES = { | |
| operations: [] | |
| }; | |
| export default /** | |
| * @param {InputQuery} input | |
| * @returns {FunctionResult} | |
| */ | |
| (input) => { | |
| const settings = JSON.parse(input.cartTransform.settings?.value || "{}") | |
| const getGroupedCartLines = () => { | |
| // Grouping Cart Lines by Product ID | |
| return groupBy(input.cart.lines, line => line.merchandise.product.id) | |
| } | |
| const getMergeOperation = () => { | |
| const groupedProductCartLines = getGroupedCartLines() | |
| // Checking if every Bundled Product is present at least once in the Cart | |
| if(every(settings.bundledProducts.productIds, productId => !isEmpty(groupedProductCartLines[productId]))){ | |
| let parentVariantId = ''; | |
| const bundleCartLines = settings.bundledProducts.productIds.map(productId => { | |
| const productCartLines = groupedProductCartLines[productId] | |
| parentVariantId = productCartLines[0].merchandise?.id | |
| return { | |
| cartLineId: productCartLines[0].id, | |
| quantity: 1 | |
| } | |
| }) | |
| return [{ | |
| merge: { | |
| cartLines: bundleCartLines, | |
| price: { | |
| percentageDecrease: { | |
| value: settings.bundledProducts.discount | |
| } | |
| }, | |
| title: settings.bundledProducts.title, | |
| parentVariantId: parentVariantId | |
| } | |
| }] | |
| } else { | |
| return [] | |
| } | |
| } | |
| return { | |
| operations: getMergeOperation() | |
| } | |
| }; |
This file contains hidden or 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
| query Input { | |
| cart { | |
| lines { | |
| id | |
| quantity | |
| merchandise { | |
| ... on ProductVariant { | |
| id | |
| sku | |
| product { | |
| id | |
| } | |
| } | |
| } | |
| } | |
| } | |
| cartTransform { | |
| settings: metafield(key: "settings", namespace: "bundles") { | |
| value | |
| } | |
| } | |
| } |
This file contains hidden or 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
| mutation SetMetafield { | |
| metafieldsSet(metafields: [ | |
| { | |
| namespace: "bundles", | |
| key: "settings", | |
| ownerId: "gid://shopify/CartTransform/<CART_TRANSFORM_FUNCTION_ID>", | |
| type: "json", | |
| value: "{\"bundledProducts\":{\"title\":\"Snowboard Water Bundle\",\"discount\":15,\"productIds\":[\"gid:\/\/shopify\/Product\/8997752668463\",\"gid:\/\/shopify\/Product\/8997751783727\",\"gid:\/\/shopify\/Product\/8997752439087\"]}}" | |
| } | |
| ]) { | |
| metafields { | |
| id | |
| } | |
| } | |
| } |
This file contains hidden or 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
| // Lodash is not supported inside a function | |
| export const groupBy = (items, fnCallback) => { | |
| let result = {} | |
| items.map(item => { | |
| const key = fnCallback(item) | |
| if (key in result) { | |
| result[key].append(item) | |
| } else { | |
| result[key] = [item] | |
| } | |
| }) | |
| return result | |
| } | |
| export const every = (items, fnCallback) => { | |
| let result = true | |
| items.map(item => { | |
| result = result && fnCallback(item) | |
| }) | |
| return Boolean(result) | |
| } | |
| export const isEmpty = (item) => { | |
| return item === undefined || item === '' || item === null || item === 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment