ChemPal Documentation - v1.6.0
    Preparing search index...
    • Filters a grouped product against the user's location, hiding options they can't buy. The parent product is one purchasable option and its variants are the others (they're disjoint — SupplierBase.groupVariants splices the representative out of the variant list). Restricted variants are pruned; if the representative itself is restricted but a variant is buyable, the cheapest buyable variant is promoted into the representative's sale fields (identity fields are kept). Returns undefined when no option is buyable.

      Type Parameters

      Parameters

      • product: T

        The grouped product to filter

      • Optionallocation: string

        The user's ISO 3166-1 alpha-2 location code (may be undefined)

      Returns undefined | T

      The product with restricted variants pruned (or a promoted representative), or undefined when the user can't buy any option

      // Parent buyable, one variant excluded for US:
      filterRestrictedProduct(product, "US"); // { ...product, variants: [buyableVariant] }
      // Parent and every variant excluded:
      filterRestrictedProduct(product, "US"); // undefined
      export function filterRestrictedProduct<T extends Product>(
      product: T,
      location?: string,
      ): T | undefined {
      const variants = product.variants ?? [];
      const buyableVariants = variants.filter((variant) => canUserBuy(variant, location));
      const parentBuyable = canUserBuy(product, location);

      if (!parentBuyable && buyableVariants.length === 0) {
      return undefined;
      }

      if (parentBuyable) {
      return variants.length > 0 ? { ...product, variants: buyableVariants } : product;
      }

      // Parent is restricted but a variant is buyable — promote the cheapest buyable variant
      // into the representative row, keeping the product's identity fields.
      const representative = [...buyableVariants].sort(
      (a, b) => (a.price ?? Number.POSITIVE_INFINITY) - (b.price ?? Number.POSITIVE_INFINITY),
      )[0];
      return {
      ...product,
      title: representative.title ?? product.title,
      price: representative.price ?? product.price,
      quantity: representative.quantity ?? product.quantity,
      uom: representative.uom ?? product.uom,
      url: representative.url ?? product.url,
      purchaseRestriction: representative.purchaseRestriction,
      variants: buyableVariants,
      };
      }