Skip to content

Commit

Permalink
Fix regex for capturing category and product IDs
Browse files Browse the repository at this point in the history
The issue was that for product categories with the second or third word
starting with the letter "c," the regex used would capture the wrong
part of the string, leading to incorrect ID extraction.

For example, for the category "Wireless charger," the slug
"wireless-charger-c12345678" would result in "harger-c12345678" as the
ID instead of "12345678." The same issue occurred for products.

This commit fixes the regex to correctly capture the IDs.
  • Loading branch information
bolah2009 committed Jun 11, 2024
1 parent b080326 commit 06ed034
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/ecwid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ export async function getCollections(): Promise<Collection[]> {
}

export async function getCollection(handle: string): Promise<Collection | undefined> {
let categoryId = handle.replace(/^.*?\-c/g, '');
let categoryId = handle.replace(/.*(?<=-c)/, '');

const res = await ecwidFetch<EcwidNode>({
method: 'GET',
Expand All @@ -751,7 +751,7 @@ export async function getCollectionProducts({
baseUrl: '/'
};

let categoryId = collection.replace(/^.*?\-c/g, '');
let categoryId = collection.replace(/.*(?<=-c)/, '');

if (collection != 'hidden-homepage-carousel' && collection != 'hidden-homepage-featured-items') {
query.categories = `${categoryId}`;
Expand Down Expand Up @@ -805,7 +805,7 @@ export async function getProducts({
}

export async function getProduct(handle: string): Promise<Product | undefined> {
let productId = handle.replace(/^.*?\-p/g, '');
let productId = handle.replace(/.*(?<=-p)/, '');

const res = await ecwidFetch<EcwidNode>({
method: 'GET',
Expand Down

0 comments on commit 06ed034

Please sign in to comment.