Skip to content

Commit

Permalink
Merge pull request #688 from deco-cx/magento-fix-pdp-gql
Browse files Browse the repository at this point in the history
fix(magento): pdp in uses only gql
  • Loading branch information
aka-sacci-ccr authored Jun 27, 2024
2 parents c71f5d0 + 5a3f049 commit 84bbfa2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 52 deletions.
71 changes: 20 additions & 51 deletions magento/loaders/product/detailsPageGQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
import { formatUrlSuffix, getCustomFields } from "../../utils/utilsGraphQL.ts";
import { GetCompleteProduct } from "../../utils/clientGraphql/queries.ts";
import { toProductGraphQL } from "../../utils/transform.ts";
import { URL_KEY } from "../../utils/constants.ts";
import stringifySearchCriteria from "../../utils/stringifySearchCriteria.ts";

export interface Props {
slug: RequestURLParam;
Expand Down Expand Up @@ -49,74 +47,45 @@ async function loader(
site,
minInstallmentValue,
maxInstallments,
currencyCode,
storeId,
clientAdmin,
} = ctx;
const STALE = enableCache ? DecoStale : undefined;
const customAttributes = getCustomFields(customFields, ctx.customAttributes);
const defaultPath = useSuffix ? formatUrlSuffix(site) : undefined;

const getProductSku = async () => {
const searchCriteria = {
filterGroups: [
{
filters: [{ field: URL_KEY, value: slug }],
},
],
};

const queryParams = {
site,
currencyCode,
storeId,
...stringifySearchCriteria(searchCriteria),
};

const itemSku = await clientAdmin["GET /rest/:site/V1/products"](
{
...queryParams,
const { products } = await clientGraphql.query<
ProductDetailsGraphQL,
ProductDetailsInputs
>(
{
variables: {
filter: { url_key: { eq: slug } },
},
STALE,
).then((res) => res.json());
if (!itemSku.items.length) return null;
return itemSku.items[0].sku;
};

const getFullProduct = async (sku: string) =>
await clientGraphql.query<ProductDetailsGraphQL, ProductDetailsInputs>(
{
variables: {
filter: { sku: { eq: sku } },
},
...GetCompleteProduct(customAttributes, isBreadcrumbProductName),
},
STALE,
);
...GetCompleteProduct(customAttributes, isBreadcrumbProductName),
},
STALE,
);

const sku = await getProductSku();
const product = products.items.find((p) => p.url_key === slug);

if (!sku) {
console.log("product not found");
if (!product) {
return null;
}
const { products } = await getFullProduct(sku);

const productCanonicalUrl = new URL(
(defaultPath ?? "") + products.items[0]?.canonical_url ?? "",
(defaultPath ?? "") + product?.canonical_url ?? "",
url.origin,
);

const productListElement = {
"@type": "ListItem",
item: productCanonicalUrl.href,
position: 1,
name: products.items[0]?.name ?? "",
name: product?.name ?? "",
} as ListItem;

const itemListElement: ListItem[] = isBreadcrumbProductName
? [productListElement]
: products.items[0].categories?.map(
: product.categories?.map(
({ position, url_path, url_key, name }) => ({
"@type": "ListItem",
item:
Expand All @@ -133,7 +102,7 @@ async function loader(
itemListElement,
numberOfItems: itemListElement.length,
},
product: toProductGraphQL(products.items[0], {
product: toProductGraphQL(product, {
originURL: url,
imagesQtd: 999,
customAttributes,
Expand All @@ -142,9 +111,9 @@ async function loader(
defaultPath,
}),
seo: {
title: products.items[0].meta_title?.trim() ??
products.items[0].name.trim() ?? "",
description: products.items[0].meta_description?.trim() ?? "",
title: product.meta_title?.trim() ??
product.name.trim() ?? "",
description: product.meta_description?.trim() ?? "",
canonical: productCanonicalUrl.href,
},
};
Expand Down
2 changes: 1 addition & 1 deletion magento/utils/clientGraphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export interface ProductSearchInputs {

export interface ProductDetailsInputs {
filter: {
sku: {
url_key: {
eq: string;
};
};
Expand Down

0 comments on commit 84bbfa2

Please sign in to comment.