@porulle/adapter-shopify 0.39.0 → 0.40.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/index.js CHANGED
@@ -55,6 +55,40 @@ function pricesForVariant(variant, currency) {
55
55
  ...(compareAtAmount !== undefined && compareAtAmount !== amount ? { compareAtAmount } : {}),
56
56
  }];
57
57
  }
58
+ /**
59
+ * Shopify carries a variant's weight twice: `grams`, which it normalises itself,
60
+ * and `weight` with a `weight_unit`. Prefer `grams`; fall back to converting
61
+ * `weight` only when `grams` is absent or zero.
62
+ *
63
+ * Returns `undefined` — never `0` — when no weight is known, so the caller omits
64
+ * the key entirely. A written `0` is indistinguishable from a genuinely
65
+ * weightless item and would defeat a downstream default-parcel substitution.
66
+ *
67
+ * An unrecognised `weight_unit` is refused rather than assumed to be grams:
68
+ * reading "lbs" (a spelling Shopify does not use, but a proxy might) as grams
69
+ * under-prices a parcel by a factor of 453.
70
+ */
71
+ function weightGramsForVariant(variant) {
72
+ const { grams, weight, weight_unit: weightUnit } = variant;
73
+ if (typeof grams === "number" && Number.isFinite(grams) && grams > 0) {
74
+ return Math.round(grams);
75
+ }
76
+ if (typeof weight === "number" && Number.isFinite(weight) && weight > 0) {
77
+ switch (weightUnit ?? "g") {
78
+ case "g":
79
+ return Math.round(weight);
80
+ case "kg":
81
+ return Math.round(weight * 1000);
82
+ case "oz":
83
+ return Math.round(weight * 28.349523125);
84
+ case "lb":
85
+ return Math.round(weight * 453.59237);
86
+ default:
87
+ return undefined;
88
+ }
89
+ }
90
+ return undefined;
91
+ }
58
92
  function catalogStatus(value) {
59
93
  return value === "draft" || value === "active" || value === "archived" ? value : undefined;
60
94
  }
@@ -249,12 +283,14 @@ export function shopifyConnector(options = {}) {
249
283
  return value != null && value !== "" ? [[option.name, value]] : [];
250
284
  }));
251
285
  const prices = pricesForVariant(variant, currency);
286
+ const weightGrams = weightGramsForVariant(variant);
252
287
  return {
253
288
  externalId: String(variant.id),
254
289
  ...(variant.sku ? { sku: variant.sku } : {}),
255
290
  ...(variant.barcode ? { barcode: variant.barcode } : {}),
256
291
  ...(Object.keys(optionValues).length > 0 ? { optionValues } : {}),
257
292
  ...(prices ? { prices } : {}),
293
+ ...(weightGrams !== undefined ? { metadata: { weightGrams } } : {}),
258
294
  };
259
295
  });
260
296
  const category = product.product_type ? slugify(product.product_type) : "";