> | null = null;
function storeApiUrl(route: string): string | undefined {
- if (!BACKEND_ORIGIN) return undefined;
+ if (!BACKEND_BASE_URL) return undefined;
const [path, query = ""] = route.split("?", 2);
- const url = new URL(`${BACKEND_ORIGIN}/index.php`);
+ const url = new URL("index.php", BACKEND_BASE_URL);
url.searchParams.set("rest_route", `/wc/store/v1/${path.replace(/^\/+/, "")}`);
new URLSearchParams(query).forEach((value, key) => url.searchParams.append(key, value));
return url.toString();
diff --git a/apps/storefront/src/main.tsx b/apps/storefront/src/main.tsx
index 57eefb8..1823d72 100644
--- a/apps/storefront/src/main.tsx
+++ b/apps/storefront/src/main.tsx
@@ -4,6 +4,7 @@ import { activatePrerenderImages } from "./lib/prerenderImages";
import { captureInitialCmsPageMarkup } from "./lib/prerenderSnapshot";
import { installStaticDocumentWarmup } from "./lib/storefrontDocumentWarmup";
import { startRecentOrdersNotifier } from "./lib/recentOrders";
+import { backendUrl } from "@funky/sdk";
activateDeferredThemeStyles(document);
@@ -132,12 +133,12 @@ const initialFontsReady = document.fonts.status === "loaded"
});
const initialVisualsReady = Promise.all([domReady, initialFontsReady]);
if (prerenderedChrome?.dataset.recentOrdersEnabled === "true") {
- const endpoint = new URL(
- "/wp-json/funkycommerce/v1/recent-orders",
- import.meta.env.VITE_GRAPHQL_ENDPOINT?.trim() || location.origin,
- ).toString();
- window.setTimeout(() => {
- void startRecentOrdersNotifier({
+ const endpoint = backendUrl("wp-json/funkycommerce/v1/recent-orders");
+ if (!endpoint) {
+ console.warn("Recent-order notifications are enabled but no backend is configured.");
+ } else {
+ window.setTimeout(() => {
+ void startRecentOrdersNotifier({
enabled: true,
itemCount: Number(prerenderedChrome.dataset.recentOrdersCount),
intervalSeconds: Number(prerenderedChrome.dataset.recentOrdersInterval),
@@ -148,7 +149,8 @@ if (prerenderedChrome?.dataset.recentOrdersEnabled === "true") {
.catch((error) => {
console.error("Recent-order notifications could not start.", error);
});
- }, deferFlagshipHomeHydration ? 10_000 : 2_000);
+ }, deferFlagshipHomeHydration ? 10_000 : 2_000);
+ }
}
const currentDocumentKey = `${location.pathname}${location.search}${location.hash}`;
let savedReloadScrollY = 0;
diff --git a/packages/sdk/src/environment.ts b/packages/sdk/src/environment.ts
index 3dff882..8f9dd62 100644
--- a/packages/sdk/src/environment.ts
+++ b/packages/sdk/src/environment.ts
@@ -57,12 +57,54 @@ export const BACKEND_ORIGIN: string | undefined = GRAPHQL_ENDPOINT
? new URL(GRAPHQL_ENDPOINT, typeof window === "undefined" ? "http://localhost" : window.location.origin).origin
: undefined;
+/** The WordPress installation base URL, preserving a subdirectory such as
+ * `https://example.test/aqua-bar-wp/` from a GraphQL endpoint below it. */
+export const BACKEND_BASE_URL: string | undefined = GRAPHQL_ENDPOINT
+ ? (() => {
+ const endpoint = new URL(
+ GRAPHQL_ENDPOINT,
+ typeof window === "undefined" ? "http://localhost" : window.location.origin,
+ );
+ endpoint.pathname = endpoint.pathname.replace(/\/+$/, "");
+ return new URL(".", endpoint).toString();
+ })()
+ : undefined;
+export const BACKEND_BASE_PATH = BACKEND_BASE_URL
+ ? new URL(BACKEND_BASE_URL).pathname.replace(/\/+$/, "")
+ : "";
+
/** Builds an absolute REST URL on the backend origin from a `wp-json`-relative path,
* e.g. `restUrl("custom/v1/abandoned-cart")` →
* `https://v1.superfunky.pro/wp-json/custom/v1/abandoned-cart`. Returns `undefined`
* when no backend is configured. */
export function restUrl(path: string): string | undefined {
- if (!BACKEND_ORIGIN) return undefined;
+ if (!BACKEND_BASE_URL) return undefined;
const cleanPath = path.replace(/^\/+/, "");
- return `${BACKEND_ORIGIN}/wp-json/${cleanPath}`;
+ return new URL(`wp-json/${cleanPath}`, BACKEND_BASE_URL).toString();
+}
+
+/** Builds an absolute URL relative to the WordPress installation base. */
+export function backendUrl(path: string): string | undefined {
+ if (!BACKEND_BASE_URL) return undefined;
+ return new URL(path.replace(/^\/+/, ""), BACKEND_BASE_URL).toString();
+}
+
+/** Converts a backend permalink into a storefront path by removing the
+ * WordPress installation directory from its pathname. */
+export function backendPathFromUrl(value: string | undefined, fallback: string): string {
+ if (!value) return fallback;
+ try {
+ const url = new URL(value, BACKEND_BASE_URL || "http://localhost");
+ const basePath = BACKEND_BASE_PATH;
+ if (
+ basePath
+ && (url.pathname === basePath || url.pathname.startsWith(`${basePath}/`))
+ ) {
+ const path = url.pathname.slice(basePath.length) || "/";
+ return path.startsWith("/") ? path : `/${path}`;
+ }
+ return url.pathname || fallback;
+ } catch {
+ return fallback;
+ }
}
diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts
index f42039b..d5eadc9 100644
--- a/packages/sdk/src/index.ts
+++ b/packages/sdk/src/index.ts
@@ -4,6 +4,10 @@ export type {
} from "@funky/shared";
export {
BACKEND_ORIGIN,
+ BACKEND_BASE_URL,
+ BACKEND_BASE_PATH,
+ backendPathFromUrl,
+ backendUrl,
GRAPHQL_ENDPOINT,
isBackendConfigured,
restUrl,
diff --git a/packages/ui/src/catalog/ProductCard.tsx b/packages/ui/src/catalog/ProductCard.tsx
index 2297271..708c2d8 100644
--- a/packages/ui/src/catalog/ProductCard.tsx
+++ b/packages/ui/src/catalog/ProductCard.tsx
@@ -518,18 +518,20 @@ export function ProductCard({
) : null}
-
- {convertedRangeLabel && !selectedVariation ? (
- {convertedRangeLabel}
- ) : (
- <>
- {selectedPriceLabel}
- {selectedCompareAtPriceLabel ? (
- {selectedCompareAtPriceLabel}
- ) : null}
- >
- )}
-
+ {hasPrice ? (
+
+ {convertedRangeLabel && !selectedVariation ? (
+ {convertedRangeLabel}
+ ) : (
+ <>
+ {selectedPriceLabel}
+ {selectedCompareAtPriceLabel ? (
+ {selectedCompareAtPriceLabel}
+ ) : null}
+ >
+ )}
+
+ ) : null}
{(isVariation || product.productType === "variable") && product.variationOptions?.length ? (
diff --git a/packages/ui/src/catalog/productCardPrice.test.ts b/packages/ui/src/catalog/productCardPrice.test.ts
index 613ffd3..5a4d552 100644
--- a/packages/ui/src/catalog/productCardPrice.test.ts
+++ b/packages/ui/src/catalog/productCardPrice.test.ts
@@ -3,7 +3,8 @@ import test from "node:test";
import { hasProductCardPrice } from "./productCardPrice.ts";
test("recognizes explicit zero as a valid product price", () => {
- assert.equal(hasProductCardPrice({ priceAmount: 0, priceLabel: "" }), true);
+ assert.equal(hasProductCardPrice({ priceAmount: 0, priceLabel: "" }), false);
+ assert.equal(hasProductCardPrice({ priceAmount: 0, priceLabel: "0,00 zł" }), false);
});
test("recognizes numeric, range, variation, and legacy label prices", () => {
diff --git a/packages/ui/src/catalog/productCardPrice.ts b/packages/ui/src/catalog/productCardPrice.ts
index 9a2f0dd..1ca5614 100644
--- a/packages/ui/src/catalog/productCardPrice.ts
+++ b/packages/ui/src/catalog/productCardPrice.ts
@@ -5,15 +5,18 @@ export type ProductCardPriceInput = {
variationPriceAmounts?: number[];
};
-/** Treat an explicit zero as a real price while rejecting products whose pricing data
- * is entirely absent. Labels remain supported for legacy/mock card data. */
+/** Treat zero as missing pricing data so unpriced products use their learn-more
+ * action instead of showing a misleading free price. Labels remain supported for
+ * legacy/mock card data when no numeric amount is available. */
export function hasProductCardPrice({
priceAmount,
priceLabel,
priceRangeLabel,
variationPriceAmounts = [],
}: ProductCardPriceInput): boolean {
- if (Number.isFinite(priceAmount) && (priceAmount ?? -1) >= 0) return true;
- if (variationPriceAmounts.some((amount) => Number.isFinite(amount) && amount >= 0)) return true;
+ if (Number.isFinite(priceAmount)) return (priceAmount ?? 0) > 0;
+ if (variationPriceAmounts.length) {
+ return variationPriceAmounts.some((amount) => Number.isFinite(amount) && amount > 0);
+ }
return Boolean(priceRangeLabel?.trim() || priceLabel?.trim());
}