Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Expose quantity rules and order minimums over the API
Damian Legawiecf1966ff8e66b
diff --git a/packages/admin-sdk/src/types/generated/Cart.ts b/packages/admin-sdk/src/types/generated/Cart.tsindex 22b4422846..82525d3f34 100644--- a/packages/admin-sdk/src/types/generated/Cart.ts+++ b/packages/admin-sdk/src/types/generated/Cart.ts@@ -1,4 +1,4 @@-// Typelizer digest f8f56f6cdedf765e65982549743116e2+// Typelizer digest 383e2268e77462e077844f3ba2596da3//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {LineItem, Address, Market, AppliedPromotion} from '@/types'@@ -51,6 +51,9 @@ interface Cart {current_step: string;completed_steps: Array<string>;requirements: Array<{step: string, field: string, code: string, message: string}>;+ order_minimum: number | null;+ order_minimum_shortfall: number | null;+ below_order_minimum: boolean;shipping_eq_billing_address: boolean;items: Array<LineItem>;billing_address: Address | null;diff --git a/packages/admin-sdk/src/types/generated/Catalog.ts b/packages/admin-sdk/src/types/generated/Catalog.tsindex c17299205b..efb09800bd 100644--- a/packages/admin-sdk/src/types/generated/Catalog.ts+++ b/packages/admin-sdk/src/types/generated/Catalog.ts@@ -1,7 +1,7 @@-// Typelizer digest 6d7bb9370fb528e2f636b56464c167f6+// Typelizer digest 0643266f193be6461c09b87b99bdcada//// DO NOT MODIFY: This file was automatically generated by Typelizer.-import type {PriceList, CatalogAssignment} from '@/types'+import type {CatalogOrderMinimum, PriceList, CatalogAssignment} from '@/types'interface Catalog {id: string;@@ -10,10 +10,15 @@ interface Catalog {active: boolean;position: number | null;metadata: Record<string, unknown> | null;+ minimum_order_quantity: number | null;+ order_multiple: number | null;created_at: string;updated_at: string;price_list_id: string | null;products_count: number;+ quantity_rules_count: number;+ order_minimums_count: number;+ order_minimums?: Array<CatalogOrderMinimum>;price_list?: PriceList;assignments?: Array<CatalogAssignment>;}diff --git a/packages/admin-sdk/src/types/generated/CatalogOrderMinimum.ts b/packages/admin-sdk/src/types/generated/CatalogOrderMinimum.tsnew file mode 100644index 0000000000..335ee74d02--- /dev/null+++ b/packages/admin-sdk/src/types/generated/CatalogOrderMinimum.ts@@ -0,0 +1,14 @@+// Typelizer digest fe037ceb54f1a385e3807be32f0811bf+//+// DO NOT MODIFY: This file was automatically generated by Typelizer.++interface CatalogOrderMinimum {+ id: string;+ currency: string;+ created_at: string;+ updated_at: string;+ amount: string;+ display_amount: string;+}++export default CatalogOrderMinimum;diff --git a/packages/admin-sdk/src/types/generated/CatalogQuantityRule.ts b/packages/admin-sdk/src/types/generated/CatalogQuantityRule.tsnew file mode 100644index 0000000000..acb2d26ef0--- /dev/null+++ b/packages/admin-sdk/src/types/generated/CatalogQuantityRule.ts@@ -0,0 +1,17 @@+// Typelizer digest 7d69776643af2080ae0364f20ea5cfdd+//+// DO NOT MODIFY: This file was automatically generated by Typelizer.++interface CatalogQuantityRule {+ id: string;+ minimum_order_quantity: number | null;+ order_multiple: number | null;+ created_at: string;+ updated_at: string;+ variant_id: string;+ variant_sku: string | null;+ product_name: string | null;+ options_text: string | null;+}++export default CatalogQuantityRule;diff --git a/packages/admin-sdk/src/types/generated/Variant.ts b/packages/admin-sdk/src/types/generated/Variant.tsindex a2929a3a0b..8c6f9bbc2b 100644--- a/packages/admin-sdk/src/types/generated/Variant.ts+++ b/packages/admin-sdk/src/types/generated/Variant.ts@@ -1,4 +1,4 @@-// Typelizer digest 861b6e6d2415a0063b1116344e39c9cb+// Typelizer digest 39f63c50e4aec82b00daa1fd51d03dcf//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {Price, PriceHistory, Seller, Media, OptionValue, CustomField, StockLevel} from '@/types'@@ -20,6 +20,10 @@ interface Variant {height: number | null;width: number | null;depth: number | null;+ minimum_order_quantity: number | null;+ order_multiple: number | null;+ purchase_unit: string | null;+ units_per_carton: number | null;price: Price;original_price: Price | null;seller_id: string | null;diff --git a/packages/admin-sdk/src/types/generated/index.ts b/packages/admin-sdk/src/types/generated/index.tsindex 315aeea5df..f6945eb0f9 100644--- a/packages/admin-sdk/src/types/generated/index.ts+++ b/packages/admin-sdk/src/types/generated/index.ts@@ -1,4 +1,4 @@-// Typelizer digest 29ae2a17067a40178563d3f33103058f+// Typelizer digest 7cb79c0fae09117231a2716a46e0f5b7//// DO NOT MODIFY: This file was automatically generated by Typelizer.export type { default as Address } from './Address'@@ -8,6 +8,8 @@ export type { default as ApiKey } from './ApiKey'export type { default as AppliedPromotion } from './AppliedPromotion'export type { default as Cart } from './Cart'export type { default as CatalogAssignment } from './CatalogAssignment'+export type { default as CatalogOrderMinimum } from './CatalogOrderMinimum'+export type { default as CatalogQuantityRule } from './CatalogQuantityRule'export type { default as Catalog } from './Catalog'export type { default as Category } from './Category'export type { default as Channel } from './Channel'diff --git a/packages/sdk/src/types/generated/Cart.ts b/packages/sdk/src/types/generated/Cart.tsindex 7483e2a18c..c1bad7f6c3 100644--- a/packages/sdk/src/types/generated/Cart.ts+++ b/packages/sdk/src/types/generated/Cart.ts@@ -1,4 +1,4 @@-// Typelizer digest 7c7daa93be0e733cd802a889a07d7f20+// Typelizer digest 0c4581177945cd335cedf426f3575f3f//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {AppliedPromotion, Fee, LineItem, Fulfillment, Payment, Address, PaymentMethod, GiftCard, Market} from '@/types'@@ -51,6 +51,9 @@ interface Cart {current_step: string;completed_steps: Array<string>;requirements: Array<{step: string, field: string, code: string, message: string}>;+ order_minimum: number | null;+ order_minimum_shortfall: number | null;+ below_order_minimum: boolean;shipping_eq_billing_address: boolean;discounts: Array<AppliedPromotion>;fees: Array<Fee>;diff --git a/packages/sdk/src/types/generated/Variant.ts b/packages/sdk/src/types/generated/Variant.tsindex c7aa0b5e3a..ecd0b1930e 100644--- a/packages/sdk/src/types/generated/Variant.ts+++ b/packages/sdk/src/types/generated/Variant.ts@@ -1,4 +1,4 @@-// Typelizer digest 2bd581f6f63d82cc74c3672e101d675a+// Typelizer digest 16e6bd684676c71f96f049dc4b2c82a3//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {Price, PriceHistory, Seller, Media, OptionValue, CustomField} from '@/types'@@ -20,6 +20,10 @@ interface Variant {height: number | null;width: number | null;depth: number | null;+ minimum_order_quantity: number;+ order_multiple: number;+ purchase_unit: string;+ units_per_carton: number | null;price: Price;original_price: Price | null;seller_id: string | null;diff --git a/packages/sdk/src/zod/generated/Cart.ts b/packages/sdk/src/zod/generated/Cart.tsindex e80bf17f4e..3f2d687ab4 100644--- a/packages/sdk/src/zod/generated/Cart.ts+++ b/packages/sdk/src/zod/generated/Cart.ts@@ -58,6 +58,9 @@ export const CartSchema = z.object({current_step: z.string(),completed_steps: z.array(z.string()),requirements: z.array(z.object({ step: z.string(), field: z.string(), code: z.string(), message: z.string() })),+ order_minimum: z.number().nullable(),+ order_minimum_shortfall: z.number().nullable(),+ below_order_minimum: z.boolean(),shipping_eq_billing_address: z.boolean(),discounts: z.array(AppliedPromotionSchema),fees: z.array(FeeSchema),diff --git a/packages/sdk/src/zod/generated/Variant.ts b/packages/sdk/src/zod/generated/Variant.tsindex f231311b75..3ac45c22db 100644--- a/packages/sdk/src/zod/generated/Variant.ts+++ b/packages/sdk/src/zod/generated/Variant.ts@@ -24,6 +24,10 @@ export const VariantSchema = z.object({height: z.number().nullable(),width: z.number().nullable(),depth: z.number().nullable(),+ minimum_order_quantity: z.number(),+ order_multiple: z.number(),+ purchase_unit: z.string(),+ units_per_carton: z.number().nullable(),price: PriceSchema,original_price: PriceSchema.nullable(),seller_id: z.string().nullable(),diff --git a/packages/seller-sdk/src/types/generated/Variant.ts b/packages/seller-sdk/src/types/generated/Variant.tsindex 6c2313557a..9f40cd18b3 100644--- a/packages/seller-sdk/src/types/generated/Variant.ts+++ b/packages/seller-sdk/src/types/generated/Variant.ts@@ -1,4 +1,4 @@-// Typelizer digest 7e8689f5d3ff50caf13cd78271a24d67+// Typelizer digest 824e6b2a5d6b5917c16067f3ab7839b4//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {Price, PriceHistory, Seller, Media, OptionValue, CustomField, StockLevel} from '@/types'@@ -20,6 +20,10 @@ type Variant = {height: number | null;width: number | null;depth: number | null;+ minimum_order_quantity: number | null;+ order_multiple: number | null;+ purchase_unit: string | null;+ units_per_carton: number | null;price: Price;original_price: Price | null;seller_id: string | null;diff --git a/spree/api/app/controllers/spree/api/v3/admin/catalogs/base_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/catalogs/base_controller.rbnew file mode 100644index 0000000000..3c34cf95ea--- /dev/null+++ b/spree/api/app/controllers/spree/api/v3/admin/catalogs/base_controller.rb@@ -0,0 +1,29 @@+module Spree+ module Api+ module V3+ module Admin+ module Catalogs+ # Anything nested under a catalog. The parent resolves through the+ # store, so a catalog belonging to another tenant is a 404 rather+ # than a readable record.+ class BaseController < ResourceController+ scoped_resource :products++ protected++ def set_parent+ @parent = current_store.catalogs.+ accessible_by(current_ability, parent_ability_action).+ find_by_prefix_id!(params[:catalog_id])+ @catalog = @parent+ end++ def authorize_parent_access!+ authorize_parent!(@parent)+ end+ end+ end+ end+ end+ end+enddiff --git a/spree/api/app/controllers/spree/api/v3/admin/catalogs/order_minimums_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/catalogs/order_minimums_controller.rbnew file mode 100644index 0000000000..cf8ff96002--- /dev/null+++ b/spree/api/app/controllers/spree/api/v3/admin/catalogs/order_minimums_controller.rb@@ -0,0 +1,38 @@+module Spree+ module Api+ module V3+ module Admin+ module Catalogs+ # The least a whole order must come to under a catalog's agreement.+ # One row per currency — never one amount with a currency beside+ # it, because Spree holds no exchange rates.+ class OrderMinimumsController < BaseController+ before_action :authorize_parent_access!++ protected++ def model_class+ Spree::CatalogOrderMinimum+ end++ def serializer_class+ Spree.api.admin_catalog_order_minimum_serializer+ end++ def scope+ @parent.order_minimums+ end++ def parent_association+ :order_minimums+ end++ def permitted_params+ params.permit(:currency, :amount)+ end+ end+ end+ end+ end+ end+enddiff --git a/spree/api/app/controllers/spree/api/v3/admin/catalogs/quantity_rules_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/catalogs/quantity_rules_controller.rbnew file mode 100644index 0000000000..4b7e7136e6--- /dev/null+++ b/spree/api/app/controllers/spree/api/v3/admin/catalogs/quantity_rules_controller.rb@@ -0,0 +1,52 @@+module Spree+ module Api+ module V3+ module Admin+ module Catalogs+ # A catalog's per-variant quantity terms — the narrowest of the+ # three levels a buyer's rules resolve through. The catalog-wide+ # default is a pair of fields on the catalog itself, so this+ # surface is strictly the overrides.+ class QuantityRulesController < BaseController+ before_action :authorize_parent_access!++ protected++ def model_class+ Spree::CatalogQuantityRule+ end++ def serializer_class+ Spree.api.admin_catalog_quantity_rule_serializer+ end++ def scope+ @parent.quantity_rules+ end++ def parent_association+ :quantity_rules+ end++ def collection_includes+ [variant: :product]+ end++ # The variant resolves through this store's own products, so an+ # id belonging to another tenant is a 404 rather than a term+ # written against a variant the merchant does not sell.+ def permitted_params+ permitted = params.permit(:minimum_order_quantity, :order_multiple, :variant_id)++ if permitted.key?(:variant_id)+ permitted[:variant_id] = current_store.variants.find_by_prefix_id!(permitted[:variant_id]).id+ end++ permitted+ end+ end+ end+ end+ end+ end+enddiff --git a/spree/api/app/controllers/spree/api/v3/admin/catalogs_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/catalogs_controller.rbindex 7ed1c4babc..639f5b013a 100644--- a/spree/api/app/controllers/spree/api/v3/admin/catalogs_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/catalogs_controller.rb@@ -58,7 +58,7 @@ module Spreeenddef collection_includes- [:catalog_products, :price_list]+ [:catalog_products, :price_list, :order_minimums]enddef create_workflow@@ -77,6 +77,7 @@ module Spreedef permitted_paramspermitted = params.permit(*model_additional_permitted_attributes,:name, :description, :active, :position, :price_list_id,+ :minimum_order_quantity, :order_multiple,metadata: {},price_list: [:name, :description, :status, :match_policy,diff --git a/spree/api/app/controllers/spree/api/v3/admin/products/variants_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products/variants_controller.rbindex f941c1c744..1a619815b3 100644--- a/spree/api/app/controllers/spree/api/v3/admin/products/variants_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/products/variants_controller.rb@@ -52,6 +52,7 @@ module Spree:cost_price, :cost_currency,:weight, :height, :width, :depth, :weight_unit, :dimensions_unit,:hs_code, :country_of_origin, :customs_description,+ :minimum_order_quantity, :order_multiple, :purchase_unit, :units_per_carton,:seller_id, :delivery_profile_id,:track_inventory, :preorderable, :preorder_ships_at, :backorder_limit, :tax_category_id, :position,options: [:name, :value],diff --git a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rbindex 80d2dffd7e..b1296fd73b 100644--- a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb@@ -262,6 +262,7 @@ module Spree:cost_price, :cost_currency,:weight, :height, :width, :depth, :weight_unit, :dimensions_unit,:hs_code, :country_of_origin, :customs_description,+ :minimum_order_quantity, :order_multiple, :purchase_unit, :units_per_carton,:seller_id, :delivery_profile_id,:track_inventory, :preorderable, :preorder_ships_at, :backorder_limit, :tax_category_id, :position,options: [:name, :value],diff --git a/spree/api/app/controllers/spree/api/v3/seller/products_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/products_controller.rbindex 4f96041d7f..ceb3e351b5 100644--- a/spree/api/app/controllers/spree/api/v3/seller/products_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/seller/products_controller.rb@@ -179,6 +179,7 @@ module Spree:cost_price, :cost_currency,:weight, :height, :width, :depth, :weight_unit, :dimensions_unit,:hs_code, :country_of_origin, :customs_description,+ :minimum_order_quantity, :order_multiple, :purchase_unit, :units_per_carton,:track_inventory, :preorderable, :preorder_ships_at, :backorder_limit, :position,options: [:name, :value],prices: [:amount, :compare_at_amount, :currency],diff --git a/spree/api/app/serializers/spree/api/v3/admin/catalog_order_minimum_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/catalog_order_minimum_serializer.rbnew file mode 100644index 0000000000..1ed4329aa0--- /dev/null+++ b/spree/api/app/serializers/spree/api/v3/admin/catalog_order_minimum_serializer.rb@@ -0,0 +1,23 @@+module Spree+ module Api+ module V3+ module Admin+ # The least a whole order must come to under a catalog's agreement,+ # in one currency.+ class CatalogOrderMinimumSerializer < V3::BaseSerializer+ typelize currency: :string, amount: :string, display_amount: :string++ attributes :currency, created_at: :iso8601, updated_at: :iso8601++ attribute :amount do |minimum|+ minimum.amount.to_s+ end++ attribute :display_amount do |minimum|+ minimum.display_amount.to_s+ end+ end+ end+ end+ end+enddiff --git a/spree/api/app/serializers/spree/api/v3/admin/catalog_quantity_rule_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/catalog_quantity_rule_serializer.rbnew file mode 100644index 0000000000..8496f89b2f--- /dev/null+++ b/spree/api/app/serializers/spree/api/v3/admin/catalog_quantity_rule_serializer.rb@@ -0,0 +1,37 @@+module Spree+ module Api+ module V3+ module Admin+ # One catalog's quantity terms for one variant. Carries enough of the+ # variant to render the row without a second request — a merchant+ # reading a terms table needs to know which SKU each line is about.+ class CatalogQuantityRuleSerializer < V3::BaseSerializer+ typelize variant_id: :string, variant_sku: 'string | null',+ product_name: 'string | null',+ options_text: 'string | null',+ minimum_order_quantity: ['number | null'],+ order_multiple: ['number | null']++ attributes :minimum_order_quantity, :order_multiple,+ created_at: :iso8601, updated_at: :iso8601++ attribute :variant_id do |rule|+ rule.variant&.prefixed_id+ end++ attribute :variant_sku do |rule|+ rule.variant&.sku+ end++ attribute :product_name do |rule|+ rule.variant&.name+ end++ attribute :options_text do |rule|+ rule.variant&.options_text.presence+ end+ end+ end+ end+ end+enddiff --git a/spree/api/app/serializers/spree/api/v3/admin/catalog_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/catalog_serializer.rbindex dec367b259..442944bea1 100644--- a/spree/api/app/serializers/spree/api/v3/admin/catalog_serializer.rb+++ b/spree/api/app/serializers/spree/api/v3/admin/catalog_serializer.rb@@ -9,9 +9,12 @@ module Spreeactive: :boolean, position: [:number, nullable: true],price_list_id: [:string, nullable: true],products_count: :number,+ minimum_order_quantity: ['number | null'], order_multiple: ['number | null'],+ quantity_rules_count: :number, order_minimums_count: :number,metadata: 'Record<string, unknown> | null'attributes :name, :description, :active, :position, :metadata,+ :minimum_order_quantity, :order_multiple,created_at: :iso8601, updated_at: :iso8601attribute :price_list_id do |catalog|@@ -22,6 +25,22 @@ module Spreecatalog.catalog_products.sizeend+ # Counts rather than the rows themselves: a catalog can carry+ # thousands of overrides, and the terms card pages through them.+ attribute :quantity_rules_count do |catalog|+ catalog.quantity_rules.size+ end++ attribute :order_minimums_count do |catalog|+ catalog.order_minimums.size+ end++ # Minimums are few (one per currency) and the card renders them+ # inline, so they ride along rather than costing a second request.+ many :order_minimums,+ resource: proc { Spree.api.admin_catalog_order_minimum_serializer },+ if: proc { expand?('order_minimums') }+# The owned list rides along so the agreement editor can render its# pricing without a second request — the catalog page is where an# owned list is edited (docs/plans/6.0-catalog-agreement-rework.md).diff --git a/spree/api/app/serializers/spree/api/v3/admin/variant_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/variant_serializer.rbindex 1176c6d30e..50c1410067 100644--- a/spree/api/app/serializers/spree/api/v3/admin/variant_serializer.rb+++ b/spree/api/app/serializers/spree/api/v3/admin/variant_serializer.rb@@ -22,6 +22,8 @@ module Spreecustoms_description: [:string, nullable: true],deleted_at: [:string, nullable: true],delivery_profile_id: [:string, nullable: true],+ minimum_order_quantity: ['number | null'], order_multiple: ['number | null'],+ purchase_unit: [:string, nullable: true],metadata: 'Record<string, unknown>'attributes :metadata, :position, :cost_price, :cost_currency,@@ -30,6 +32,21 @@ module Spreepreorder_ships_at: :iso8601, deleted_at: :iso8601,created_at: :iso8601, updated_at: :iso8601+ # The variant's OWN rules, overriding the store serializer's+ # buyer-resolved ones: a merchant edits what is stored on the row,+ # not what some catalog resolves to for a buyer they are not.+ attribute :minimum_order_quantity do |variant|+ variant.minimum_order_quantity+ end++ attribute :order_multiple do |variant|+ variant.order_multiple+ end++ attribute :purchase_unit do |variant|+ variant.purchase_unit+ end+attribute :preorderable do |variant|variant.preorderable?enddiff --git a/spree/api/app/serializers/spree/api/v3/base_serializer.rb b/spree/api/app/serializers/spree/api/v3/base_serializer.rbindex e78a923aff..f02206699d 100644--- a/spree/api/app/serializers/spree/api/v3/base_serializer.rb+++ b/spree/api/app/serializers/spree/api/v3/base_serializer.rb@@ -109,6 +109,24 @@ module Spree)end+ # The buyer's effective purchasing rules for a variant — their+ # catalogs' commercial terms resolved over the variant's own base+ # rules. Read from the request context the same way price_for does,+ # so a storefront gets the stepper matching the price it is quoted.+ #+ # One resolver per render rather than one per variant: a listing page+ # asks about every row against the same few catalogs.+ def quantity_rule_for(variant)+ @quantity_rule_cache ||= {}+ @quantity_rule_cache[variant.id] ||= quantity_rules_resolver.call(variant)+ end++ def quantity_rules_resolver+ @quantity_rules_resolver ||= Spree::Catalogs::ResolveQuantityRules.new(+ Spree::Current.catalogs_for(user: current_user)+ )+ end+# Returns the base price for a variant without Price List resolution# This is the "original" price before any price list discounts# Memoized per variant to avoid duplicate queriesdiff --git a/spree/api/app/serializers/spree/api/v3/cart_serializer.rb b/spree/api/app/serializers/spree/api/v3/cart_serializer.rbindex 7cf175e82b..b6535a7156 100644--- a/spree/api/app/serializers/spree/api/v3/cart_serializer.rb+++ b/spree/api/app/serializers/spree/api/v3/cart_serializer.rb@@ -13,6 +13,8 @@ module Spreepo_number: [:string, nullable: true], po_number_required: :boolean,po_document_filename: [:string, nullable: true],po_document_byte_size: ['number | null'],+ order_minimum: ['number | null'], order_minimum_shortfall: ['number | null'],+ below_order_minimum: :boolean,requirements: 'Array<{step: string, field: string, code: string, message: string}>',item_total: [:string, nullable: true], display_item_total: [:string, nullable: true],delivery_total: [:string, nullable: true], display_delivery_total: [:string, nullable: true],@@ -114,6 +116,22 @@ module SpreeSpree::Checkout::Requirements.new(order).callend+ # The order minimum in force for this buyer, or nil when their+ # agreements state none. Exposed alongside the shortfall so a+ # storefront can render "$180 to reach the $500 minimum" without+ # doing the arithmetic or knowing where the number came from.+ attribute :order_minimum do |order|+ order.order_minimum_amount&.to_f+ end++ attribute :order_minimum_shortfall do |order|+ order.order_minimum_shortfall&.to_f+ end++ attribute :below_order_minimum do |order|+ order.below_order_minimum?+ end+attribute :shipping_eq_billing_address do |order|order.shipping_eq_billing_address?enddiff --git a/spree/api/app/serializers/spree/api/v3/seller/variant_serializer.rb b/spree/api/app/serializers/spree/api/v3/seller/variant_serializer.rbindex 73099d4e7e..4451e88d7d 100644--- a/spree/api/app/serializers/spree/api/v3/seller/variant_serializer.rb+++ b/spree/api/app/serializers/spree/api/v3/seller/variant_serializer.rb@@ -24,6 +24,8 @@ module Spreehs_code: [:string, nullable: true],country_of_origin: [:string, nullable: true],customs_description: [:string, nullable: true],+ minimum_order_quantity: ['number | null'], order_multiple: ['number | null'],+ purchase_unit: [:string, nullable: true],metadata: 'Record<string, unknown>'attributes :metadata, :position, :cost_price, :cost_currency,@@ -32,6 +34,12 @@ module Spreepreorder_ships_at: :iso8601,created_at: :iso8601, updated_at: :iso8601+ # The variant's own stored rules, not a buyer's resolved ones — a+ # seller edits the row.+ attribute :minimum_order_quantity, &:minimum_order_quantity+ attribute :order_multiple, &:order_multiple+ attribute :purchase_unit, &:purchase_unit+attribute :preorderable, &:preorderable?attribute :available_stock do |variant|diff --git a/spree/api/app/serializers/spree/api/v3/variant_serializer.rb b/spree/api/app/serializers/spree/api/v3/variant_serializer.rbindex 1b39df7176..51e23dec4a 100644--- a/spree/api/app/serializers/spree/api/v3/variant_serializer.rb+++ b/spree/api/app/serializers/spree/api/v3/variant_serializer.rb@@ -12,6 +12,8 @@ module Spreeweight: [:number, nullable: true], height: [:number, nullable: true], width: [:number, nullable: true], depth: [:number, nullable: true],price: 'Price',original_price: ['Price', nullable: true],+ minimum_order_quantity: :number, order_multiple: :number,+ purchase_unit: :string, units_per_carton: ['number | null'],seller_id: [:string, nullable: true]attribute :product_id do |variant|@@ -63,6 +65,28 @@ module Spreevariant.depth&.to_fend+ # The buyer's RESOLVED rules — their catalog terms over the variant's+ # base — so a storefront draws the right stepper without knowing+ # which of the two answered. Always present: an unrestricted variant+ # reads 1 and 1, which is a valid stepper rather than a special case.+ attribute :minimum_order_quantity do |variant|+ quantity_rule_for(variant).minimum+ end++ attribute :order_multiple do |variant|+ quantity_rule_for(variant).multiple+ end++ # What the buyer is quoted in. Stored quantities stay units at every+ # level; this only says how to present them.+ attribute :purchase_unit do |variant|+ variant.purchase_unit.presence || 'unit'+ end++ attribute :units_per_carton do |variant|+ variant.units_per_carton+ end+# Price object - calculated price with price list resolutionattribute :price do |variant|price = price_for(variant)diff --git a/spree/api/config/routes.rb b/spree/api/config/routes.rbindex 842d55e561..96934872ab 100644--- a/spree/api/config/routes.rb+++ b/spree/api/config/routes.rb@@ -606,6 +606,11 @@ Spree::Core::Engine.add_routes do# Assortment membership. Unordered: a catalog decides what a buyer# sees, never the order they see it in.concerns :product_membership, controller: 'catalogs/products'++ # Commercial terms. Typed per grain rather than one rules endpoint:+ # quantity rules are per variant, minimums are per currency.+ resources :quantity_rules, controller: 'catalogs/quantity_rules'+ resources :order_minimums, controller: 'catalogs/order_minimums'endresources :catalog_assignments, only: [:show, :destroy]diff --git a/spree/api/lib/spree/api/dependencies.rb b/spree/api/lib/spree/api/dependencies.rbindex 6bcc748d2f..af39b97c26 100644--- a/spree/api/lib/spree/api/dependencies.rb+++ b/spree/api/lib/spree/api/dependencies.rb@@ -158,6 +158,8 @@ module Spreeadmin_company_invitation_serializer: 'Spree::Api::V3::Admin::CompanyInvitationSerializer',admin_catalog_serializer: 'Spree::Api::V3::Admin::CatalogSerializer',admin_catalog_assignment_serializer: 'Spree::Api::V3::Admin::CatalogAssignmentSerializer',+ admin_catalog_quantity_rule_serializer: 'Spree::Api::V3::Admin::CatalogQuantityRuleSerializer',+ admin_catalog_order_minimum_serializer: 'Spree::Api::V3::Admin::CatalogOrderMinimumSerializer',admin_tax_exemption_certificate_serializer: 'Spree::Api::V3::Admin::TaxExemptionCertificateSerializer',admin_admin_user_serializer: 'Spree::Api::V3::Admin::AdminUserSerializer',admin_seller_team_member_serializer: 'Spree::Api::V3::Admin::SellerTeamMemberSerializer',diff --git a/spree/api/spec/controllers/spree/api/v3/admin/catalogs/order_minimums_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/catalogs/order_minimums_controller_spec.rbnew file mode 100644index 0000000000..c208054d5f--- /dev/null+++ b/spree/api/spec/controllers/spree/api/v3/admin/catalogs/order_minimums_controller_spec.rb@@ -0,0 +1,62 @@+require 'spec_helper'++RSpec.describe Spree::Api::V3::Admin::Catalogs::OrderMinimumsController, type: :controller do+ render_views++ include_context 'API v3 Admin authenticated'++ let!(:catalog) { create(:catalog, store: store) }++ before { request.headers.merge!(headers) }++ describe 'GET #index' do+ it 'lists the minimums with their formatted amount' do+ create(:catalog_order_minimum, catalog: catalog, currency: 'USD', amount: 500)++ get :index, params: { catalog_id: catalog.prefixed_id }, as: :json++ expect(response).to have_http_status(:ok)+ row = json_response['data'].first+ expect(row['currency']).to eq('USD')+ expect(row['amount']).to eq('500.0')+ expect(row['display_amount']).to include('500')+ end+ end++ describe 'POST #create' do+ it 'records a minimum for a currency' do+ post :create, params: { catalog_id: catalog.prefixed_id, currency: 'EUR', amount: '450.00' }, as: :json++ expect(response).to have_http_status(:created)+ expect(catalog.order_minimums.reload.first.currency).to eq('EUR')+ end++ it 'refuses a second row for the same currency' do+ create(:catalog_order_minimum, catalog: catalog, currency: 'USD')++ post :create, params: { catalog_id: catalog.prefixed_id, currency: 'USD', amount: '100' }, as: :json++ expect(response).to have_http_status(:unprocessable_content)+ end+ end++ describe 'DELETE #destroy' do+ it 'lifts the minimum' do+ minimum = create(:catalog_order_minimum, catalog: catalog)++ delete :destroy, params: { catalog_id: catalog.prefixed_id, id: minimum.prefixed_id }, as: :json++ expect(catalog.order_minimums.reload).to be_empty+ end+ end++ describe 'a catalog in another store' do+ it 'is not found' do+ foreign = create(:catalog, store: create(:store))++ get :index, params: { catalog_id: foreign.prefixed_id }, as: :json++ expect(response).to have_http_status(:not_found)+ end+ end+enddiff --git a/spree/api/spec/controllers/spree/api/v3/admin/catalogs/quantity_rules_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/catalogs/quantity_rules_controller_spec.rbnew file mode 100644index 0000000000..d70c3e6f29--- /dev/null+++ b/spree/api/spec/controllers/spree/api/v3/admin/catalogs/quantity_rules_controller_spec.rb@@ -0,0 +1,98 @@+require 'spec_helper'++RSpec.describe Spree::Api::V3::Admin::Catalogs::QuantityRulesController, type: :controller do+ render_views++ include_context 'API v3 Admin authenticated'++ let!(:catalog) { create(:catalog, store: store) }+ let(:variant) { create(:variant, product: create(:product, store: store)) }++ before { request.headers.merge!(headers) }++ describe 'GET #index' do+ it "lists the catalog's overrides with the variant they name" do+ rule = create(:catalog_quantity_rule, catalog: catalog, variant: variant)++ get :index, params: { catalog_id: catalog.prefixed_id }, as: :json++ expect(response).to have_http_status(:ok)+ row = json_response['data'].first+ expect(row['id']).to eq(rule.prefixed_id)+ expect(row['variant_id']).to eq(variant.prefixed_id)+ expect(row['minimum_order_quantity']).to eq(48)+ end++ it "does not leak another catalog's rules" do+ other = create(:catalog, store: store)+ create(:catalog_quantity_rule, catalog: other, variant: variant)++ get :index, params: { catalog_id: catalog.prefixed_id }, as: :json++ expect(json_response['data']).to be_empty+ end+ end++ describe 'POST #create' do+ it 'records an override' do+ post :create, params: {+ catalog_id: catalog.prefixed_id,+ variant_id: variant.prefixed_id,+ minimum_order_quantity: 480,+ order_multiple: 240+ }, as: :json++ expect(response).to have_http_status(:created)+ expect(catalog.quantity_rules.reload.first.minimum_order_quantity).to eq(480)+ end++ it 'refuses a row stating neither field' do+ post :create, params: { catalog_id: catalog.prefixed_id, variant_id: variant.prefixed_id }, as: :json++ expect(response).to have_http_status(:unprocessable_content)+ end++ # A variant belonging to another tenant must be unreachable, not merely+ # rejected by a validation.+ it 'is not found for a variant from another store' do+ foreign = create(:variant, product: create(:product, store: create(:store)))++ post :create, params: {+ catalog_id: catalog.prefixed_id, variant_id: foreign.prefixed_id, minimum_order_quantity: 12+ }, as: :json++ expect(response).to have_http_status(:not_found)+ end+ end++ describe 'PATCH #update' do+ it 'changes the stated terms' do+ rule = create(:catalog_quantity_rule, catalog: catalog, variant: variant)++ patch :update, params: { catalog_id: catalog.prefixed_id, id: rule.prefixed_id, order_multiple: 12 }, as: :json++ expect(response).to have_http_status(:ok)+ expect(rule.reload.order_multiple).to eq(12)+ end+ end++ describe 'DELETE #destroy' do+ it 'removes the override' do+ rule = create(:catalog_quantity_rule, catalog: catalog, variant: variant)++ delete :destroy, params: { catalog_id: catalog.prefixed_id, id: rule.prefixed_id }, as: :json++ expect(catalog.quantity_rules.reload).to be_empty+ end+ end++ describe 'a catalog in another store' do+ it 'is not found' do+ foreign = create(:catalog, store: create(:store))++ get :index, params: { catalog_id: foreign.prefixed_id }, as: :json++ expect(response).to have_http_status(:not_found)+ end+ end+end