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: stopped add-to-cart options from replacing the checked variant or quantity
Damian Legawiec1f4e0be34442
diff --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 233fb33af6..5e3770a327 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,12 +97,15 @@ module Spreeend# Extension attributes ride in `options`, which is how AddItem- # forwards per-line-item values onto the record.+ # forwards per-line-item values onto the record. Only those reach+ # it: anything else in `options` is internal to AddItem (the+ # fulfillment an admin pins a line to) and not the shopper's to set.def item_options- keys = Spree::LineItem.additional_permitted_attributes.flat_map { |a| a.is_a?(Hash) ? a.keys : a }- extras = permitted_params.to_h.slice(*keys.map(&:to_s)).symbolize_keys+ keys = Spree::LineItem.additional_permitted_attributes.flat_map { |a| a.is_a?(Hash) ? a.keys : a }.map(&:to_s)- (permitted_params[:options] || {}).to_h.symbolize_keys.merge(extras)+ (permitted_params[:options] || {}).to_h.slice(*keys).+ merge(permitted_params.to_h.slice(*keys)).+ symbolize_keysenddef permitted_paramsdiff --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 5c9eb28428..df1e66e0f3 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@@ -32,6 +32,22 @@ RSpec.describe Spree::Api::V3::Store::Carts::ItemsController, type: :controllerexpect(json_response['total_quantity']).to eq(1)end+ # `options` could once replace the checked variant after it was priced,+ # buying an expensive item at a cheap one's price.+ it 'ignores a variant or quantity sent in options' do+ expensive = create(:variant, product: product, price: 2000)++ post :create, params: {+ cart_id: order.prefixed_id, variant_id: variant.prefixed_id, quantity: 1,+ options: { variant_id: expensive.id, quantity: 50 }+ }++ expect(response).to have_http_status(:created)+ line_item = order.reload.line_items.first+ expect(line_item.variant).to eq(variant)+ expect(line_item.quantity).to eq(1)+ end+# A completion attempt fixed the cart's totals and is off at the gateway —# a mutation landing in that window would complete the cart with different# totals than the ones being charged.diff --git a/spree/core/app/workflows/spree/carts/add_item.rb b/spree/core/app/workflows/spree/carts/add_item.rbindex dbabacbe29..98df8207a1 100644--- a/spree/core/app/workflows/spree/carts/add_item.rb+++ b/spree/core/app/workflows/spree/carts/add_item.rb@@ -1,9 +1,6 @@module Spreemodule Cartsclass AddItem < Spree::Workflow- # Line item attributes a caller may set through `options`.- ITEM_OPTIONS = [:id, :variant_id, :quantity].freeze-hooks :validate, :after_item_added# Hook handlers read these plus the argument readers. Both are nil@@ -110,10 +107,11 @@ module Spreeif @line_item.nil?# `LineItem#options=` mass-assigns, so the caller's hash is narrowed- # first — `options` must not be a way to set arbitrary attributes.- # Extensions widen this through the model's permitted attributes.- writable = ITEM_OPTIONS +- ::Spree::LineItem.additional_permitted_attributes.flat_map { |a| a.is_a?(Hash) ? a.keys : a }+ # to the attributes extensions declare. The variant and quantity are+ # never among them: both were checked and priced above, and an+ # `options` value replacing them would sell something else.+ writable = ::Spree::LineItem.additional_permitted_attributes.flat_map { |a| a.is_a?(Hash) ? a.keys : a }.+ map(&:to_sym) - [:id, :variant_id, :quantity]opts = item_options.symbolize_keys.slice(*writable).merge(currency: cart.currency).diff --git a/spree/core/spec/workflows/spree/carts/add_item_spec.rb b/spree/core/spec/workflows/spree/carts/add_item_spec.rbindex 42595702b5..d3c797e73c 100644--- a/spree/core/spec/workflows/spree/carts/add_item_spec.rb+++ b/spree/core/spec/workflows/spree/carts/add_item_spec.rb@@ -111,15 +111,19 @@ module Spreeendend- context 'pass valid params hash in options' do- let(:options) { { quantity: 2, variant_id: variant.id } }- let(:execute) { subject.call(cart: cart, variant: variant, quantity: nil, options: options) }-- it 'takes the quantity from options' do+ # The variant and quantity are checked and priced before the line is+ # built; an `options` value replacing them would sell something else at+ # the checked item's price.+ context 'options naming another variant' do+ let(:expensive_variant) { create(:variant, price: 2000) }+ let(:options) { { variant_id: expensive_variant.id, id: 999 } }+ let(:execute) { subject.call(cart: cart, variant: variant, quantity: 1, options: options) }++ it 'keeps the checked variant and its price' doexpect(execute).to be_success- expect(cart.line_items.count).to eq 1line_item = cart.line_items.first- expect(line_item.quantity).to eq 2+ expect(line_item.variant).to eq(variant)+ expect(line_item.price).to eq(20)endend@@ -128,10 +132,10 @@ module Spreelet(:options) { { quantity: 2 } }let(:execute) { subject.call(cart: cart, variant: variant, quantity: 3, options: options) }- it 'takes value from options' do+ it 'takes the checked quantity, not the one in options' doexpect(execute).to be_successline_item = cart.line_items.first- expect(line_item.quantity).to eq 2+ expect(line_item.quantity).to eq 3endend