/**
* Customer-facing Cart & Checkout Tools
* Add to cart, generate checkout links, check shipping
*/
interface AddToCartParams {
variant_id: string;
quantity?: number;
}
interface CreateCheckoutParams {
items: Array<{ variant_id: string; quantity: number }>;
discount_code?: string;
}
interface CheckShippingParams {
zipcode: string;
country_code?: string;
}
const STORE_URL = process.env.SHOPIFY_STORE_URL || '';
export function addToCart(params: AddToCartParams) {
const { variant_id, quantity = 1 } = params;
// Generate Shopify add-to-cart URL
const cartUrl = `https://${STORE_URL}/cart/add?id=${variant_id}&quantity=${quantity}`;
const checkoutUrl = `https://${STORE_URL}/cart/${variant_id}:${quantity}`;
return {
success: true,
message: `Added ${quantity} item(s) to cart`,
cart_url: cartUrl,
checkout_url: checkoutUrl,
instructions: 'Click the cart_url to add items, or checkout_url to go directly to checkout',
};
}
export function createCheckoutLink(params: CreateCheckoutParams) {
const { items, discount_code } = params;
// Build cart permalink: /cart/variant_id:quantity,variant_id:quantity
const cartItems = items.map((item) => `${item.variant_id}:${item.quantity}`).join(',');
let checkoutUrl = `https://${STORE_URL}/cart/${cartItems}`;
if (discount_code) {
checkoutUrl += `?discount=${encodeURIComponent(discount_code)}`;
}
return {
success: true,
checkout_url: checkoutUrl,
items_count: items.reduce((sum, item) => sum + item.quantity, 0),
discount_code: discount_code || null,
};
}
export function checkShipping(params: CheckShippingParams) {
const { zipcode, country_code = 'US' } = params;
// For now, return general shipping info
// In a real implementation, you would call Shopify's shipping rates API
// Basic US shipping zones
const usShippingInfo: Record<string, { zone: string; estimate: string }> = {
// West Coast
'9': { zone: 'West Coast', estimate: '3-5 business days' },
// Mountain
'8': { zone: 'Mountain', estimate: '4-6 business days' },
'7': { zone: 'South Central', estimate: '4-6 business days' },
// Midwest
'6': { zone: 'Midwest', estimate: '3-5 business days' },
'5': { zone: 'Midwest', estimate: '3-5 business days' },
'4': { zone: 'Great Lakes', estimate: '3-5 business days' },
// East Coast
'3': { zone: 'Southeast', estimate: '3-5 business days' },
'2': { zone: 'Mid-Atlantic', estimate: '2-4 business days' },
'1': { zone: 'Northeast', estimate: '2-4 business days' },
'0': { zone: 'New England', estimate: '2-4 business days' },
};
if (country_code === 'US' && zipcode.length >= 1) {
const firstDigit = zipcode[0];
const shippingInfo = usShippingInfo[firstDigit] || { zone: 'Standard', estimate: '5-7 business days' };
return {
success: true,
zipcode,
country: 'United States',
shipping_zone: shippingInfo.zone,
estimated_delivery: shippingInfo.estimate,
shipping_options: [
{
name: 'Standard Shipping',
price: 'Calculated at checkout',
estimate: shippingInfo.estimate,
},
{
name: 'Express Shipping',
price: 'Calculated at checkout',
estimate: '1-2 business days',
},
],
note: 'Final shipping costs calculated at checkout based on order weight and destination.',
};
}
// International shipping
return {
success: true,
zipcode,
country_code,
shipping_zone: 'International',
estimated_delivery: '7-14 business days',
shipping_options: [
{
name: 'International Standard',
price: 'Calculated at checkout',
estimate: '7-14 business days',
},
{
name: 'International Express',
price: 'Calculated at checkout',
estimate: '3-5 business days',
},
],
note: 'International orders may be subject to customs duties and taxes.',
};
}
export function getShippingZones() {
return {
zones: [
{ name: 'United States', countries: ['US'], estimate: '2-7 business days' },
{ name: 'Canada', countries: ['CA'], estimate: '5-10 business days' },
{ name: 'Europe', countries: ['UK', 'DE', 'FR', 'ES', 'IT'], estimate: '7-14 business days' },
{ name: 'Australia/NZ', countries: ['AU', 'NZ'], estimate: '10-14 business days' },
{ name: 'Rest of World', countries: ['Other'], estimate: '14-21 business days' },
],
note: 'Shipping times are estimates and may vary based on carrier and customs processing.',
};
}