← back to Shopify Sample Shipping

proof-designer.mjs

56 lines

// READ-ONLY proof: does TRADESHIP give a designer $0 shipping on a 12-sample ($51) cart?
// Uses draftOrderCalculate (creates NOTHING) with a real in-segment customer + the code.
import { query } from './query.mjs';
const SAMPLE = 'gid://shopify/ProductVariant/44090076954675';
const lineItems = [{ variantId: SAMPLE, quantity: 12 }]; // 12 samples = $51 (over the $45 band)
const addr = { address1: '15442 Ventura Blvd', city: 'Sherman Oaks', provinceCode: 'CA', countryCode: 'US', zip: '91403' };

async function pickCustomer(q, label) {
  const r = await query(`query($q:String!){customers(first:1,query:$q){nodes{id email tags}}}`, { q });
  const c = r.customers.nodes[0];
  console.log(`${label}: ${c ? c.email + '  tags=[' + c.tags.join(', ') + ']' : 'NONE FOUND'}`);
  return c?.id;
}
const CALC = `mutation($input:DraftOrderInput!){draftOrderCalculate(input:$input){calculatedDraftOrder{
  totalShippingPriceSet{shopMoney{amount}} shippingLine{title price}
  totalDiscountsSet{shopMoney{amount}}
  availableShippingRates{title price{amount}} discountCodes
  appliedDiscount{title} } userErrors{field message}}}`;

async function scenario(label, customerId, codes, ship) {
  const input = { lineItems, shippingAddress: addr };
  if (customerId) input.purchasingEntity = { customerId };
  if (codes) input.discountCodes = codes;
  if (ship) input.shippingLine = ship;
  const d = (await query(CALC, { input })).draftOrderCalculate;
  if (d.userErrors?.length) { return { err: JSON.stringify(d.userErrors) }; }
  const c = d.calculatedDraftOrder;
  const rates = c.availableShippingRates || [];
  const free = rates.some(r => parseFloat(r.price.amount) === 0);
  if (label) {
    console.log(`  ${label}:`);
    console.log(`     applied discountCodes: ${JSON.stringify(c.discountCodes)} | selected shippingLine: ${c.shippingLine?.title || '—'} $${c.shippingLine?.price ?? '—'}`);
    console.log(`     totalShipping: $${c.totalShippingPriceSet?.shopMoney?.amount} | totalDiscounts: $${c.totalDiscountsSet?.shopMoney?.amount}`);
  }
  return { free, codes: c.discountCodes, ship: c.totalShippingPriceSet?.shopMoney?.amount };
}

console.log('=== PROOF: TRADESHIP for a designer on 12 samples ($51, over the $45 band) ===\n');
// try in-segment designers until one passes Shopify's calc email-domain validation
const cands = (await query(`query($q:String!){customers(first:50,query:$q){nodes{id email tags}}}`,
  { q: "tag:'sample-freeship'" })).customers.nodes;
let designer = null, designerEmail = null;
for (const c of cands) {
  const t = await scenario('', c.id, ['TRADESHIP']);
  if (!t.err) { designer = c.id; designerEmail = c.email; break; }
}
console.log('in-segment DESIGNER used:', designerEmail || 'NONE of first 50 validated');
const retail = await pickCustomer("tag:'Home Owner'", 'RETAIL (Home Owner)');
console.log();
const UPS = { title: 'UPS® Ground', price: '20.53' }; // select the real carrier rate, then see if the code zeroes it
if (designer) {
  await scenario('A) DESIGNER + TRADESHIP + UPS rate  (expect shipping discounted to $0)', designer, ['TRADESHIP'], UPS);
  await scenario('C) DESIGNER + no code + UPS rate    (expect shipping stays $20.53)', designer, null, UPS);
} else console.log('  (could not find an in-segment customer whose email passes calc validation)');
await scenario('B) RETAIL   + TRADESHIP + UPS rate  (expect code rejected, shipping stays $20.53)', retail, ['TRADESHIP'], UPS);