Skip to content

armgit / spree

Public Imported from GitHub
0

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.

SD-1451 Enable Setting LineItem MetaData when adding Item to Cart (#11525)

Aleksandar Petrushevd4074b363ae9
diff --git a/api/app/controllers/concerns/spree/api/v2/storefront/metadata_controller_concern.rb b/api/app/controllers/concerns/spree/api/v2/storefront/metadata_controller_concern.rb
new file mode 100644
index 0000000000..7dc4f2ae7b
--- /dev/null
+++ b/api/app/controllers/concerns/spree/api/v2/storefront/metadata_controller_concern.rb
@@ -0,0 +1,18 @@
+module Spree
+ module Api
+ module V2
+ module Storefront
+ module MetadataControllerConcern
+ protected
+
+ def ensure_valid_metadata
+ if params[:public_metadata].present? && !params[:public_metadata].is_a?(ActionController::Parameters) ||
+ params[:private_metadata].present? && !params[:private_metadata].is_a?(ActionController::Parameters)
+ render_error_payload(I18n.t(:invalid_params, scope: 'spree.api.v2.metadata'))
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/controllers/spree/api/v2/storefront/cart_controller.rb b/api/app/controllers/spree/api/v2/storefront/cart_controller.rb
index 1aedb7a0cd..d4f4ade0d1 100644
--- a/api/app/controllers/spree/api/v2/storefront/cart_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/cart_controller.rb
@@ -5,7 +5,9 @@ module Spree
class CartController < ::Spree::Api::V2::BaseController
include OrderConcern
include CouponCodesHelper
+ include Spree::Api::V2::Storefront::MetadataControllerConcern
+ before_action :ensure_valid_metadata, only: %i[create add_item]
before_action :ensure_order, except: %i[create associate]
before_action :load_variant, only: :add_item
before_action :require_spree_current_user, only: :associate
@@ -33,8 +35,10 @@ module Spree
result = add_item_service.call(
order: spree_current_order,
variant: @variant,
- quantity: params[:quantity],
- options: params[:options]
+ quantity: add_item_params[:quantity],
+ public_metadata: add_item_params[:public_metadata],
+ private_metadata: add_item_params[:private_metadata],
+ options: add_item_params[:options]
)
render_order(result)
@@ -228,6 +232,10 @@ module Spree
params: serializer_params
).serializable_hash
end
+
+ def add_item_params
+ params.permit(:quantity, :variant_id, public_metadata: {}, private_metadata: {}, options: {})
+ end
end
end
end
diff --git a/api/config/locales/en.yml b/api/config/locales/en.yml
index f620417742..ec3f61effb 100644
--- a/api/config/locales/en.yml
+++ b/api/config/locales/en.yml
@@ -29,6 +29,8 @@ en:
digitals:
missing_file: 'Missing Digital Item: attachment'
unauthorized: Error, you are not authorized to access this asset
+ metadata:
+ invalid_params: Public and private metadata parameters should be objects
wishlist:
errors:
the_wishlist_could_not_be_destroyed: The wishlist could not be destroyed.
diff --git a/api/docs/v2/storefront/index.yaml b/api/docs/v2/storefront/index.yaml
index cf57775aad..efb0802208 100644
--- a/api/docs/v2/storefront/index.yaml
+++ b/api/docs/v2/storefront/index.yaml
@@ -432,11 +432,21 @@ paths:
example:
variant_id: '1'
quantity: 5
+ public_metadata:
+ first_item_order: true
+ private_metadata:
+ recommended_by_us: false
properties:
variant_id:
type: string
quantity:
type: integer
+ public_metadata:
+ type: object
+ description: 'The public metadata for the added item.'
+ private_metadata:
+ type: object
+ description: 'The private metadata for the added item.'
options:
type: object
description: 'Additional custom options. Activate these by adding: `Spree::PermittedAttributes.line_item_attributes << :foo` in `config/initializers/spree.rb`'
@@ -2611,6 +2621,11 @@ components:
display_inluded_tax_total:
type: string
example: $0.00
+ public_metadata:
+ type: object
+ example:
+ recommended_by_us: true
+ description: 'The public metadata for the Line Item.'
relationships:
type: object
properties:
diff --git a/api/spec/requests/spree/api/v2/storefront/cart_spec.rb b/api/spec/requests/spree/api/v2/storefront/cart_spec.rb
index b9440ddd13..3baa9db78a 100644
--- a/api/spec/requests/spree/api/v2/storefront/cart_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/cart_spec.rb
@@ -147,7 +147,16 @@ describe 'API V2 Storefront Cart Spec', type: :request do
describe 'cart#add_item' do
let(:options) { {} }
- let(:params) { { variant_id: variant.id, quantity: 5, options: options, include: 'variants' } }
+ let(:params) do
+ {
+ variant_id: variant.id,
+ quantity: 5,
+ public_metadata: { 'prop1' => 'value1' },
+ private_metadata: { 'prop2' => 'value2' },
+ options: options,
+ include: 'variants'
+ }
+ end
let(:execute) { post '/api/v2/storefront/cart/add_item', params: params, headers: headers }
before do
@@ -166,6 +175,8 @@ describe 'API V2 Storefront Cart Spec', type: :request do
expect(order.line_items.count).to eq(2)
expect(order.line_items.last.variant).to eq(variant)
expect(order.line_items.last.quantity).to eq(5)
+ expect(order.line_items.last.public_metadata).to eq(params[:public_metadata])
+ expect(order.line_items.last.private_metadata).to eq(params[:private_metadata])
expect(json_response['included']).to include(have_type('variant').and(have_id(variant.id.to_s)))
end
@@ -220,6 +231,19 @@ describe 'API V2 Storefront Cart Spec', type: :request do
end
end
+ shared_examples 'doesnt add item if metadata is not a hash' do
+ before do
+ params[:public_metadata] = [1, 2, 3]
+ execute
+ end
+
+ it_behaves_like 'returns 422 HTTP status'
+
+ it 'return an error' do
+ expect(json_response[:error]).to eq(I18n.t(:invalid_params, scope: 'spree.api.v2.metadata'))
+ end
+ end
+
context 'as a signed in user' do
include_context 'order with a physical line item'
@@ -228,6 +252,7 @@ describe 'API V2 Storefront Cart Spec', type: :request do
it_behaves_like 'doesnt add item with quantity unnavailble'
it_behaves_like 'doesnt add item from different store'
it_behaves_like 'doesnt add non-existing item'
+ it_behaves_like 'doesnt add item if metadata is not a hash'
end
it_behaves_like 'no current order'
@@ -241,6 +266,7 @@ describe 'API V2 Storefront Cart Spec', type: :request do
it_behaves_like 'doesnt add item with quantity unnavailble'
it_behaves_like 'doesnt add item from different store'
it_behaves_like 'doesnt add non-existing item'
+ it_behaves_like 'doesnt add item if metadata is not a hash'
end
it_behaves_like 'no current order'
diff --git a/core/app/services/spree/cart/add_item.rb b/core/app/services/spree/cart/add_item.rb
index a533702f16..9036afb1d4 100644
--- a/core/app/services/spree/cart/add_item.rb
+++ b/core/app/services/spree/cart/add_item.rb
@@ -3,7 +3,7 @@ module Spree
class AddItem
prepend Spree::ServiceModule::Base
- def call(order:, variant:, quantity: nil, options: {})
+ def call(order:, variant:, quantity: nil, public_metadata: {}, private_metadata: {}, options: {})
ApplicationRecord.transaction do
run :add_to_line_item
run Spree::Dependencies.cart_recalculate_service.constantize
@@ -12,7 +12,7 @@ module Spree
private
- def add_to_line_item(order:, variant:, quantity: nil, options: {})
+ def add_to_line_item(order:, variant:, quantity: nil, public_metadata: {}, private_metadata: {}, options: {})
options ||= {}
quantity ||= 1
@@ -32,6 +32,8 @@ module Spree
end
line_item.target_shipment = options[:shipment] if options.key? :shipment
+ line_item.public_metadata = public_metadata.to_h if public_metadata
+ line_item.private_metadata = private_metadata.to_h if private_metadata
return failure(line_item) unless line_item.save
diff --git a/core/spec/services/spree/cart/add_item_spec.rb b/core/spec/services/spree/cart/add_item_spec.rb
index 0636c5d3e0..620af56bde 100644
--- a/core/spec/services/spree/cart/add_item_spec.rb
+++ b/core/spec/services/spree/cart/add_item_spec.rb
@@ -208,5 +208,27 @@ module Spree
end
end
end
+
+ context 'setting metadata' do
+ let(:public_metadata) { {'prop1' => 'value1'} }
+ let(:private_metadata) { {'prop2' => 'value2'} }
+ let(:execute) { subject.call(order: order, variant: variant, quantity: qty, public_metadata: public_metadata, private_metadata: private_metadata) }
+
+ context 'private metadata' do
+ it do
+ expect(execute).to be_success
+ order.reload
+ expect(order.line_items.first.private_metadata).to eq private_metadata
+ end
+ end
+
+ context 'public metadata' do
+ it do
+ expect(execute).to be_success
+ order.reload
+ expect(order.line_items.first.public_metadata).to eq public_metadata
+ end
+ end
+ end
end
end