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
Ability to copy products and payment methods from another Store during creation
Damian Legawiecb08ff028c323
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rbindex 7d9b55716e..e5b2fd5fba 100644--- a/core/app/models/spree/store.rb+++ b/core/app/models/spree/store.rb@@ -32,8 +32,6 @@ module Spreepreference :weight_unit, :string, default: 'lb'preference :unit_system, :string, default: 'imperial'- attr_accessor :skip_validate_not_last-acts_as_paranoidhas_many :checkouts, -> { incomplete }, class_name: 'Spree::Order', inverse_of: :store@@ -87,6 +85,16 @@ module Spreehas_rich_text :customer_returns_policyhas_rich_text :customer_shipping_policy+ #+ # Virtual attributes+ #+ attribute :import_products_from_store_id, :string, default: nil+ attribute :import_payment_methods_from_store_id, :string, default: nil+ attr_accessor :skip_validate_not_last++ #+ # Validations+ #with_options presence: true dovalidates :name, :url, :mail_from_address, :default_currency, :default_country, :codeend@@ -114,11 +122,15 @@ module Spreehas_one :favicon_image, class_name: 'Spree::StoreFaviconImage', dependent: :destroy, as: :viewableaccepts_nested_attributes_for :favicon_image, reject_if: :all_blank+ #+ # Callbacksbefore_validation :ensure_default_countrybefore_save :ensure_default_exists_and_is_uniquebefore_save :ensure_supported_currencies, :ensure_supported_localesafter_create :ensure_default_taxonomies_are_createdafter_create :ensure_default_automatic_taxons+ after_create :import_products_from_store, if: -> { import_products_from_store_id.present? }+ after_create :import_payment_methods_from_store, if: -> { import_payment_methods_from_store_id.present? }before_destroy :validate_not_last, unless: :skip_validate_not_lastbefore_destroy :pass_default_flag_to_other_store@@ -248,6 +260,24 @@ module Spreepreferred_unit_system == 'metric'end+ def import_products_from_store+ store = Store.find(import_products_from_store_id)+ product_ids = store.products.pluck(:id)++ return if product_ids.empty?++ StoreProduct.insert_all(product_ids.map { |product_id| { store_id: id, product_id: product_id } })+ end++ def import_payment_methods_from_store+ store = Store.find(import_payment_methods_from_store_id)+ payment_method_ids = store.payment_method_ids++ return if payment_method_ids.empty?++ StorePaymentMethod.insert_all(payment_method_ids.map { |payment_method_id| { store_id: id, payment_method_id: payment_method_id } })+ end+privatedef countries_available_for_checkout_cache_keydiff --git a/core/spec/models/spree/store_spec.rb b/core/spec/models/spree/store_spec.rbindex 975281fb2d..aa74d7ad62 100644--- a/core/spec/models/spree/store_spec.rb+++ b/core/spec/models/spree/store_spec.rb@@ -139,6 +139,30 @@ describe Spree::Store, type: :model doexpect(store.taxons.automatic.pluck(:taxonomy_id).uniq).to contain_exactly(collections_taxonomy.id)endend++ describe '#import_products_from_store' do+ let(:store) { build(:store, import_products_from_store_id: other_store.id) }+ let(:other_store) { create(:store) }+ let!(:products) { create_list(:product, 2, stores: [other_store]) }++ it 'imports products from other store' do+ expect { store.save! }.to change(Spree::StoreProduct, :count).by(2)+ expect(store.products.count).to eq(2)++ expect(store.products.pluck(:id)).to match_array(products.pluck(:id))+ end+ end++ describe '#import_payment_methods_from_store' do+ let(:store) { build(:store, import_payment_methods_from_store_id: other_store.id) }+ let(:other_store) { create(:store) }+ let!(:payment_methods) { create_list(:payment_method, 2, stores: [other_store]) }++ it 'imports payment methods from other store' do+ expect { store.save! }.to change(Spree::StorePaymentMethod, :count).by(2)+ expect(store.payment_methods.count).to eq(2)+ end+ endendcontext 'Validations' do@@ -661,17 +685,6 @@ describe Spree::Store, type: :model doexpect(another_store.deleted_at).not_to be_nilendend-- context 'with associations' do- before do- another_store.products << create(:product)- end-- it "doesn't destroy associations" do- associations = described_class.reflect_on_all_associations(:has_many)- expect(associations.select { |a| a.options[:dependent] }.count).to equal(1)- end- endenddescribe '#default_stock_location' do