medusa-product-detail-logic
v1.0.2
Published
Ported Product Detail logic from B&B_ui project.
Downloads
44
Readme
medusa-product-detail-logic
Reusable product detail hook logic for Medusa storefronts.
This package exposes useProduct and related service helpers used by the product detail page flow:
- variant selection
- price formatting
- stock/availability
- add-to-cart readiness and status
- media/gallery state
- wishlist (local or Medusa API)
- reviews (callback or Medusa rating endpoint)
- related products
Installation
npm installFor local development in this monorepo:
npm run buildExports
From src/index.ts:
useProducthook- product formatter/service helpers from
medusa-services:product-detailwishlistreviewsrelated-products
Quick Usage
import { useProduct } from "medusa-product-detail-logic"
const pdp = useProduct({
initialProduct,
onAddToCart: async (variantId, quantity) => {
// call cart API
},
syncSelectionWithUrl: true,
wishlistMedusa: {
backendUrl: "https://your-medusa-server.com",
publishableApiKey: "pk_...",
},
wishlistCustomerAuthorization: customerJwtOrNull,
reviewsMedusa: {
backendUrl: "https://your-medusa-server.com",
publishableApiKey: "pk_...",
},
relatedProductsMedusa: {
backendUrl: "https://your-medusa-server.com",
publishableApiKey: "pk_...",
regionId: "reg_...",
limit: 8,
},
})useProduct Input (high level)
initialProduct: normalized product data for PDPonAddToCart: async callback for cart addmaxLineQuantity: optional per-line quantity capsyncSelectionWithUrl: read/write selected options in query paramswishlistMedusa+wishlistCustomerAuthorization: enables Medusa/store/wishlistmodeonFetchReviewsSummary: custom reviews summary callback (takes precedence)reviewsMedusa: fallback Medusa rating summary source (GET /store/products/:id/rating)onFetchRelatedProducts: custom related products callback (takes precedence)relatedProductsMedusa: fallback Medusa related products source
Returned API (high level)
- Core:
product,isLoading,error - Variants:
options,selectedOptions,activeVariant,availableOptions,setOption - Pricing:
pricing - Quantity:
quantity,incrementQuantity,decrementQuantity,setQuantity - Media:
mediaUrls,activeMedia,activeMediaIndex,setActiveMedia - Cart:
isReadyToAddToCart,addToCartStatus,handleAddToCart - Inventory:
isAvailable,inventoryQuantity,stockStatusMessage - Wishlist group:
wishlist.{ ids, isWishlisted, toggleWishlist, status, error, refresh } - Reviews group:
reviews.{ averageRating, totalReviews, status, error, refresh, fetch } - Related group:
relatedProducts.{ items, status, error, refresh, fetch }
Required Product Shape
initialProduct should include:
id,title,descriptionoptions[]variants[]with option mapping, price, stock/backordermediaUrls[]
For related-products Medusa fallback, include:
collectionId(optional but recommended)tagIds(optional but recommended)
Use formatProductData export from medusa-services/product-detail to normalize Medusa Store product responses.
Notes
- Wishlist/reviews/related can run in callback mode or Medusa mode.
- If Medusa config/auth is missing, the hook falls back safely where applicable.
- This package is intended for UI-agnostic logic; render layer stays in your app.
