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.

Add metadata support to stock items (#11717)

Damian Legawiecd894358e49b8
diff --git a/api/spec/serializers/spree/api/v2/platform/stock_item_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/stock_item_serializer_spec.rb
index a3b3b9ff26..9565e0c973 100644
--- a/api/spec/serializers/spree/api/v2/platform/stock_item_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/stock_item_serializer_spec.rb
@@ -19,7 +19,9 @@ describe Spree::Api::V2::Platform::StockItemSerializer do
created_at: resource.created_at,
deleted_at: resource.deleted_at,
is_available: resource.available?,
- updated_at: resource.updated_at
+ updated_at: resource.updated_at,
+ public_metadata: {},
+ private_metadata: {}
},
relationships: {
stock_location: {
diff --git a/core/app/models/spree/stock_item.rb b/core/app/models/spree/stock_item.rb
index 9543d44bab..6da13a9bfa 100644
--- a/core/app/models/spree/stock_item.rb
+++ b/core/app/models/spree/stock_item.rb
@@ -2,6 +2,7 @@ module Spree
class StockItem < Spree::Base
acts_as_paranoid
+ include Metadata
if defined?(Spree::Webhooks)
include Spree::Webhooks::HasWebhooks
end
diff --git a/core/db/migrate/20220613133029_add_metadata_to_spree_stock_items.rb b/core/db/migrate/20220613133029_add_metadata_to_spree_stock_items.rb
new file mode 100644
index 0000000000..721b085644
--- /dev/null
+++ b/core/db/migrate/20220613133029_add_metadata_to_spree_stock_items.rb
@@ -0,0 +1,13 @@
+class AddMetadataToSpreeStockItems < ActiveRecord::Migration[5.2]
+ def change
+ change_table :spree_stock_items do |t|
+ if t.respond_to? :jsonb
+ add_column :spree_stock_items, :public_metadata, :jsonb
+ add_column :spree_stock_items, :private_metadata, :jsonb
+ else
+ add_column :spree_stock_items, :public_metadata, :json
+ add_column :spree_stock_items, :private_metadata, :json
+ end
+ end
+ end
+end
diff --git a/core/spec/models/spree/stock_item_spec.rb b/core/spec/models/spree/stock_item_spec.rb
index 75930afd29..cd94318913 100644
--- a/core/spec/models/spree/stock_item_spec.rb
+++ b/core/spec/models/spree/stock_item_spec.rb
@@ -5,6 +5,8 @@ describe Spree::StockItem, type: :model do
let(:stock_location) { create(:stock_location_with_items) }
+ it_behaves_like 'metadata'
+
it 'maintains the count on hand for a variant' do
expect(subject.count_on_hand).to eq 10
end