learnshopify.dev/Workspace
code reading

Trace the validation Function, then answer.

// extensions/wholesale-minimum/src/cart_validations_generate_run.js
//
// Input query (cart_validations_generate_run.graphql):
//
//   query CartValidationsGenerateRunInput {
//     cart {
//       buyerIdentity {
//         customer { hasAnyTag(tags: ["wholesale"]) }
//       }
//       lines {
//         quantity
//         merchandise {
//           __typename
//           ... on ProductVariant {
//             product {
//               title
//               metafield(namespace: "wholesale", key: "min_qty") { value }
//             }
//           }
//         }
//       }
//     }
//   }

export function cartValidationsGenerateRun(input) {
  const errors = [];
  const isWholesale =
    input.cart.buyerIdentity?.customer?.hasAnyTag ?? false;

  if (isWholesale) {
    for (const line of input.cart.lines) {
      if (line.merchandise.__typename !== "ProductVariant") continue;

      const product = line.merchandise.product;
      if (!product.metafield) continue;

      const minQty = parseInt(product.metafield.value, 10);
      if (line.quantity < minQty) {
        errors.push({
          message: `${product.title}: wholesale minimum is ${minQty} bags`,
          target: "$.cart",
        });
      }
    }
  }

  return {
    operations: [{ validationAdd: { errors } }],
  };
}
1. Marco (wholesale) tries to check out with 4 bags against a minimum of 24. Where does the error surface?
2. Why does this catch the reorder-app order that the PDP's `min` attribute missed?
3. Marco orders 24 bags and the rule is satisfied. What does the function return?
4. A retail guest (not signed in) checks out with 4 bags. What happens?
0 / 1 task solved