@brainerce/mcp-server 2.0.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/http.js +891 -14
- package/dist/bin/stdio.js +891 -14
- package/dist/index.js +891 -14
- package/dist/index.mjs +891 -14
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -702,27 +702,103 @@ const nudges: CartNudge[] = await client.getCartNudges(cartId);
|
|
|
702
702
|
Display: banners in header, badges on product cards (strikethrough + discounted price), nudges below cart totals, applied discounts as line items in order summary.`;
|
|
703
703
|
}
|
|
704
704
|
function getRecommendationsSection() {
|
|
705
|
-
return `## Product Recommendations
|
|
705
|
+
return `## Product Recommendations & Upsell Features
|
|
706
706
|
|
|
707
|
-
|
|
707
|
+
### Relation Types
|
|
708
|
+
- **Cross-sell**: Complementary products ("Bought a phone? Add a case") \u2014 shown on product page + cart
|
|
709
|
+
- **Upsell**: Premium alternatives ("Upgrade to Pro for $20 more") \u2014 shown on product page + cart upgrade banner
|
|
710
|
+
- **Related**: Similar products ("Other shirts in this collection") \u2014 shown on product page
|
|
711
|
+
|
|
712
|
+
### Product Page Recommendations
|
|
713
|
+
Recommendations come **embedded in the product response** \u2014 no extra API call needed.
|
|
708
714
|
|
|
709
715
|
\`\`\`typescript
|
|
710
|
-
import type { ProductRecommendation, ProductRecommendationsResponse
|
|
716
|
+
import type { ProductRecommendation, ProductRecommendationsResponse } from 'brainerce';
|
|
711
717
|
|
|
712
|
-
// Product page \u2014 recommendations are embedded in the product response
|
|
713
718
|
const product = await client.getProductBySlug('some-slug');
|
|
714
719
|
const recs = (product as any).recommendations as ProductRecommendationsResponse | undefined;
|
|
715
|
-
// recs?.upsells \u2014 premium alternatives (show on product page)
|
|
716
|
-
// recs?.related \u2014 similar products (show at bottom of product page)
|
|
717
|
-
// recs?.crossSells \u2014 complementary products (typically used on cart page)
|
|
718
720
|
|
|
719
|
-
//
|
|
720
|
-
|
|
721
|
+
// recs?.crossSells \u2014 complementary products \u2192 "Frequently Bought Together" section
|
|
722
|
+
// recs?.upsells \u2014 premium alternatives \u2192 "Upgrade Your Choice" section
|
|
723
|
+
// recs?.related \u2014 similar products \u2192 "Similar Products" section
|
|
724
|
+
\`\`\`
|
|
725
|
+
|
|
726
|
+
### Cart Page Features
|
|
727
|
+
|
|
728
|
+
**Cross-sell recommendations** (existing products the customer might also want):
|
|
729
|
+
\`\`\`typescript
|
|
730
|
+
import type { CartRecommendationsResponse } from 'brainerce';
|
|
731
|
+
const cartRecs = await client.getCartRecommendations(cartId, 4);
|
|
721
732
|
// cartRecs.recommendations \u2014 deduplicated cross-sells (excludes items already in cart)
|
|
722
733
|
\`\`\`
|
|
723
734
|
|
|
724
|
-
|
|
725
|
-
|
|
735
|
+
**Cart upgrade banner** (upsell relations with small price delta \u2014 "Upgrade to X for +$Y"):
|
|
736
|
+
\`\`\`typescript
|
|
737
|
+
import type { CartUpgradesResponse } from 'brainerce';
|
|
738
|
+
const { upgrades } = await client.getCartUpgrades(cartId);
|
|
739
|
+
// upgrades is a Record<sourceProductId, { targetProduct, priceDelta, deltaPercent }>
|
|
740
|
+
// Show inline banner per cart item if upgrade exists
|
|
741
|
+
\`\`\`
|
|
742
|
+
|
|
743
|
+
**Bundle offers** (discounted complementary products configured by the store owner):
|
|
744
|
+
\`\`\`typescript
|
|
745
|
+
import type { CartBundlesResponse } from 'brainerce';
|
|
746
|
+
const { bundles } = await client.getCartBundles(cartId);
|
|
747
|
+
// bundles[].bundleProduct \u2014 the product to offer
|
|
748
|
+
// bundles[].originalPrice / bundles[].discountedPrice \u2014 prices
|
|
749
|
+
// bundles[].discountType ('PERCENTAGE' | 'FIXED_AMOUNT') + discountValue
|
|
750
|
+
\`\`\`
|
|
751
|
+
|
|
752
|
+
**Free shipping progress bar** (configured threshold in channel settings):
|
|
753
|
+
\`\`\`typescript
|
|
754
|
+
const { storeInfo } = useStoreInfo(); // from store provider
|
|
755
|
+
const threshold = storeInfo?.upsell?.freeShippingThreshold;
|
|
756
|
+
const subtotal = parseFloat(cart.subtotal);
|
|
757
|
+
const remaining = threshold ? threshold - subtotal : 0;
|
|
758
|
+
const qualified = threshold ? subtotal >= threshold : false;
|
|
759
|
+
// Show progress bar: remaining > 0 ? "You're $X away from free shipping!" : "You've got free shipping!"
|
|
760
|
+
\`\`\`
|
|
761
|
+
|
|
762
|
+
### Checkout Features
|
|
763
|
+
|
|
764
|
+
**Order bumps** (one-click add-ons at checkout):
|
|
765
|
+
\`\`\`typescript
|
|
766
|
+
import type { CheckoutBumpsResponse } from 'brainerce';
|
|
767
|
+
const { bumps } = await client.getCheckoutBumps(checkoutId);
|
|
768
|
+
// bumps[].bumpProduct \u2014 product to offer
|
|
769
|
+
// bumps[].originalPrice / discountedPrice \u2014 with optional discount
|
|
770
|
+
// bumps[].title / description \u2014 display text
|
|
771
|
+
|
|
772
|
+
// Add a bump to cart:
|
|
773
|
+
await client.addOrderBump(cartId, bumpId);
|
|
774
|
+
// Remove a bump:
|
|
775
|
+
await client.removeOrderBump(cartId, bumpId);
|
|
776
|
+
// Detect already-added bumps: check cart.items for metadata?.isOrderBump === true
|
|
777
|
+
\`\`\`
|
|
778
|
+
|
|
779
|
+
### Feature Flags (per-channel)
|
|
780
|
+
All upsell features are controlled by \`storeInfo.upsell.*\`:
|
|
781
|
+
- \`freeShippingBarEnabled\` \u2014 free shipping progress bar in cart
|
|
782
|
+
- \`frequentlyBoughtTogetherEnabled\` \u2014 cross-sell section on product page
|
|
783
|
+
- \`cartUpgradeBannerEnabled\` \u2014 upgrade banner per cart item
|
|
784
|
+
- \`cartBundleEnabled\` \u2014 bundle offers in cart
|
|
785
|
+
- \`checkoutOrderBumpEnabled\` \u2014 order bumps at checkout
|
|
786
|
+
- \`freeShippingThreshold\` \u2014 threshold amount for free shipping bar
|
|
787
|
+
|
|
788
|
+
Always check the flag before rendering: \`if (storeInfo?.upsell?.featureName !== false)\`
|
|
789
|
+
|
|
790
|
+
### Components Reference
|
|
791
|
+
| Component | Location | Purpose |
|
|
792
|
+
|-----------|----------|---------|
|
|
793
|
+
| FrequentlyBoughtTogether | products/ | Cross-sells with "Add all to cart" on product page |
|
|
794
|
+
| RecommendationSection | products/ | Grid of upsells or related products on product page |
|
|
795
|
+
| CartRecommendationSection | products/ | Cross-sell grid at bottom of cart page |
|
|
796
|
+
| FreeShippingBar | cart/ | Progress bar toward free shipping threshold |
|
|
797
|
+
| CartUpgradeBanner | cart/ | Inline "Upgrade to X for +$Y" per cart item |
|
|
798
|
+
| CartBundleOfferCard | cart/ | Discounted bundle offer card in cart |
|
|
799
|
+
| OrderBumpCard | checkout/ | Checkbox add-on card in checkout sidebar |
|
|
800
|
+
|
|
801
|
+
ProductRecommendation: \`id\`, \`name\`, \`slug\`, \`basePrice\`, \`salePrice\`, \`images\`, \`type\`, \`inventory\`, \`relationType\`.`;
|
|
726
802
|
}
|
|
727
803
|
function getInventorySection() {
|
|
728
804
|
return `## Inventory, Stock Display & Reservation Countdown
|
|
@@ -2895,13 +2971,17 @@ export default async function ProductDetailPage({ params }: Props) {
|
|
|
2895
2971
|
|
|
2896
2972
|
import { useEffect, useState } from 'react';
|
|
2897
2973
|
import Link from 'next/link';
|
|
2898
|
-
import type { CartRecommendationsResponse } from 'brainerce';
|
|
2974
|
+
import type { CartRecommendationsResponse, CartUpgradesResponse, CartBundlesResponse } from 'brainerce';
|
|
2899
2975
|
import { getClient } from '@/lib/brainerce';
|
|
2900
2976
|
import { useCart } from '@/providers/store-provider';
|
|
2977
|
+
import { useStoreInfo } from '@/providers/store-provider';
|
|
2901
2978
|
import { CartItem } from '@/components/cart/cart-item';
|
|
2979
|
+
import { CartUpgradeBanner } from '@/components/cart/cart-upgrade-banner';
|
|
2980
|
+
import { CartBundleOfferCard } from '@/components/cart/cart-bundle-offer';
|
|
2902
2981
|
import { CartSummary } from '@/components/cart/cart-summary';
|
|
2903
2982
|
import { CouponInput } from '@/components/cart/coupon-input';
|
|
2904
2983
|
import { CartNudges } from '@/components/cart/cart-nudges';
|
|
2984
|
+
import { FreeShippingBar } from '@/components/cart/free-shipping-bar';
|
|
2905
2985
|
import { ReservationCountdown } from '@/components/cart/reservation-countdown';
|
|
2906
2986
|
import { CartRecommendationSection } from '@/components/products/recommendation-section';
|
|
2907
2987
|
import { LoadingSpinner } from '@/components/shared/loading-spinner';
|
|
@@ -2909,9 +2989,12 @@ import { useTranslations } from '@/lib/translations';
|
|
|
2909
2989
|
|
|
2910
2990
|
export default function CartPage() {
|
|
2911
2991
|
const { cart, cartLoading, refreshCart, itemCount } = useCart();
|
|
2992
|
+
const { storeInfo } = useStoreInfo();
|
|
2912
2993
|
const t = useTranslations('cart');
|
|
2913
2994
|
const tc = useTranslations('common');
|
|
2914
2995
|
const [cartRecs, setCartRecs] = useState<CartRecommendationsResponse | null>(null);
|
|
2996
|
+
const [upgrades, setUpgrades] = useState<CartUpgradesResponse | null>(null);
|
|
2997
|
+
const [bundles, setBundles] = useState<CartBundlesResponse | null>(null);
|
|
2915
2998
|
|
|
2916
2999
|
// Load cross-sell recommendations when cart changes
|
|
2917
3000
|
useEffect(() => {
|
|
@@ -2926,6 +3009,32 @@ export default function CartPage() {
|
|
|
2926
3009
|
.catch(() => {});
|
|
2927
3010
|
}, [cart?.id, cart?.items.length]);
|
|
2928
3011
|
|
|
3012
|
+
// Load upgrade suggestions when cart changes
|
|
3013
|
+
useEffect(() => {
|
|
3014
|
+
if (!cart?.id || cart.items.length === 0 || storeInfo?.upsell?.cartUpgradeBannerEnabled === false) {
|
|
3015
|
+
setUpgrades(null);
|
|
3016
|
+
return;
|
|
3017
|
+
}
|
|
3018
|
+
const client = getClient();
|
|
3019
|
+
client
|
|
3020
|
+
.getCartUpgrades(cart.id)
|
|
3021
|
+
.then(setUpgrades)
|
|
3022
|
+
.catch(() => {});
|
|
3023
|
+
}, [cart?.id, cart?.items.length, storeInfo?.upsell?.cartUpgradeBannerEnabled]);
|
|
3024
|
+
|
|
3025
|
+
// Load bundle offers when cart changes
|
|
3026
|
+
useEffect(() => {
|
|
3027
|
+
if (!cart?.id || cart.items.length === 0 || storeInfo?.upsell?.cartBundleEnabled === false) {
|
|
3028
|
+
setBundles(null);
|
|
3029
|
+
return;
|
|
3030
|
+
}
|
|
3031
|
+
const client = getClient();
|
|
3032
|
+
client
|
|
3033
|
+
.getCartBundles(cart.id)
|
|
3034
|
+
.then(setBundles)
|
|
3035
|
+
.catch(() => {});
|
|
3036
|
+
}, [cart?.id, cart?.items.length, storeInfo?.upsell?.cartBundleEnabled]);
|
|
3037
|
+
|
|
2929
3038
|
if (cartLoading) {
|
|
2930
3039
|
return (
|
|
2931
3040
|
<div className="flex min-h-[60vh] items-center justify-center">
|
|
@@ -2985,10 +3094,30 @@ export default function CartPage() {
|
|
|
2985
3094
|
{/* Cart items */}
|
|
2986
3095
|
<div>
|
|
2987
3096
|
{cart.items.map((item) => (
|
|
2988
|
-
<
|
|
3097
|
+
<div key={item.id}>
|
|
3098
|
+
<CartItem item={item} onUpdate={refreshCart} />
|
|
3099
|
+
{upgrades?.upgrades?.[item.productId] && (
|
|
3100
|
+
<CartUpgradeBanner
|
|
3101
|
+
suggestion={upgrades.upgrades[item.productId]}
|
|
3102
|
+
cartItem={item}
|
|
3103
|
+
onUpgrade={refreshCart}
|
|
3104
|
+
className="mb-2 ms-24"
|
|
3105
|
+
/>
|
|
3106
|
+
)}
|
|
3107
|
+
</div>
|
|
2989
3108
|
))}
|
|
2990
3109
|
</div>
|
|
2991
3110
|
|
|
3111
|
+
{/* Bundle offers */}
|
|
3112
|
+
{bundles?.bundles && bundles.bundles.length > 0 && (
|
|
3113
|
+
<div className="mt-6 space-y-3">
|
|
3114
|
+
<h3 className="text-foreground text-sm font-semibold">{t('bundleOffers')}</h3>
|
|
3115
|
+
{bundles.bundles.map((offer) => (
|
|
3116
|
+
<CartBundleOfferCard key={offer.id} offer={offer} onAdd={refreshCart} />
|
|
3117
|
+
))}
|
|
3118
|
+
</div>
|
|
3119
|
+
)}
|
|
3120
|
+
|
|
2992
3121
|
{/* Coupon input */}
|
|
2993
3122
|
<div className="border-border mt-6 border-t pt-4">
|
|
2994
3123
|
<CouponInput cart={cart} onUpdate={refreshCart} />
|
|
@@ -2998,6 +3127,7 @@ export default function CartPage() {
|
|
|
2998
3127
|
{/* Summary sidebar */}
|
|
2999
3128
|
<div className="lg:col-span-1">
|
|
3000
3129
|
<div className="bg-muted/50 border-border sticky top-24 rounded-lg border p-6">
|
|
3130
|
+
<FreeShippingBar className="mb-4" />
|
|
3001
3131
|
<CartSummary />
|
|
3002
3132
|
|
|
3003
3133
|
<Link
|
|
@@ -3041,6 +3171,7 @@ import type {
|
|
|
3041
3171
|
SetShippingAddressDto,
|
|
3042
3172
|
ShippingDestinations,
|
|
3043
3173
|
PickupLocation,
|
|
3174
|
+
CheckoutBumpsResponse,
|
|
3044
3175
|
} from 'brainerce';
|
|
3045
3176
|
import { formatPrice } from 'brainerce';
|
|
3046
3177
|
import { getClient } from '@/lib/brainerce';
|
|
@@ -3051,6 +3182,7 @@ import { PaymentStep } from '@/components/checkout/payment-step';
|
|
|
3051
3182
|
import { DeliveryMethodStep } from '@/components/checkout/delivery-method-step';
|
|
3052
3183
|
import { PickupStep } from '@/components/checkout/pickup-step';
|
|
3053
3184
|
import { TaxDisplay } from '@/components/checkout/tax-display';
|
|
3185
|
+
import { OrderBumpCard } from '@/components/checkout/order-bump-card';
|
|
3054
3186
|
import { CouponInput } from '@/components/cart/coupon-input';
|
|
3055
3187
|
import { ReservationCountdown } from '@/components/cart/reservation-countdown';
|
|
3056
3188
|
import { LoadingSpinner } from '@/components/shared/loading-spinner';
|
|
@@ -3087,6 +3219,9 @@ function CheckoutContent() {
|
|
|
3087
3219
|
phone?: string;
|
|
3088
3220
|
} | null>(null);
|
|
3089
3221
|
const [hasSavedAddress, setHasSavedAddress] = useState(false);
|
|
3222
|
+
const [orderBumps, setOrderBumps] = useState<CheckoutBumpsResponse | null>(null);
|
|
3223
|
+
const [addedBumpIds, setAddedBumpIds] = useState<Set<string>>(new Set());
|
|
3224
|
+
const [bumpLoading, setBumpLoading] = useState<string | null>(null);
|
|
3090
3225
|
|
|
3091
3226
|
// Check for returning from canceled payment
|
|
3092
3227
|
const canceled = searchParams.get('canceled') === 'true';
|
|
@@ -3189,9 +3324,59 @@ function CheckoutContent() {
|
|
|
3189
3324
|
};
|
|
3190
3325
|
|
|
3191
3326
|
initCheckout();
|
|
3192
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
3193
3327
|
}, [cart?.id, existingCheckoutId]);
|
|
3194
3328
|
|
|
3329
|
+
// Load order bumps when checkout is available
|
|
3330
|
+
useEffect(() => {
|
|
3331
|
+
if (!checkout?.id || storeInfo?.upsell?.checkoutOrderBumpEnabled === false) {
|
|
3332
|
+
setOrderBumps(null);
|
|
3333
|
+
return;
|
|
3334
|
+
}
|
|
3335
|
+
const client = getClient();
|
|
3336
|
+
client
|
|
3337
|
+
.getCheckoutBumps(checkout.id)
|
|
3338
|
+
.then((data) => {
|
|
3339
|
+
setOrderBumps(data);
|
|
3340
|
+
// Detect already-added bumps from cart
|
|
3341
|
+
if (cart?.items) {
|
|
3342
|
+
const existingBumpIds = new Set<string>();
|
|
3343
|
+
for (const item of cart.items) {
|
|
3344
|
+
const meta = item.metadata as Record<string, unknown> | undefined;
|
|
3345
|
+
if (meta?.isOrderBump && meta?.orderBumpId) {
|
|
3346
|
+
existingBumpIds.add(meta.orderBumpId as string);
|
|
3347
|
+
}
|
|
3348
|
+
}
|
|
3349
|
+
setAddedBumpIds(existingBumpIds);
|
|
3350
|
+
}
|
|
3351
|
+
})
|
|
3352
|
+
.catch(() => {});
|
|
3353
|
+
}, [checkout?.id, storeInfo?.upsell?.checkoutOrderBumpEnabled]);
|
|
3354
|
+
|
|
3355
|
+
// Handle bump toggle
|
|
3356
|
+
async function handleBumpToggle(bumpId: string, add: boolean) {
|
|
3357
|
+
if (!cart?.id || bumpLoading) return;
|
|
3358
|
+
try {
|
|
3359
|
+
setBumpLoading(bumpId);
|
|
3360
|
+
const client = getClient();
|
|
3361
|
+
if (add) {
|
|
3362
|
+
await client.addOrderBump(cart.id, bumpId);
|
|
3363
|
+
setAddedBumpIds((prev) => new Set([...prev, bumpId]));
|
|
3364
|
+
} else {
|
|
3365
|
+
await client.removeOrderBump(cart.id, bumpId);
|
|
3366
|
+
setAddedBumpIds((prev) => {
|
|
3367
|
+
const next = new Set(prev);
|
|
3368
|
+
next.delete(bumpId);
|
|
3369
|
+
return next;
|
|
3370
|
+
});
|
|
3371
|
+
}
|
|
3372
|
+
await refreshCart();
|
|
3373
|
+
} catch (err) {
|
|
3374
|
+
console.error('Failed to toggle order bump:', err);
|
|
3375
|
+
} finally {
|
|
3376
|
+
setBumpLoading(null);
|
|
3377
|
+
}
|
|
3378
|
+
}
|
|
3379
|
+
|
|
3195
3380
|
// Handle shipping address submission
|
|
3196
3381
|
async function handleAddressSubmit(
|
|
3197
3382
|
address: SetShippingAddressDto,
|
|
@@ -3694,6 +3879,24 @@ function CheckoutContent() {
|
|
|
3694
3879
|
)
|
|
3695
3880
|
)}
|
|
3696
3881
|
|
|
3882
|
+
{/* Order bumps */}
|
|
3883
|
+
{orderBumps?.bumps && orderBumps.bumps.length > 0 && (
|
|
3884
|
+
<div className="border-border space-y-2 border-t pt-4">
|
|
3885
|
+
<p className="text-foreground text-xs font-semibold uppercase tracking-wide">
|
|
3886
|
+
{t('addToYourOrder')}
|
|
3887
|
+
</p>
|
|
3888
|
+
{orderBumps.bumps.map((bump) => (
|
|
3889
|
+
<OrderBumpCard
|
|
3890
|
+
key={bump.id}
|
|
3891
|
+
bump={bump}
|
|
3892
|
+
isAdded={addedBumpIds.has(bump.id)}
|
|
3893
|
+
onToggle={handleBumpToggle}
|
|
3894
|
+
loading={bumpLoading === bump.id}
|
|
3895
|
+
/>
|
|
3896
|
+
))}
|
|
3897
|
+
</div>
|
|
3898
|
+
)}
|
|
3899
|
+
|
|
3697
3900
|
{/* Coupon input \u2014 show from shipping/pickup step onwards (or immediately if digital) */}
|
|
3698
3901
|
{cart &&
|
|
3699
3902
|
(isAllDigital || step === 'shipping' || step === 'pickup' || step === 'payment') && (
|
|
@@ -9165,6 +9368,680 @@ export function LoadingSpinner({ size = 'md', className }: LoadingSpinnerProps)
|
|
|
9165
9368
|
</div>
|
|
9166
9369
|
);
|
|
9167
9370
|
}
|
|
9371
|
+
`,
|
|
9372
|
+
"src/components/products/recommendation-section.tsx": `'use client';
|
|
9373
|
+
|
|
9374
|
+
import { useEffect, useState } from 'react';
|
|
9375
|
+
import Link from 'next/link';
|
|
9376
|
+
import Image from 'next/image';
|
|
9377
|
+
import type { ProductRecommendation } from 'brainerce';
|
|
9378
|
+
import { PriceDisplay } from '@/components/shared/price-display';
|
|
9379
|
+
import { cn } from '@/lib/utils';
|
|
9380
|
+
|
|
9381
|
+
interface RecommendationCardProps {
|
|
9382
|
+
item: ProductRecommendation;
|
|
9383
|
+
className?: string;
|
|
9384
|
+
}
|
|
9385
|
+
|
|
9386
|
+
function RecommendationCard({ item, className }: RecommendationCardProps) {
|
|
9387
|
+
const firstImage = item.images?.[0];
|
|
9388
|
+
const imageUrl = typeof firstImage === 'string' ? firstImage : firstImage?.url || null;
|
|
9389
|
+
const slug = item.slug || item.id;
|
|
9390
|
+
const basePrice = parseFloat(item.basePrice);
|
|
9391
|
+
const salePrice = item.salePrice ? parseFloat(item.salePrice) : undefined;
|
|
9392
|
+
const isOnSale = salePrice != null && salePrice < basePrice;
|
|
9393
|
+
|
|
9394
|
+
return (
|
|
9395
|
+
<Link
|
|
9396
|
+
href={\`/products/\${slug}\`}
|
|
9397
|
+
className={cn(
|
|
9398
|
+
'border-border bg-background group block overflow-hidden rounded-lg border transition-shadow hover:shadow-md',
|
|
9399
|
+
className
|
|
9400
|
+
)}
|
|
9401
|
+
>
|
|
9402
|
+
<div className="bg-muted relative aspect-square overflow-hidden">
|
|
9403
|
+
{imageUrl ? (
|
|
9404
|
+
<Image
|
|
9405
|
+
src={imageUrl}
|
|
9406
|
+
alt={item.name}
|
|
9407
|
+
fill
|
|
9408
|
+
sizes="(max-width: 640px) 50vw, (max-width: 1024px) 33vw, 20vw"
|
|
9409
|
+
className="object-cover transition-transform duration-300 group-hover:scale-105"
|
|
9410
|
+
/>
|
|
9411
|
+
) : (
|
|
9412
|
+
<div className="text-muted-foreground absolute inset-0 flex items-center justify-center">
|
|
9413
|
+
<svg className="h-10 w-10" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
|
9414
|
+
<path
|
|
9415
|
+
strokeLinecap="round"
|
|
9416
|
+
strokeLinejoin="round"
|
|
9417
|
+
strokeWidth={1.5}
|
|
9418
|
+
d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"
|
|
9419
|
+
/>
|
|
9420
|
+
</svg>
|
|
9421
|
+
</div>
|
|
9422
|
+
)}
|
|
9423
|
+
</div>
|
|
9424
|
+
<div className="space-y-1.5 p-3">
|
|
9425
|
+
<h3 className="text-foreground group-hover:text-primary line-clamp-2 text-sm font-medium transition-colors">
|
|
9426
|
+
{item.name}
|
|
9427
|
+
</h3>
|
|
9428
|
+
<PriceDisplay price={basePrice} salePrice={isOnSale ? salePrice : undefined} size="sm" />
|
|
9429
|
+
</div>
|
|
9430
|
+
</Link>
|
|
9431
|
+
);
|
|
9432
|
+
}
|
|
9433
|
+
|
|
9434
|
+
interface RecommendationSectionProps {
|
|
9435
|
+
title: string;
|
|
9436
|
+
items: ProductRecommendation[];
|
|
9437
|
+
className?: string;
|
|
9438
|
+
}
|
|
9439
|
+
|
|
9440
|
+
export function RecommendationSection({ title, items, className }: RecommendationSectionProps) {
|
|
9441
|
+
if (items.length === 0) return null;
|
|
9442
|
+
|
|
9443
|
+
return (
|
|
9444
|
+
<div className={cn('', className)}>
|
|
9445
|
+
<h2 className="text-foreground mb-4 text-xl font-semibold">{title}</h2>
|
|
9446
|
+
<div className="grid grid-cols-2 gap-3 sm:grid-cols-3 lg:grid-cols-4">
|
|
9447
|
+
{items.map((item) => (
|
|
9448
|
+
<RecommendationCard key={item.id} item={item} />
|
|
9449
|
+
))}
|
|
9450
|
+
</div>
|
|
9451
|
+
</div>
|
|
9452
|
+
);
|
|
9453
|
+
}
|
|
9454
|
+
|
|
9455
|
+
interface CartRecommendationSectionProps {
|
|
9456
|
+
title: string;
|
|
9457
|
+
items: ProductRecommendation[];
|
|
9458
|
+
className?: string;
|
|
9459
|
+
}
|
|
9460
|
+
|
|
9461
|
+
export function CartRecommendationSection({
|
|
9462
|
+
title,
|
|
9463
|
+
items,
|
|
9464
|
+
className,
|
|
9465
|
+
}: CartRecommendationSectionProps) {
|
|
9466
|
+
if (items.length === 0) return null;
|
|
9467
|
+
|
|
9468
|
+
return (
|
|
9469
|
+
<div className={cn('', className)}>
|
|
9470
|
+
<h2 className="text-foreground mb-4 text-lg font-semibold">{title}</h2>
|
|
9471
|
+
<div className="grid grid-cols-2 gap-3 sm:grid-cols-4">
|
|
9472
|
+
{items.map((item) => (
|
|
9473
|
+
<RecommendationCard key={item.id} item={item} />
|
|
9474
|
+
))}
|
|
9475
|
+
</div>
|
|
9476
|
+
</div>
|
|
9477
|
+
);
|
|
9478
|
+
}
|
|
9479
|
+
`,
|
|
9480
|
+
"src/components/products/frequently-bought-together.tsx": `'use client';
|
|
9481
|
+
|
|
9482
|
+
import { useState } from 'react';
|
|
9483
|
+
import Image from 'next/image';
|
|
9484
|
+
import type { Product, ProductRecommendation } from 'brainerce';
|
|
9485
|
+
import { formatPrice } from 'brainerce';
|
|
9486
|
+
import { useCart } from '@/providers/store-provider';
|
|
9487
|
+
import { useStoreInfo } from '@/providers/store-provider';
|
|
9488
|
+
import { useTranslations } from '@/lib/translations';
|
|
9489
|
+
import { cn } from '@/lib/utils';
|
|
9490
|
+
|
|
9491
|
+
interface FrequentlyBoughtTogetherProps {
|
|
9492
|
+
items: ProductRecommendation[];
|
|
9493
|
+
currentProduct: Product;
|
|
9494
|
+
className?: string;
|
|
9495
|
+
}
|
|
9496
|
+
|
|
9497
|
+
function getEffectivePrice(item: { basePrice: string; salePrice?: string | null }): number {
|
|
9498
|
+
const sale = item.salePrice ? parseFloat(item.salePrice) : null;
|
|
9499
|
+
const base = parseFloat(item.basePrice);
|
|
9500
|
+
return sale != null && sale < base ? sale : base;
|
|
9501
|
+
}
|
|
9502
|
+
|
|
9503
|
+
function ProductThumb({
|
|
9504
|
+
name,
|
|
9505
|
+
imageUrl,
|
|
9506
|
+
price,
|
|
9507
|
+
currency,
|
|
9508
|
+
checked,
|
|
9509
|
+
onToggle,
|
|
9510
|
+
disabled,
|
|
9511
|
+
}: {
|
|
9512
|
+
name: string;
|
|
9513
|
+
imageUrl: string | null;
|
|
9514
|
+
price: number;
|
|
9515
|
+
currency: string;
|
|
9516
|
+
checked: boolean;
|
|
9517
|
+
onToggle?: () => void;
|
|
9518
|
+
disabled?: boolean;
|
|
9519
|
+
}) {
|
|
9520
|
+
return (
|
|
9521
|
+
<label
|
|
9522
|
+
className={cn(
|
|
9523
|
+
'border-border bg-background relative flex cursor-pointer flex-col items-center rounded-lg border p-3 transition-all',
|
|
9524
|
+
checked ? 'ring-primary ring-2' : 'opacity-60',
|
|
9525
|
+
disabled && 'pointer-events-none'
|
|
9526
|
+
)}
|
|
9527
|
+
>
|
|
9528
|
+
{onToggle && (
|
|
9529
|
+
<input
|
|
9530
|
+
type="checkbox"
|
|
9531
|
+
checked={checked}
|
|
9532
|
+
onChange={onToggle}
|
|
9533
|
+
className="absolute start-2 top-2 h-4 w-4 rounded"
|
|
9534
|
+
/>
|
|
9535
|
+
)}
|
|
9536
|
+
<div className="bg-muted relative mb-2 h-20 w-20 overflow-hidden rounded">
|
|
9537
|
+
{imageUrl ? (
|
|
9538
|
+
<Image src={imageUrl} alt={name} fill sizes="80px" className="object-cover" />
|
|
9539
|
+
) : (
|
|
9540
|
+
<div className="flex h-full w-full items-center justify-center">
|
|
9541
|
+
<svg className="text-muted-foreground h-8 w-8" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
|
9542
|
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" />
|
|
9543
|
+
</svg>
|
|
9544
|
+
</div>
|
|
9545
|
+
)}
|
|
9546
|
+
</div>
|
|
9547
|
+
<span className="text-foreground line-clamp-2 text-center text-xs font-medium">{name}</span>
|
|
9548
|
+
<span className="text-muted-foreground mt-1 text-xs">
|
|
9549
|
+
{formatPrice(price, { currency }) as string}
|
|
9550
|
+
</span>
|
|
9551
|
+
</label>
|
|
9552
|
+
);
|
|
9553
|
+
}
|
|
9554
|
+
|
|
9555
|
+
export function FrequentlyBoughtTogether({
|
|
9556
|
+
items,
|
|
9557
|
+
currentProduct,
|
|
9558
|
+
className,
|
|
9559
|
+
}: FrequentlyBoughtTogetherProps) {
|
|
9560
|
+
const { storeInfo } = useStoreInfo();
|
|
9561
|
+
const { refreshCart } = useCart();
|
|
9562
|
+
const t = useTranslations('productDetail');
|
|
9563
|
+
|
|
9564
|
+
// Only show up to 3 cross-sells
|
|
9565
|
+
const crossSells = items.slice(0, 3);
|
|
9566
|
+
|
|
9567
|
+
const [selected, setSelected] = useState<Set<string>>(() => new Set(crossSells.map((i) => i.id)));
|
|
9568
|
+
const [adding, setAdding] = useState(false);
|
|
9569
|
+
|
|
9570
|
+
if (!storeInfo?.upsell?.frequentlyBoughtTogetherEnabled) return null;
|
|
9571
|
+
if (crossSells.length === 0) return null;
|
|
9572
|
+
|
|
9573
|
+
const currency = storeInfo.currency || 'USD';
|
|
9574
|
+
|
|
9575
|
+
const currentPrice = getEffectivePrice(currentProduct);
|
|
9576
|
+
const currentImage = currentProduct.images?.[0];
|
|
9577
|
+
const currentImageUrl = currentImage
|
|
9578
|
+
? typeof currentImage === 'string'
|
|
9579
|
+
? currentImage
|
|
9580
|
+
: currentImage.url
|
|
9581
|
+
: null;
|
|
9582
|
+
|
|
9583
|
+
const totalPrice = crossSells
|
|
9584
|
+
.filter((item) => selected.has(item.id))
|
|
9585
|
+
.reduce((sum, item) => sum + getEffectivePrice(item), currentPrice);
|
|
9586
|
+
|
|
9587
|
+
const toggleItem = (id: string) => {
|
|
9588
|
+
setSelected((prev) => {
|
|
9589
|
+
const next = new Set(prev);
|
|
9590
|
+
if (next.has(id)) {
|
|
9591
|
+
next.delete(id);
|
|
9592
|
+
} else {
|
|
9593
|
+
next.add(id);
|
|
9594
|
+
}
|
|
9595
|
+
return next;
|
|
9596
|
+
});
|
|
9597
|
+
};
|
|
9598
|
+
|
|
9599
|
+
async function handleAddAll() {
|
|
9600
|
+
if (adding || selected.size === 0) return;
|
|
9601
|
+
try {
|
|
9602
|
+
setAdding(true);
|
|
9603
|
+
const { getClient } = await import('@/lib/brainerce');
|
|
9604
|
+
const client = getClient();
|
|
9605
|
+
const selectedItems = crossSells.filter((item) => selected.has(item.id));
|
|
9606
|
+
for (const item of selectedItems) {
|
|
9607
|
+
await client.smartAddToCart({ productId: item.id, quantity: 1 });
|
|
9608
|
+
}
|
|
9609
|
+
await refreshCart();
|
|
9610
|
+
} catch (err) {
|
|
9611
|
+
console.error('Failed to add items to cart:', err);
|
|
9612
|
+
} finally {
|
|
9613
|
+
setAdding(false);
|
|
9614
|
+
}
|
|
9615
|
+
}
|
|
9616
|
+
|
|
9617
|
+
return (
|
|
9618
|
+
<div className={cn('border-border rounded-lg border p-6', className)}>
|
|
9619
|
+
<h2 className="text-foreground mb-4 text-xl font-semibold">{t('frequentlyBoughtTogether')}</h2>
|
|
9620
|
+
|
|
9621
|
+
<div className="flex flex-wrap items-center gap-3">
|
|
9622
|
+
{/* Current product (always included, no checkbox) */}
|
|
9623
|
+
<ProductThumb
|
|
9624
|
+
name={currentProduct.name}
|
|
9625
|
+
imageUrl={currentImageUrl}
|
|
9626
|
+
price={currentPrice}
|
|
9627
|
+
currency={currency}
|
|
9628
|
+
checked={true}
|
|
9629
|
+
disabled
|
|
9630
|
+
/>
|
|
9631
|
+
|
|
9632
|
+
{crossSells.map((item) => {
|
|
9633
|
+
const img = item.images?.[0];
|
|
9634
|
+
const imgUrl = img ? (typeof img === 'string' ? img : img.url) : null;
|
|
9635
|
+
return (
|
|
9636
|
+
<div key={item.id} className="flex items-center gap-3">
|
|
9637
|
+
<span className="text-muted-foreground text-lg font-light">+</span>
|
|
9638
|
+
<ProductThumb
|
|
9639
|
+
name={item.name}
|
|
9640
|
+
imageUrl={imgUrl}
|
|
9641
|
+
price={getEffectivePrice(item)}
|
|
9642
|
+
currency={currency}
|
|
9643
|
+
checked={selected.has(item.id)}
|
|
9644
|
+
onToggle={() => toggleItem(item.id)}
|
|
9645
|
+
/>
|
|
9646
|
+
</div>
|
|
9647
|
+
);
|
|
9648
|
+
})}
|
|
9649
|
+
</div>
|
|
9650
|
+
|
|
9651
|
+
{/* Total + Add button */}
|
|
9652
|
+
<div className="mt-4 flex flex-wrap items-center gap-4">
|
|
9653
|
+
<span className="text-foreground text-lg font-semibold">
|
|
9654
|
+
{t('totalPrice', { price: formatPrice(totalPrice, { currency }) as string })}
|
|
9655
|
+
</span>
|
|
9656
|
+
<button
|
|
9657
|
+
onClick={handleAddAll}
|
|
9658
|
+
disabled={adding || selected.size === 0}
|
|
9659
|
+
className={cn(
|
|
9660
|
+
'bg-primary text-primary-foreground rounded px-5 py-2.5 text-sm font-medium transition-opacity hover:opacity-90',
|
|
9661
|
+
'disabled:cursor-not-allowed disabled:opacity-50'
|
|
9662
|
+
)}
|
|
9663
|
+
>
|
|
9664
|
+
{adding ? t('addingAll') : t('addSelectedToCart')}
|
|
9665
|
+
</button>
|
|
9666
|
+
</div>
|
|
9667
|
+
</div>
|
|
9668
|
+
);
|
|
9669
|
+
}
|
|
9670
|
+
`,
|
|
9671
|
+
"src/components/cart/free-shipping-bar.tsx": `'use client';
|
|
9672
|
+
|
|
9673
|
+
import { formatPrice } from 'brainerce';
|
|
9674
|
+
import { useStoreInfo, useCart } from '@/providers/store-provider';
|
|
9675
|
+
import { useTranslations } from '@/lib/translations';
|
|
9676
|
+
import { cn } from '@/lib/utils';
|
|
9677
|
+
|
|
9678
|
+
interface FreeShippingBarProps {
|
|
9679
|
+
className?: string;
|
|
9680
|
+
}
|
|
9681
|
+
|
|
9682
|
+
export function FreeShippingBar({ className }: FreeShippingBarProps) {
|
|
9683
|
+
const t = useTranslations('cart');
|
|
9684
|
+
const { storeInfo } = useStoreInfo();
|
|
9685
|
+
const { totals } = useCart();
|
|
9686
|
+
|
|
9687
|
+
const upsell = storeInfo?.upsell;
|
|
9688
|
+
const threshold = upsell?.freeShippingThreshold;
|
|
9689
|
+
const enabled = upsell?.freeShippingBarEnabled !== false;
|
|
9690
|
+
|
|
9691
|
+
// Don't render if disabled or no threshold configured
|
|
9692
|
+
if (!enabled || !threshold || threshold <= 0) return null;
|
|
9693
|
+
|
|
9694
|
+
const subtotal = totals.subtotal;
|
|
9695
|
+
const remaining = Math.max(0, threshold - subtotal);
|
|
9696
|
+
const progress = Math.min(100, (subtotal / threshold) * 100);
|
|
9697
|
+
const qualified = remaining <= 0;
|
|
9698
|
+
const currency = storeInfo?.currency || 'USD';
|
|
9699
|
+
|
|
9700
|
+
// Don't show if already qualified
|
|
9701
|
+
if (qualified) {
|
|
9702
|
+
return (
|
|
9703
|
+
<div className={cn('rounded-lg border border-green-200 bg-green-50 p-3', className)}>
|
|
9704
|
+
<div className="flex items-center gap-2 text-sm font-medium text-green-700">
|
|
9705
|
+
<svg className="h-4 w-4 shrink-0" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
|
9706
|
+
<path
|
|
9707
|
+
strokeLinecap="round"
|
|
9708
|
+
strokeLinejoin="round"
|
|
9709
|
+
strokeWidth={2}
|
|
9710
|
+
d="M5 13l4 4L19 7"
|
|
9711
|
+
/>
|
|
9712
|
+
</svg>
|
|
9713
|
+
{t('freeShippingQualified')}
|
|
9714
|
+
</div>
|
|
9715
|
+
</div>
|
|
9716
|
+
);
|
|
9717
|
+
}
|
|
9718
|
+
|
|
9719
|
+
return (
|
|
9720
|
+
<div className={cn('rounded-lg border border-amber-200 bg-amber-50 p-3', className)}>
|
|
9721
|
+
<p className="mb-2 text-sm text-amber-800">
|
|
9722
|
+
{t('freeShippingRemaining', {
|
|
9723
|
+
amount: formatPrice(remaining, { currency }) as string,
|
|
9724
|
+
})}
|
|
9725
|
+
</p>
|
|
9726
|
+
<div className="h-2 w-full overflow-hidden rounded-full bg-amber-200">
|
|
9727
|
+
<div
|
|
9728
|
+
className="h-full rounded-full bg-amber-500 transition-all duration-500 ease-out"
|
|
9729
|
+
style={{ width: \`\${progress}%\` }}
|
|
9730
|
+
/>
|
|
9731
|
+
</div>
|
|
9732
|
+
</div>
|
|
9733
|
+
);
|
|
9734
|
+
}
|
|
9735
|
+
`,
|
|
9736
|
+
"src/components/cart/cart-upgrade-banner.tsx": `'use client';
|
|
9737
|
+
|
|
9738
|
+
import { useState, useEffect } from 'react';
|
|
9739
|
+
import Image from 'next/image';
|
|
9740
|
+
import type { CartUpgradeSuggestion, CartItem as CartItemType } from 'brainerce';
|
|
9741
|
+
import { formatPrice } from 'brainerce';
|
|
9742
|
+
import { getClient } from '@/lib/brainerce';
|
|
9743
|
+
import { useStoreInfo } from '@/providers/store-provider';
|
|
9744
|
+
import { useTranslations } from '@/lib/translations';
|
|
9745
|
+
import { cn } from '@/lib/utils';
|
|
9746
|
+
|
|
9747
|
+
interface CartUpgradeBannerProps {
|
|
9748
|
+
suggestion: CartUpgradeSuggestion;
|
|
9749
|
+
cartItem: CartItemType;
|
|
9750
|
+
onUpgrade: () => void;
|
|
9751
|
+
className?: string;
|
|
9752
|
+
}
|
|
9753
|
+
|
|
9754
|
+
export function CartUpgradeBanner({
|
|
9755
|
+
suggestion,
|
|
9756
|
+
cartItem,
|
|
9757
|
+
onUpgrade,
|
|
9758
|
+
className,
|
|
9759
|
+
}: CartUpgradeBannerProps) {
|
|
9760
|
+
const { storeInfo } = useStoreInfo();
|
|
9761
|
+
const t = useTranslations('cart');
|
|
9762
|
+
const currency = storeInfo?.currency || 'USD';
|
|
9763
|
+
const [upgrading, setUpgrading] = useState(false);
|
|
9764
|
+
const [dismissed, setDismissed] = useState(false);
|
|
9765
|
+
|
|
9766
|
+
const storageKey = \`dismissed_upgrade_\${suggestion.sourceProductId}\`;
|
|
9767
|
+
|
|
9768
|
+
useEffect(() => {
|
|
9769
|
+
try {
|
|
9770
|
+
if (sessionStorage.getItem(storageKey)) {
|
|
9771
|
+
setDismissed(true);
|
|
9772
|
+
}
|
|
9773
|
+
} catch {}
|
|
9774
|
+
}, [storageKey]);
|
|
9775
|
+
|
|
9776
|
+
if (dismissed) return null;
|
|
9777
|
+
|
|
9778
|
+
const target = suggestion.targetProduct;
|
|
9779
|
+
const firstImage = target.images?.[0];
|
|
9780
|
+
const imageUrl = firstImage ? (typeof firstImage === 'string' ? firstImage : firstImage.url) : null;
|
|
9781
|
+
const formattedDelta = formatPrice(parseFloat(suggestion.priceDelta), { currency }) as string;
|
|
9782
|
+
|
|
9783
|
+
function handleDismiss() {
|
|
9784
|
+
try {
|
|
9785
|
+
sessionStorage.setItem(storageKey, '1');
|
|
9786
|
+
} catch {}
|
|
9787
|
+
setDismissed(true);
|
|
9788
|
+
}
|
|
9789
|
+
|
|
9790
|
+
async function handleUpgrade() {
|
|
9791
|
+
if (upgrading) return;
|
|
9792
|
+
try {
|
|
9793
|
+
setUpgrading(true);
|
|
9794
|
+
const client = getClient();
|
|
9795
|
+
await client.smartRemoveFromCart(cartItem.productId, cartItem.variantId || undefined);
|
|
9796
|
+
await client.smartAddToCart({ productId: target.id, quantity: cartItem.quantity });
|
|
9797
|
+
onUpgrade();
|
|
9798
|
+
} catch (err) {
|
|
9799
|
+
console.error('Failed to upgrade cart item:', err);
|
|
9800
|
+
} finally {
|
|
9801
|
+
setUpgrading(false);
|
|
9802
|
+
}
|
|
9803
|
+
}
|
|
9804
|
+
|
|
9805
|
+
return (
|
|
9806
|
+
<div
|
|
9807
|
+
className={cn(
|
|
9808
|
+
'bg-primary/5 border-primary/20 relative flex items-center gap-3 rounded-lg border px-4 py-3',
|
|
9809
|
+
className
|
|
9810
|
+
)}
|
|
9811
|
+
>
|
|
9812
|
+
{/* Dismiss button */}
|
|
9813
|
+
<button
|
|
9814
|
+
type="button"
|
|
9815
|
+
onClick={handleDismiss}
|
|
9816
|
+
className="text-muted-foreground hover:text-foreground absolute end-2 top-2 text-xs"
|
|
9817
|
+
aria-label={t('dismissUpgrade')}
|
|
9818
|
+
>
|
|
9819
|
+
<svg className="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
|
|
9820
|
+
<path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" />
|
|
9821
|
+
</svg>
|
|
9822
|
+
</button>
|
|
9823
|
+
|
|
9824
|
+
{/* Product image */}
|
|
9825
|
+
<div className="bg-muted relative h-12 w-12 flex-shrink-0 overflow-hidden rounded">
|
|
9826
|
+
{imageUrl ? (
|
|
9827
|
+
<Image src={imageUrl} alt={target.name} fill sizes="48px" className="object-cover" />
|
|
9828
|
+
) : (
|
|
9829
|
+
<div className="text-muted-foreground flex h-full w-full items-center justify-center">
|
|
9830
|
+
<svg className="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
|
9831
|
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" />
|
|
9832
|
+
</svg>
|
|
9833
|
+
</div>
|
|
9834
|
+
)}
|
|
9835
|
+
</div>
|
|
9836
|
+
|
|
9837
|
+
{/* Text */}
|
|
9838
|
+
<div className="min-w-0 flex-1">
|
|
9839
|
+
<p className="text-foreground text-sm font-medium">
|
|
9840
|
+
{t('upgradeFor', { name: target.name, amount: formattedDelta })}
|
|
9841
|
+
</p>
|
|
9842
|
+
</div>
|
|
9843
|
+
|
|
9844
|
+
{/* Upgrade button */}
|
|
9845
|
+
<button
|
|
9846
|
+
type="button"
|
|
9847
|
+
onClick={handleUpgrade}
|
|
9848
|
+
disabled={upgrading}
|
|
9849
|
+
className={cn(
|
|
9850
|
+
'bg-primary text-primary-foreground flex-shrink-0 rounded px-3 py-1.5 text-xs font-medium transition-opacity hover:opacity-90',
|
|
9851
|
+
'disabled:cursor-not-allowed disabled:opacity-50'
|
|
9852
|
+
)}
|
|
9853
|
+
>
|
|
9854
|
+
{upgrading ? t('upgrading') : t('upgrade')}
|
|
9855
|
+
</button>
|
|
9856
|
+
</div>
|
|
9857
|
+
);
|
|
9858
|
+
}
|
|
9859
|
+
`,
|
|
9860
|
+
"src/components/cart/cart-bundle-offer.tsx": `'use client';
|
|
9861
|
+
|
|
9862
|
+
import { useState } from 'react';
|
|
9863
|
+
import Image from 'next/image';
|
|
9864
|
+
import type { CartBundleOffer as CartBundleOfferType } from 'brainerce';
|
|
9865
|
+
import { formatPrice } from 'brainerce';
|
|
9866
|
+
import { getClient } from '@/lib/brainerce';
|
|
9867
|
+
import { useStoreInfo } from '@/providers/store-provider';
|
|
9868
|
+
import { useTranslations } from '@/lib/translations';
|
|
9869
|
+
import { cn } from '@/lib/utils';
|
|
9870
|
+
|
|
9871
|
+
interface CartBundleOfferCardProps {
|
|
9872
|
+
offer: CartBundleOfferType;
|
|
9873
|
+
onAdd: () => void;
|
|
9874
|
+
className?: string;
|
|
9875
|
+
}
|
|
9876
|
+
|
|
9877
|
+
export function CartBundleOfferCard({ offer, onAdd, className }: CartBundleOfferCardProps) {
|
|
9878
|
+
const { storeInfo } = useStoreInfo();
|
|
9879
|
+
const t = useTranslations('cart');
|
|
9880
|
+
const currency = storeInfo?.currency || 'USD';
|
|
9881
|
+
const [adding, setAdding] = useState(false);
|
|
9882
|
+
|
|
9883
|
+
const product = offer.bundleProduct;
|
|
9884
|
+
const firstImage = product.images?.[0];
|
|
9885
|
+
const imageUrl = firstImage ? (typeof firstImage === 'string' ? firstImage : firstImage.url) : null;
|
|
9886
|
+
const originalPrice = parseFloat(offer.originalPrice);
|
|
9887
|
+
const discountedPrice = parseFloat(offer.discountedPrice);
|
|
9888
|
+
const discountLabel =
|
|
9889
|
+
offer.discountType === 'PERCENTAGE'
|
|
9890
|
+
? \`\${offer.discountValue}%\`
|
|
9891
|
+
: formatPrice(parseFloat(offer.discountValue), { currency }) as string;
|
|
9892
|
+
|
|
9893
|
+
async function handleAdd() {
|
|
9894
|
+
if (adding) return;
|
|
9895
|
+
try {
|
|
9896
|
+
setAdding(true);
|
|
9897
|
+
const client = getClient();
|
|
9898
|
+
await client.smartAddToCart({
|
|
9899
|
+
productId: product.id,
|
|
9900
|
+
variantId: offer.bundleVariantId || undefined,
|
|
9901
|
+
quantity: 1,
|
|
9902
|
+
});
|
|
9903
|
+
onAdd();
|
|
9904
|
+
} catch (err) {
|
|
9905
|
+
console.error('Failed to add bundle item:', err);
|
|
9906
|
+
} finally {
|
|
9907
|
+
setAdding(false);
|
|
9908
|
+
}
|
|
9909
|
+
}
|
|
9910
|
+
|
|
9911
|
+
return (
|
|
9912
|
+
<div
|
|
9913
|
+
className={cn(
|
|
9914
|
+
'bg-background border-border flex items-center gap-4 rounded-lg border p-4',
|
|
9915
|
+
className
|
|
9916
|
+
)}
|
|
9917
|
+
>
|
|
9918
|
+
{/* Product image */}
|
|
9919
|
+
<div className="bg-muted relative h-16 w-16 flex-shrink-0 overflow-hidden rounded">
|
|
9920
|
+
{imageUrl ? (
|
|
9921
|
+
<Image src={imageUrl} alt={product.name} fill sizes="64px" className="object-cover" />
|
|
9922
|
+
) : (
|
|
9923
|
+
<div className="text-muted-foreground flex h-full w-full items-center justify-center">
|
|
9924
|
+
<svg className="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
|
9925
|
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" />
|
|
9926
|
+
</svg>
|
|
9927
|
+
</div>
|
|
9928
|
+
)}
|
|
9929
|
+
</div>
|
|
9930
|
+
|
|
9931
|
+
{/* Details */}
|
|
9932
|
+
<div className="min-w-0 flex-1">
|
|
9933
|
+
<p className="text-foreground text-sm font-medium">{offer.name}</p>
|
|
9934
|
+
{offer.description && (
|
|
9935
|
+
<p className="text-muted-foreground mt-0.5 text-xs">{offer.description}</p>
|
|
9936
|
+
)}
|
|
9937
|
+
<div className="mt-1 flex items-center gap-2">
|
|
9938
|
+
<span className="text-muted-foreground text-sm line-through">
|
|
9939
|
+
{formatPrice(originalPrice, { currency }) as string}
|
|
9940
|
+
</span>
|
|
9941
|
+
<span className="text-foreground text-sm font-semibold">
|
|
9942
|
+
{formatPrice(discountedPrice, { currency }) as string}
|
|
9943
|
+
</span>
|
|
9944
|
+
<span className="bg-destructive/10 text-destructive rounded px-1.5 py-0.5 text-xs font-medium">
|
|
9945
|
+
-{discountLabel}
|
|
9946
|
+
</span>
|
|
9947
|
+
</div>
|
|
9948
|
+
</div>
|
|
9949
|
+
|
|
9950
|
+
{/* Add button */}
|
|
9951
|
+
<button
|
|
9952
|
+
type="button"
|
|
9953
|
+
onClick={handleAdd}
|
|
9954
|
+
disabled={adding}
|
|
9955
|
+
className={cn(
|
|
9956
|
+
'bg-primary text-primary-foreground flex-shrink-0 rounded px-4 py-2 text-xs font-medium transition-opacity hover:opacity-90',
|
|
9957
|
+
'disabled:cursor-not-allowed disabled:opacity-50'
|
|
9958
|
+
)}
|
|
9959
|
+
>
|
|
9960
|
+
{adding ? t('addingBundle') : t('addBundleItem')}
|
|
9961
|
+
</button>
|
|
9962
|
+
</div>
|
|
9963
|
+
);
|
|
9964
|
+
}
|
|
9965
|
+
`,
|
|
9966
|
+
"src/components/checkout/order-bump-card.tsx": `'use client';
|
|
9967
|
+
|
|
9968
|
+
import Image from 'next/image';
|
|
9969
|
+
import type { OrderBump } from 'brainerce';
|
|
9970
|
+
import { formatPrice } from 'brainerce';
|
|
9971
|
+
import { useStoreInfo } from '@/providers/store-provider';
|
|
9972
|
+
import { useTranslations } from '@/lib/translations';
|
|
9973
|
+
import { cn } from '@/lib/utils';
|
|
9974
|
+
|
|
9975
|
+
interface OrderBumpCardProps {
|
|
9976
|
+
bump: OrderBump;
|
|
9977
|
+
isAdded: boolean;
|
|
9978
|
+
onToggle: (bumpId: string, add: boolean) => void;
|
|
9979
|
+
loading: boolean;
|
|
9980
|
+
className?: string;
|
|
9981
|
+
}
|
|
9982
|
+
|
|
9983
|
+
export function OrderBumpCard({ bump, isAdded, onToggle, loading, className }: OrderBumpCardProps) {
|
|
9984
|
+
const { storeInfo } = useStoreInfo();
|
|
9985
|
+
const t = useTranslations('checkout');
|
|
9986
|
+
const currency = storeInfo?.currency || 'USD';
|
|
9987
|
+
|
|
9988
|
+
const product = bump.bumpProduct;
|
|
9989
|
+
const firstImage = product.images?.[0];
|
|
9990
|
+
const imageUrl = firstImage ? (typeof firstImage === 'string' ? firstImage : firstImage.url) : null;
|
|
9991
|
+
const originalPrice = parseFloat(bump.originalPrice);
|
|
9992
|
+
const hasDiscount = bump.discountedPrice != null;
|
|
9993
|
+
const discountedPrice = hasDiscount ? parseFloat(bump.discountedPrice!) : null;
|
|
9994
|
+
|
|
9995
|
+
return (
|
|
9996
|
+
<label
|
|
9997
|
+
className={cn(
|
|
9998
|
+
'border-border hover:border-primary/50 flex cursor-pointer items-start gap-3 rounded-lg border p-3 transition-colors',
|
|
9999
|
+
isAdded && 'border-primary bg-primary/5',
|
|
10000
|
+
loading && 'pointer-events-none opacity-60',
|
|
10001
|
+
className
|
|
10002
|
+
)}
|
|
10003
|
+
>
|
|
10004
|
+
<input
|
|
10005
|
+
type="checkbox"
|
|
10006
|
+
checked={isAdded}
|
|
10007
|
+
onChange={() => onToggle(bump.id, !isAdded)}
|
|
10008
|
+
disabled={loading}
|
|
10009
|
+
className="mt-1 h-4 w-4 shrink-0 rounded"
|
|
10010
|
+
/>
|
|
10011
|
+
|
|
10012
|
+
{/* Image */}
|
|
10013
|
+
{imageUrl && (
|
|
10014
|
+
<div className="bg-muted relative h-10 w-10 shrink-0 overflow-hidden rounded">
|
|
10015
|
+
<Image src={imageUrl} alt={product.name} fill sizes="40px" className="object-cover" />
|
|
10016
|
+
</div>
|
|
10017
|
+
)}
|
|
10018
|
+
|
|
10019
|
+
{/* Content */}
|
|
10020
|
+
<div className="min-w-0 flex-1">
|
|
10021
|
+
<p className="text-foreground text-sm font-medium">{bump.title}</p>
|
|
10022
|
+
{bump.description && (
|
|
10023
|
+
<p className="text-muted-foreground mt-0.5 text-xs">{bump.description}</p>
|
|
10024
|
+
)}
|
|
10025
|
+
<div className="mt-1 flex items-center gap-2">
|
|
10026
|
+
{hasDiscount ? (
|
|
10027
|
+
<>
|
|
10028
|
+
<span className="text-muted-foreground text-xs line-through">
|
|
10029
|
+
{formatPrice(originalPrice, { currency }) as string}
|
|
10030
|
+
</span>
|
|
10031
|
+
<span className="text-foreground text-sm font-semibold">
|
|
10032
|
+
{formatPrice(discountedPrice!, { currency }) as string}
|
|
10033
|
+
</span>
|
|
10034
|
+
</>
|
|
10035
|
+
) : (
|
|
10036
|
+
<span className="text-foreground text-sm font-semibold">
|
|
10037
|
+
{formatPrice(originalPrice, { currency }) as string}
|
|
10038
|
+
</span>
|
|
10039
|
+
)}
|
|
10040
|
+
</div>
|
|
10041
|
+
</div>
|
|
10042
|
+
</label>
|
|
10043
|
+
);
|
|
10044
|
+
}
|
|
9168
10045
|
`,
|
|
9169
10046
|
"src/hooks/use-search.ts": `'use client';
|
|
9170
10047
|
|