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
Fix: a buyer can order products their catalogs hide (V-3722) (#14787)
Łukasz Adamczyk3fe9c0d61d8c
diff --git a/docs/developer/core-concepts/carts.mdx b/docs/developer/core-concepts/carts.mdxindex 5af59907e6..34ed9cfd4a 100644--- a/docs/developer/core-concepts/carts.mdx+++ b/docs/developer/core-concepts/carts.mdx@@ -158,7 +158,8 @@ Seven checks run on every cart read:**An empty `requirements` array does not guarantee completion will succeed.** What acart reports is the *advisory* set. Completing one additionally checks stock(`out_of_stock`, `discontinued`), negotiated quantity rules- (`quantity_rule_violated`) and the guest-checkout policy+ (`quantity_rule_violated`), whether each product is still in the buyer's+ [catalogs](/developer/core-concepts/catalogs) (`not_orderable`) and the guest-checkout policy(`guest_checkout_not_allowed`) — each of which has to load line items, so they areheld back to keep reading a cart cheap. Treat a failed completion as a normal path,not an exception.diff --git a/docs/developer/core-concepts/catalogs.mdx b/docs/developer/core-concepts/catalogs.mdxindex 0468bc551f..5345677ab9 100644--- a/docs/developer/core-concepts/catalogs.mdx+++ b/docs/developer/core-concepts/catalogs.mdx@@ -182,6 +182,8 @@ The three ways of reaching a buyer are tried in turn, and the first one that finGated storefront access is checked before any of this — a shopper who has to sign in never reaches catalog resolution.+What a shopper can see is also what they can buy. Adding a product outside their catalogs, or one not published on the cart's channel, answers `404` — the same as reading that product. A cart can still come to hold one without an add: a guest cart claimed after signing in, a cart switched to another company, or a catalog edited while the cart was open. Completing checkout checks every line again and refuses such a cart with a `not_orderable` [requirement](/developer/core-concepts/carts) naming the product, until the shopper removes it. Free items a promotion adds are no exception, so keep gift products inside the catalogs of the buyers the promotion targets. Staff keying in a draft order are not restricted.+<Warning>**A company buyer never picks up their customer group's catalogs.** As soon as any catalog is assigned to their company or one above it, that is their agreement, and group assignments are not consulted for them.diff --git a/docs/plans/6.0-b2b-companies-and-catalogs.md b/docs/plans/6.0-b2b-companies-and-catalogs.mdindex 137e041179..b7e534c3bc 100644--- a/docs/plans/6.0-b2b-companies-and-catalogs.md+++ b/docs/plans/6.0-b2b-companies-and-catalogs.md@@ -407,6 +407,12 @@ amendments:alternatives, not layers — a buyer whose company subtree carries anycatalog is never also served their customer group's, and trade tiers overa company tree are expressed as company assignments (see decisions.md).+ **Amended 2026-10-01 (V-3722):** visibility is also what a buyer may buy.+ Storefront cart adds resolve variants through `Spree::Cart#orderable_variants`+ (this resolver, asked about the cart's own customer, resolved company and+ channel), so a hidden or unpublished product answers 404 like its product+ read; completion re-checks every line with a `not_orderable` requirement,+ gifts included. Staff draft orders are exempt.- **Pricing**: the resolver checks the effective catalogs' PriceLists in**nearest-node-first** order (the purchase node's own assignments beforeits parent's), then the existing PriceRule matching, then base price.@@ -606,6 +612,11 @@ Phases (each lands green on its own):role capabilities, payment terms do not get columns here — their plans livein `ee/docs/plans/` and consume hooks/slots/policies (themodel-not-the-feature rule, decisions.md 2026-08-07).+- **A variant a buyer names resolves through what that buyer may see,**+ never `current_store.variants`. Product reads and wishlist items narrow+ through the Store API's `StorefrontProducts`; carts through+ `Spree::Cart#orderable_variants`. A new storefront endpoint accepting a+ variant or product id picks one of the two (V-3722).- **Catalog and company lookups are store-scoped** like every controllerlookup (`current_store.companies`, `current_store.catalogs`) includingincidental resolutions (`parent_id`, `assignable_id`, `price_list_id`).diff --git a/docs/plans/decisions.md b/docs/plans/decisions.mdindex 12c7a45c4b..f431806b1d 100644--- a/docs/plans/decisions.md+++ b/docs/plans/decisions.md@@ -1,3 +1,13 @@+## 2026-10-01: A buyer can buy only what their catalogs show them (V-3722)++**Context:** Catalogs narrowed the product listing and product reads, but the cart looked variants up across the whole store. A company buyer whose catalog hid a product could add it by sending its variant id, and check out with it; the same lookup let a shopper add a product not published on their channel. A product read answered 404 for both. Blocking adds alone would not close it: a guest cart claimed after sign-in, a cart switched to another company, or a catalog edited while a cart is open all leave a hidden line in the cart without any add.++**Decision:** Visibility is purchasability. The storefront's three item-writing paths (add item, and creating or updating a cart with `items`) resolve variants through `Spree::Cart#orderable_variants` — `Products::ForContext` asked about the cart's own customer, resolved company and channel — and answer 404 for anything outside it. Cart updates resolve after the cart's own attributes are saved, so a company named in the same request decides the catalogs. Completion re-checks every line and refuses a hidden one with a `not_orderable` requirement naming it, rather than silently removing it on read. A promotion's free gift gets no exception: merchants keep gift products inside the catalogs of the buyers they target. Staff draft orders are exempt, as they are from quantity terms, and the admin order workflows are untouched. A visible product with no price in the cart's currency keeps its 422 explanation, so the narrowing deliberately leaves out the listing's currency filter. Of the publication window it applies only the end: a publication whose `unpublished_at` has passed is the product's discontinuation on that channel, so the add answers 404 and checkout keeps its existing `discontinued` requirement for a line already in the cart, without a second `not_orderable` for it. The start is left out because the listing admits a product scheduled to publish later when it can be pre-ordered.++**Consequences:** `Carts::UpsertItems` takes an optional `orderable_variants:` relation; an app that swapped `cart_upsert_items_workflow` for a class not accepting it must add the keyword. A cart line for a product unpublished from the channel now blocks checkout until removed. A line already in the cart is a quantity edit, not an add, on both the item endpoint and a cart update: it is judged at completion, so one stale line cannot fail every other change a cart update carries. Not covered: a product whose channel publication starts in the future and cannot be pre-ordered can still be added, and the hidden line is only reported at completion — the cart's advisory requirements do not list it, the same trade quantity rules make. A buyer in several companies may still see a listing resolved without the cart's company while the cart checks against it; that gap is the listing's, and predates this.++**Plans amended:** `6.0-b2b-companies-and-catalogs.md` (Visibility, and a Constraints on Current Work bullet).+## 2026-10-01: No delivery is offered before the address, and the storefront never charges a delivery it has not shown**Context:** The storefront charged a delivery method and total the buyer was never shown (V-3718). Reproduced on `main`: a signed-in buyer's checkout read Store Pickup $0.00, total $89.98; pressing Place Order booked order R1001 as Standard $5.00, $94.98, and the Check payment completed for $94.98. Two things combined. Every item change rebuilt the cart's delivery proposals whether or not it had a destination, and quoted without one, only pickup qualifies, so the estimator preselected it. That contradicted the 2026-08-02 rule that proposals wait for an address or a pickup intent, which only `Checkout::Advance` applied. The preselected pickup also made `shipping_address_required?` answer false, so a cart whose email had saved but whose address had not could complete as pickup without the buyer choosing it. Then pressing Place Order moved focus out of the address block, which saved the address. That rebuilt the proposals for the new destination and picked Standard, as the 2026-09-24 entry intends. Next.js runs a client's server actions one at a time, so the Place Order handler always re-read the cart after that save, and then paid without comparing it with what was on screen.@@ -10,6 +20,7 @@ Rejected: sending the client's `expected_total` with completion (the `Carts::Com**Plans amended:** `6.0-fulfillment-and-delivery.md` (Rebuild triggers).+## 2026-09-30: Merchants edit email templates in the dashboard, as drafts published per store**Context:** Since the Liquid and MJML plan, every email renders from a template only a developer can change. The end goal was always merchant editing; this settles its shape.diff --git a/spree/api/app/controllers/concerns/spree/api/v3/store/storefront_products.rb b/spree/api/app/controllers/concerns/spree/api/v3/store/storefront_products.rbindex 2c02a7432f..ddc8163022 100644--- a/spree/api/app/controllers/concerns/spree/api/v3/store/storefront_products.rb+++ b/spree/api/app/controllers/concerns/spree/api/v3/store/storefront_products.rb@@ -5,9 +5,10 @@ module Spree# The products a storefront buyer may see: available now (or on# pre-order) in the current currency, narrowed to the catalogs their# company, customer group or channel resolve to. The product listing- # reads through it, and so does anything else that accepts a product- # or variant from the buyer, so an id cannot reach what the listing- # would never show.+ # and wishlist items read through it, so an id cannot reach what the+ # listing would never show. Carts narrow through+ # {Spree::Cart#orderable_variants} instead, which asks the same+ # catalogs about the cart's own buyer and company.module StorefrontProductsextend ActiveSupport::Concern@@ -26,7 +27,7 @@ module Spree# @return [ActiveRecord::Relation<Spree::Variant>]def storefront_variants- Spree::Variant.where(product_id: storefront_products.reorder(nil).select(Spree::Product.arel_table[:id]))+ Spree::Variant.for_products(storefront_products)endendenddiff --git a/spree/api/app/controllers/spree/api/v3/store/carts/items_controller.rb b/spree/api/app/controllers/spree/api/v3/store/carts/items_controller.rbindex 85eafddb8b..92bf1a4a7d 100644--- a/spree/api/app/controllers/spree/api/v3/store/carts/items_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/store/carts/items_controller.rb@@ -97,7 +97,7 @@ module Spreeprivatedef variant- @variant ||= current_store.variants.find_by_prefix_id!(permitted_params[:variant_id])+ @variant ||= @cart.orderable_variants.find_by_prefix_id!(permitted_params[:variant_id])end# Extension attributes ride in `options`, which is how AddItemdiff --git a/spree/api/spec/controllers/spree/api/v3/store/carts/items_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/carts/items_controller_spec.rbindex 231e0b704b..cdc53ac278 100644--- a/spree/api/spec/controllers/spree/api/v3/store/carts/items_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/store/carts/items_controller_spec.rb@@ -132,6 +132,40 @@ RSpec.describe Spree::Api::V3::Store::Carts::ItemsController, type: :controllerexpect(response).to have_http_status(:not_found)endend++ # The same answer a product read gives: a variant the listing would not+ # show this buyer reads as missing.+ context 'with a variant the buyer cannot order' do+ it "refuses a product outside the buyer's catalogs" do+ company = create(:company, store: store)+ catalog = create(:catalog, store: store)+ create(:catalog_product, catalog: catalog, product: create(:product, store: store))+ create(:catalog_assignment, catalog: catalog, assignable: company)+ create(:company_membership, company: company, customer: user)++ expect {+ post :create, params: { cart_id: order.prefixed_id, variant_id: variant.prefixed_id }+ }.not_to change(Spree::LineItem, :count)++ expect(response).to have_http_status(:not_found)+ end++ it "refuses a product not published on the cart's channel" do+ product.product_publications.destroy_all++ post :create, params: { cart_id: order.prefixed_id, variant_id: variant.prefixed_id }++ expect(response).to have_http_status(:not_found)+ end++ it "refuses a product already unpublished from the cart's channel" do+ product.product_publications.update_all(unpublished_at: 1.hour.ago)++ post :create, params: { cart_id: order.prefixed_id, variant_id: variant.prefixed_id }++ expect(response).to have_http_status(:not_found)+ end+ endenddescribe 'PATCH #update' dodiff --git a/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rbindex 89954545cd..6440bd9de2 100644--- a/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb@@ -289,6 +289,20 @@ RSpec.describe Spree::Api::V3::Store::CartsController, type: :controller doexpect(json_response['error']['code']).to eq('variant_not_found')expect(json_response['error']['message']).to eq('Variant not found')end++ it "refuses an item outside the buyer's catalogs" do+ company = create(:company, store: store)+ catalog = create(:catalog, store: store)+ create(:catalog_product, catalog: catalog, product: product2)+ create(:catalog_assignment, catalog: catalog, assignable: company)+ create(:company_membership, company: company, customer: user)+ request.headers['Authorization'] = "Bearer #{jwt_token}"++ post :create, params: { items: [{ variant_id: variant.prefixed_id, quantity: 1 }] }++ expect(response).to have_http_status(:not_found)+ expect(Spree::LineItem.where(variant: variant)).to be_empty+ endendcontext 'without API key' dodiff --git a/spree/core/app/models/concerns/spree/product_scopes.rb b/spree/core/app/models/concerns/spree/product_scopes.rbindex 2efb11c200..6d03699f15 100644--- a/spree/core/app/models/concerns/spree/product_scopes.rb+++ b/spree/core/app/models/concerns/spree/product_scopes.rb@@ -219,6 +219,15 @@ module Spreechannel = Spree::Current.channelreturn where(discontinue_on: [nil, Time.current.beginning_of_minute..]) unless channel+ not_discontinued_on(channel)+ end++ # Published on the channel and not yet unpublished from it — a channel+ # publication's end date is the product's discontinuation there.+ #+ # @param channel [Spree::Channel]+ # @return [ActiveRecord::Relation<Spree::Product>]+ def self.not_discontinued_on(channel)for_channel(channel).where(Spree::ProductPublication.table_name => { unpublished_at: [nil, Time.current.beginning_of_minute..] })enddiff --git a/spree/core/app/models/spree/cart.rb b/spree/core/app/models/spree/cart.rbindex 5571dc1a46..ec36c9e0ff 100644--- a/spree/core/app/models/spree/cart.rb+++ b/spree/core/app/models/spree/cart.rb@@ -275,6 +275,22 @@ module Spreereloadend+ # The variants this cart's buyer may order: the channel's live+ # publications, narrowed to the catalogs the cart's company, customer+ # group or channel resolve to. Status, stock and currency are left to the+ # add and checkout checks, which explain a refusal rather than reading as+ # not found.+ #+ # @return [ActiveRecord::Relation<Spree::Variant>]+ def orderable_variants+ products = Spree.products_for_context_service.call(+ store: store, channel: channel, customer: customer, company: resolved_company,+ base: store.products.not_discontinued_on(channel)+ ).value++ Spree::Variant.for_products(products)+ end+# Removes out-of-stock/discontinued items and populates warnings# (mirrors Order#remove_out_of_stock_items!).def remove_out_of_stock_items!diff --git a/spree/core/app/models/spree/variant.rb b/spree/core/app/models/spree/variant.rbindex c52a773056..07db524c82 100644--- a/spree/core/app/models/spree/variant.rb+++ b/spree/core/app/models/spree/variant.rb@@ -231,6 +231,10 @@ module Spreescope :not_deleted, -> { where("#{Spree::Variant.quoted_table_name}.deleted_at IS NULL") }+ # The variants of a product relation, as a subquery. Unordered, because a+ # listing's sort would otherwise land inside the IN.+ scope :for_products, ->(products) { where(product_id: products.reorder(nil).select(Spree::Product.arel_table[:id])) }+# Variants the shop sells in this currency. Base prices only: a price# list's rows are what one audience pays under an agreement, and a ladder# holds several rows for one variant, so a contract-only variant is notdiff --git a/spree/core/app/services/spree/carts/update.rb b/spree/core/app/services/spree/carts/update.rbindex 7afba40cf7..9e82c8fa3a 100644--- a/spree/core/app/services/spree/carts/update.rb+++ b/spree/core/app/services/spree/carts/update.rb@@ -187,7 +187,9 @@ module Spreedef process_itemsreturn unless params[:items].is_a?(Array)- result = Spree.cart_upsert_items_workflow.call(cart: cart, items: params[:items])+ # Resolved after the save, so a company named in the same request+ # decides which catalogs the items are checked against.+ result = Spree.cart_upsert_items_workflow.call(cart: cart, items: params[:items], orderable_variants: cart.orderable_variants)raise StandardError, result.error.to_s if result.failure?enddiff --git a/spree/core/app/workflows/spree/carts/upsert_items.rb b/spree/core/app/workflows/spree/carts/upsert_items.rbindex ed3038798a..e74890a5d0 100644--- a/spree/core/app/workflows/spree/carts/upsert_items.rb+++ b/spree/core/app/workflows/spree/carts/upsert_items.rb@@ -33,8 +33,10 @@ module Spree# amount negotiates the line (+price_source: 'manual'+), an explicit# nil reverts a negotiated line to catalog pricing. Admin surface# only (draft orders); refused once the order is placed.+ # @param orderable_variants [ActiveRecord::Relation<Spree::Variant>, nil]+ # what an add may name; the store's variants when nil# @return [Spree::ServiceModule::Result] value is the cart- def perform(cart:, items:)+ def perform(cart:, items:, orderable_variants: nil)super@warnings = []@@ -355,8 +357,15 @@ module Spreecart.line_items.detect { |line_item| line_item.variant_id.to_s == variant_id.to_s }&.variant ||Spree::Variant.with_deleted.find_by_param(variant_id)else- store.variants.find_by_param(variant_id) ||- raise(ActiveRecord::RecordNotFound.new("Variant '#{variant_id}' not found in this store", 'Spree::Variant', 'id', variant_id))+ variant = store.variants.find_by_param(variant_id)+ # +orderable_variants+ narrows what a request may add. A line already+ # in the cart is a quantity edit and resolves as before — checkout+ # judges it — so one stale line cannot fail a whole cart's update.+ if variant && orderable_variants && cart.line_items.none? { |line_item| line_item.variant_id == variant.id }+ variant = orderable_variants.find_by(id: variant.id)+ end++ variant || raise(ActiveRecord::RecordNotFound.new("Variant '#{variant_id}' not found in this store", 'Spree::Variant', 'id', variant_id))endenddiff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.ymlindex 2b10588618..bf6f4214b7 100644--- a/spree/core/config/locales/en.yml+++ b/spree/core/config/locales/en.yml@@ -786,6 +786,7 @@ en:delivery_unavailable: "%{li_name} cannot be delivered to the selected address"discontinued: "%{li_name} was removed because it was discontinued"invalid_price: Price must be a non-negative number+ not_orderable: "%{li_name} is not available to order. Remove it to continue."out_of_stock: "%{li_name} was removed because it was sold out"price_override_not_allowed: Prices cannot be changed on a placed order — use fees or discounts insteadpricing_unavailable: Prices could not be confirmed. Please try again.diff --git a/spree/core/lib/spree/checkout/default_requirements.rb b/spree/core/lib/spree/checkout/default_requirements.rbindex a925c6029a..443ab3efde 100644--- a/spree/core/lib/spree/checkout/default_requirements.rb+++ b/spree/core/lib/spree/checkout/default_requirements.rb@@ -40,7 +40,7 @@ module Spreeenddef completion_requirements- errors = stock_errors + quantity_rule_errors+ errors = stock_errors + quantity_rule_errors + assortment_errorserrors << req('address', 'email', Spree.t(:guest_checkout_not_allowed), code: 'guest_checkout_not_allowed') if @cart.guest_checkout_disallowed?errorsend@@ -55,6 +55,22 @@ module Spreeendend+ # Re-checked at completion because a line can outlive the add that+ # admitted it: a guest cart claimed after sign-in, a cart moved to+ # another company, an assortment edited since. An order arriving here is+ # staff's draft, which may sell outside the buyer's catalogs. A+ # discontinued line already has its own requirement.+ def assortment_errors+ return [] unless @cart.is_a?(Spree::Cart)++ line_items = completion_line_items.reject { |line_item| discontinued?(line_item) }+ orderable_ids = @cart.orderable_variants.where(id: line_items.map(&:variant_id)).pluck(:id).to_set++ line_items.reject { |line_item| orderable_ids.include?(line_item.variant_id) }.map do |line_item|+ req('cart', 'line_items', Spree.t('cart_line_item.not_orderable', li_name: line_item.name), code: 'not_orderable')+ end+ end+# Advisory rather than completion-only: a buyer has to be able to build# the order below the threshold and read how far off they are while# they can still add to it. Completion refuses it because the advisory@@ -77,8 +93,8 @@ module Spree# while an individual variant hit its discontinue_on date after it# entered the cart.def stock_errors- @cart.line_items.includes(variant: :product).filter_map do |line_item|- if line_item.variant.nil? || line_item.variant.discontinued? || line_item.variant.product.discontinued?+ completion_line_items.filter_map do |line_item|+ if discontinued?(line_item)req('cart', 'line_items', Spree.t('cart_line_item.discontinued', li_name: line_item.name), code: 'discontinued')elsif !line_item.sufficient_stock?req('cart', 'line_items', Spree.t('cart_line_item.out_of_stock', li_name: line_item.name), code: 'out_of_stock')@@ -86,6 +102,14 @@ module Spreeendend+ def discontinued?(line_item)+ line_item.variant.nil? || line_item.variant.discontinued? || line_item.variant.product.discontinued?+ end++ def completion_line_items+ @completion_line_items ||= @cart.line_items.includes(variant: :product).to_a+ end+def delivery_step_required?@cart.delivery_step_required?enddiff --git a/spree/core/spec/lib/spree/checkout/requirements_spec.rb b/spree/core/spec/lib/spree/checkout/requirements_spec.rbindex c15a13e303..a9ea45d7ff 100644--- a/spree/core/spec/lib/spree/checkout/requirements_spec.rb+++ b/spree/core/spec/lib/spree/checkout/requirements_spec.rb@@ -190,6 +190,54 @@ RSpec.describe Spree::Checkout::Requirements doendend+ describe 'assortment requirement' do+ let(:customer) { create(:user) }+ let(:company) { create(:company, store: store) }+ let(:catalog) { create(:catalog, store: store) }+ let(:hidden) { create(:product, store: store, name: 'Gravel Bike') }+ let(:cart) { create(:cart, store: store, customer: customer) }++ before do+ create(:catalog_product, catalog: catalog, product: create(:product, store: store))+ create(:catalog_assignment, catalog: catalog, assignable: company)+ create(:company_membership, company: company, customer: customer)+ end++ # A guest cart claimed after sign-in carries lines no add ever checked+ # against this buyer's catalogs.+ it "refuses a line outside the buyer's catalogs at completion" do+ create(:line_item, cart: cart, order: nil, variant: hidden.default_variant)++ expect(described_class.new(cart.reload).call(completion: true)).to include(+ a_hash_including(step: 'cart', field: 'line_items', code: 'not_orderable', message: include('Gravel Bike'))+ )+ end++ it 'accepts a line inside them' do+ create(:catalog_product, catalog: catalog, product: hidden)+ create(:line_item, cart: cart, order: nil, variant: hidden.default_variant)++ expect(described_class.new(cart.reload).call(completion: true)).not_to include(a_hash_including(code: 'not_orderable'))+ end++ it 'reports a discontinued line once' do+ hidden.update_column(:discontinue_on, 1.day.ago)+ create(:line_item, cart: cart, order: nil, variant: hidden.default_variant)++ codes = described_class.new(cart.reload).call(completion: true).map { |requirement| requirement[:code] }++ expect(codes).to include('discontinued')+ expect(codes).not_to include('not_orderable')+ end++ it "does not hold back staff's draft order" do+ order.update_columns(customer_id: customer.id)+ create(:line_item, order: order, variant: hidden.default_variant)++ expect(described_class.new(order.reload).call(completion: true)).not_to include(a_hash_including(code: 'not_orderable'))+ end+ end+describe 'fully ready order' dolet(:order) { create(:order_with_line_items, store: store, state: 'payment') }diff --git a/spree/core/spec/models/spree/cart_spec.rb b/spree/core/spec/models/spree/cart_spec.rbindex 2ed347104a..e145fd24d2 100644--- a/spree/core/spec/models/spree/cart_spec.rb+++ b/spree/core/spec/models/spree/cart_spec.rb@@ -230,6 +230,51 @@ describe Spree::Cart, type: :model doendend+ describe '#orderable_variants' do+ let(:company) { create(:company, store: store) }+ let(:catalog) { create(:catalog, store: store) }+ let(:listed) { create(:product, store: store) }+ let(:hidden) { create(:product, store: store) }+ let(:cart) { create(:cart, store: store, customer: customer) }++ before do+ create(:catalog_product, catalog: catalog, product: listed)+ create(:catalog_assignment, catalog: catalog, assignable: company)+ create(:company_membership, company: company, customer: customer)+ end++ it "narrows to the catalogs of the buyer's company" do+ expect(cart.orderable_variants).to include(listed.default_variant)+ expect(cart.orderable_variants).not_to include(hidden.default_variant)+ end++ # A buyer in two companies has no sole standing, so only the company the+ # cart names can say which agreement applies.+ it 'answers from the company the cart is for' do+ other_company = create(:company, store: store)+ other_catalog = create(:catalog, store: store)+ create(:catalog_product, catalog: other_catalog, product: hidden)+ create(:catalog_assignment, catalog: other_catalog, assignable: other_company)+ create(:company_membership, company: other_company, customer: customer)+ cart.update!(company: other_company)++ expect(cart.orderable_variants).to include(hidden.default_variant)+ expect(cart.orderable_variants).not_to include(listed.default_variant)+ end++ it "leaves out a catalog product not published on the cart's channel" do+ listed.product_publications.destroy_all++ expect(cart.orderable_variants).not_to include(listed.default_variant)+ end++ it "leaves out a product whose publication on the cart's channel has ended" do+ listed.product_publications.update_all(unpublished_at: 1.hour.ago)++ expect(cart.orderable_variants).not_to include(listed.default_variant)+ end+ end+describe '#remove_out_of_stock_items!' dolet(:cart) { create(:cart_with_line_items, store: store, customer: customer) }diff --git a/spree/core/spec/services/spree/carts/update_spec.rb b/spree/core/spec/services/spree/carts/update_spec.rbindex 7f3cef009a..a72aef7ac8 100644--- a/spree/core/spec/services/spree/carts/update_spec.rb+++ b/spree/core/spec/services/spree/carts/update_spec.rb@@ -715,11 +715,10 @@ module Spreeenddescribe 'setting line items' do- let(:variant) { create(:variant) }+ let(:variant) { create(:variant, product: create(:product, store: store)) }before dovariant.stock_levels.first.update!(count_on_hand: 10)- store.products << variant.product unless store.products.include?(variant.product)endcontext 'with new line_items' do@@ -778,6 +777,47 @@ module Spreeendendend++ context "with a variant outside the buyer's catalogs" do+ let(:hidden) { create(:product, store: store).default_variant }+ let(:company) { create(:company, store: store) }+ let(:open_company) { create(:company, store: store) }+ let(:params) { { items: [{ variant_id: hidden.prefixed_id, quantity: 1 }] } }++ before do+ hidden.stock_levels.first.update!(count_on_hand: 10)+ catalog = create(:catalog, store: store)+ create(:catalog_product, catalog: catalog, product: create(:product, store: store))+ create(:catalog_assignment, catalog: catalog, assignable: company)+ create(:company_membership, company: company, customer: user)+ end++ it 'refuses the item and leaves the cart as it was' do+ expect { subject }.to raise_error(ActiveRecord::RecordNotFound)+ expect(cart.reload.line_items.map(&:variant)).not_to include(hidden)+ end++ it 'checks the items against a company named in the same request' do+ create(:company_membership, company: open_company, customer: user)+ cart.update!(company: open_company)++ expect {+ described_class.call(cart: cart, params: params.merge(company_id: company.prefixed_id))+ }.to raise_error(ActiveRecord::RecordNotFound)+ end++ # A claimed guest cart can already hold the line; resending the cart+ # must not fail every other change in the request. Checkout judges it.+ it 'still applies a quantity edit to a line already in the cart' do+ line_item = create(:line_item, cart: cart, order: nil, variant: hidden)++ result = described_class.call(cart: cart, params: params.merge(email: '[email protected]', items: [{ variant_id: hidden.prefixed_id, quantity: 3 }]))++ expect(result).to be_success+ expect(line_item.reload.quantity).to eq(3)+ expect(cart.reload.email).to eq('[email protected]')+ end+ endenddescribe 'error handling' do