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.

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.rb
index 7d9b55716e..e5b2fd5fba 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -32,8 +32,6 @@ module Spree
preference :weight_unit, :string, default: 'lb'
preference :unit_system, :string, default: 'imperial'
- attr_accessor :skip_validate_not_last
-
acts_as_paranoid
has_many :checkouts, -> { incomplete }, class_name: 'Spree::Order', inverse_of: :store
@@ -87,6 +85,16 @@ module Spree
has_rich_text :customer_returns_policy
has_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 do
validates :name, :url, :mail_from_address, :default_currency, :default_country, :code
end
@@ -114,11 +122,15 @@ module Spree
has_one :favicon_image, class_name: 'Spree::StoreFaviconImage', dependent: :destroy, as: :viewable
accepts_nested_attributes_for :favicon_image, reject_if: :all_blank
+ #
+ # Callbacks
before_validation :ensure_default_country
before_save :ensure_default_exists_and_is_unique
before_save :ensure_supported_currencies, :ensure_supported_locales
after_create :ensure_default_taxonomies_are_created
after_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_last
before_destroy :pass_default_flag_to_other_store
@@ -248,6 +260,24 @@ module Spree
preferred_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
+
private
def countries_available_for_checkout_cache_key
diff --git a/core/spec/models/spree/store_spec.rb b/core/spec/models/spree/store_spec.rb
index 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 do
expect(store.taxons.automatic.pluck(:taxonomy_id).uniq).to contain_exactly(collections_taxonomy.id)
end
end
+
+ 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
+ end
end
context 'Validations' do
@@ -661,17 +685,6 @@ describe Spree::Store, type: :model do
expect(another_store.deleted_at).not_to be_nil
end
end
-
- 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
- end
end
describe '#default_stock_location' do