Skip to content

Commit

Permalink
Fix shopify loader (#73)
Browse files Browse the repository at this point in the history
Signed-off-by: Marcos Candeia <[email protected]>
  • Loading branch information
mcandeia authored May 17, 2023
1 parent 4996498 commit 667ef16
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions commerce/shopify/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const nonEmptyArray = <T>(array: T[] | null | undefined) =>

export const toProductPage = (
product: ProductShopify,
url: URL,
maybeSkuId?: number,
): ProductDetailsPage => {
const skuId = maybeSkuId
Expand All @@ -53,7 +54,7 @@ export const toProductPage = (
return {
"@type": "ProductDetailsPage",
breadcrumbList: toBreadcrumbList(product, sku),
product: toProduct(product, sku),
product: toProduct(product, sku, url),
};
};

Expand All @@ -76,6 +77,7 @@ export const toBreadcrumbList = (
export const toProduct = (
product: ProductShopify,
sku: SkuShopify,
url: URL,
level = 0, // prevent inifinte loop while self referencing the product
): Product => {
const {
Expand All @@ -100,7 +102,7 @@ export const toProduct = (
const additionalProperty = selectedOptions.map(toPropertyValue);
const allImages = nonEmptyArray([image, ...images.nodes]) ?? [DEFAULT_IMAGE];
const hasVariant = level < 1 &&
variants.nodes.map((variant) => toProduct(product, variant, 1));
variants.nodes.map((variant) => toProduct(product, variant, url, 1));
const priceSpec: UnitPriceSpecification[] = [{
"@type": "UnitPriceSpecification",
priceType: "https://schema.org/SalePrice",
Expand All @@ -118,7 +120,7 @@ export const toProduct = (
return {
"@type": "Product",
productID,
url: getPath(product, sku),
url: `${url.host}${getPath(product, sku)}`,
name: sku.title,
description,
sku: productID,
Expand All @@ -130,7 +132,7 @@ export const toProduct = (
"@type": "ProductGroup",
productGroupID,
hasVariant: hasVariant || [],
url: getPath(product),
url: `${url.host}${getPath(product)}`,
name: product.title,
additionalProperty: [],
},
Expand Down
2 changes: 1 addition & 1 deletion functions/shopifyProductDetailsPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const productPageLoader: LoaderFunction<
};
}

const product = toProductPage(data.product, maybeSkuId);
const product = toProductPage(data.product, new URL(_req.url), maybeSkuId);

return { data: product };
};
Expand Down
2 changes: 1 addition & 1 deletion functions/shopifyProductList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const searchLoader: LoaderFunction<
// If a property is missing from the final `products` array you can add
// it in here
const products = data?.products.nodes.map((p) =>
toProduct(p, p.variants.nodes[0])
toProduct(p, p.variants.nodes[0], new URL(_req.url))
);

return {
Expand Down

0 comments on commit 667ef16

Please sign in to comment.