--- import Layout from "../../layouts/Layout.astro"; import AddToCartButton from "../../components/AddToCartButton.astro"; import SubscriptionOptions from "../../components/SubscriptionOptions.astro"; import SponsorshipOptions from "../../components/SponsorshipOptions.astro"; import GiftVoucherOptions from "../../components/GiftVoucherOptions.astro"; import { getProductByHandle, getDefaultRegion, listHoneyVarieties, getShopSettings, getGiftShippingSurcharge } from "../../lib/medusa"; import { formatEUR, formatEURKurz, formatGrundpreis } from "../../lib/format"; import { findVariety, COLLECTION_LABELS } from "../../lib/varieties"; import { PRIMARY_DOMAIN } from "../../lib/sections"; export const prerender = false; const { handle } = Astro.params; const region = await getDefaultRegion(); const product = handle ? await getProductByHandle(handle, region.id) : null; if (!product) { return Astro.redirect("/shop"); } const infoByVariant = Object.fromEntries( product.variants.map((variant) => [ variant.id, { price: variant.calculated_price ? formatEUR(variant.calculated_price.calculated_amount) : "", grundpreis: variant.calculated_price && variant.metadata?.net_weight_grams ? formatGrundpreis(variant.calculated_price.calculated_amount, variant.metadata.net_weight_grams) : null, // Only set in the Medusa admin per variant (Produkt → Variante → // Bild) — most variants don't have one yet, so the gallery simply // doesn't switch for those until someone assigns one. image: variant.thumbnail ?? null, }, ]) ); const initial = infoByVariant[product.variants[0]?.id] ?? { price: "", grundpreis: null }; const deliveryTime = product.metadata?.delivery_time; const categoryName = product.categories?.[0]?.name; const gallery = (product.images?.length ? product.images.map((i) => i.url) : product.thumbnail ? [product.thumbnail] : []); // Beim Abo-Produkt kommt die Sortenauswahl dazu. Sie ist keine Variante, // sondern reist als Metadatum am Warenkorb-Posten mit. const isSubscription = Boolean(product.metadata?.subscription); // Die Patenschaft braucht Angaben, die keine Bestellung mitbringt: einen // Namen für Urkunde und Etikett, beim Geschenk die Adresse des Beschenkten. const isSponsorship = Boolean(product.metadata?.sponsorship); // Der Geschenkgutschein kommt per E-Mail: kein Versand, keine Lieferzeit, // dafür wahlweise die Adresse des Beschenkten. const isGiftVoucher = Boolean(product.metadata?.gift_voucher); const honeyVarieties = isSubscription ? await listHoneyVarieties(region.id) : []; // Beim Geschenk-Abo: ob jedes Paket seinen Versand trägt. Steht als // Metadatum am Abo-Produkt und ist im Admin änderbar. const giftShippingPerDelivery = product.metadata?.gift_shipping === "per_delivery"; const giftSurcharge = isSubscription && giftShippingPerDelivery ? await getGiftShippingSurcharge(region.id) : null; const variety = findVariety(product.title); // Aus den Shop-Einstellungen, damit hier nichts steht, was an der Kasse // nicht gilt. const { freeShippingThreshold } = await getShopSettings(); // Strukturierte Daten für Suchmaschinen: Damit in der Trefferliste Preis und // Verfügbarkeit stehen können und nicht nur ein blauer Link. Jede Variante // wird als eigenes Angebot ausgewiesen, weil sich die Preise unterscheiden. // Die Brotkrumen darunter zeigen, wo die Seite im Shop hängt. const angebote = product.variants .filter((v) => v.calculated_price) .map((v) => ({ "@type": "Offer", name: v.title, price: (v.calculated_price!.calculated_amount).toFixed(2), priceCurrency: "EUR", availability: "https://schema.org/InStock", url: `${PRIMARY_DOMAIN}/shop/${product.handle}`, itemCondition: "https://schema.org/NewCondition", })); const produktDaten = { "@context": "https://schema.org", "@type": "Product", name: product.title, description: product.description ?? product.title, image: gallery.map((bild) => (bild.startsWith("http") ? bild : `${PRIMARY_DOMAIN}${bild}`)), brand: { "@type": "Brand", name: "PLACE4BEES" }, ...(angebote.length === 1 ? { offers: angebote[0] } : {}), ...(angebote.length > 1 ? { offers: { "@type": "AggregateOffer", priceCurrency: "EUR", lowPrice: Math.min(...angebote.map((a) => Number(a.price))).toFixed(2), highPrice: Math.max(...angebote.map((a) => Number(a.price))).toFixed(2), offerCount: angebote.length, offers: angebote, }, } : {}), }; const brotkrumenDaten = { "@context": "https://schema.org", "@type": "BreadcrumbList", itemListElement: [ { "@type": "ListItem", position: 1, name: "Shop", item: `${PRIMARY_DOMAIN}/shop` }, ...(categoryName ? [{ "@type": "ListItem", position: 2, name: categoryName }] : []), { "@type": "ListItem", position: categoryName ? 3 : 2, name: product.title, item: `${PRIMARY_DOMAIN}/shop/${product.handle}`, }, ], }; ---