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.

Seed product types and switch sample products to them

Damian Legawiec8957368a15ea
diff --git a/spree/core/app/services/spree/seeds/all.rb b/spree/core/app/services/spree/seeds/all.rb
index 0e14073848..40501fd1f9 100644
--- a/spree/core/app/services/spree/seeds/all.rb
+++ b/spree/core/app/services/spree/seeds/all.rb
@@ -29,6 +29,7 @@ module Spree
# add store resources
PaymentMethods.call
+ ProductTypes.call
CustomerGroups.call
ApiKeys.call
AllowedOrigins.call
diff --git a/spree/core/app/services/spree/seeds/product_types.rb b/spree/core/app/services/spree/seeds/product_types.rb
new file mode 100644
index 0000000000..1718e8ebf5
--- /dev/null
+++ b/spree/core/app/services/spree/seeds/product_types.rb
@@ -0,0 +1,18 @@
+module Spree
+ module Seeds
+ class ProductTypes
+ prepend Spree::ServiceModule::Base
+
+ def call
+ Spree::Store.all.find_each do |store|
+ Spree::ProductType.where(store: store).find_or_create_by!(name: I18n.t('spree.seed.product_types.default')) do |product_type|
+ product_type.fulfillment_types = ['shipping']
+ end
+ Spree::ProductType.where(store: store).find_or_create_by!(name: I18n.t('spree.seed.product_types.digital')) do |product_type|
+ product_type.fulfillment_types = ['digital']
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index da21c123b7..d02ccf4b5d 100644
--- a/spree/core/config/locales/en.yml
+++ b/spree/core/config/locales/en.yml
@@ -1449,6 +1449,9 @@ en:
search_by: 'Search by:'
security: Security
seed:
+ product_types:
+ default: Default
+ digital: Digital
shipping:
categories:
default: Default
diff --git a/spree/core/db/sample_data/products.csv b/spree/core/db/sample_data/products.csv
index 421a413e39..4a7904fa63 100644
--- a/spree/core/db/sample_data/products.csv
+++ b/spree/core/db/sample_data/products.csv
@@ -1,4 +1,4 @@
-slug,sku,name,price,compare_at_price,currency,status,description,meta_title,meta_description,meta_keywords,tags,tax_category,shipping_category,category1,option1_name,option1_value,option2_name,option2_value,image1_src,width,height,depth,dimensions_unit,weight,weight_unit,available_on,discontinue_on,inventory_count,inventory_backorderable,track_inventory,metafield.custom.wattage,metafield.custom.voltage,metafield.custom.warranty,metafield.custom.capacity,metafield.custom.runtime,metafield.custom.room_coverage,metafield.custom.noise_level,metafield.custom.connectivity
+slug,sku,name,price,compare_at_price,currency,status,description,meta_title,meta_description,meta_keywords,tags,tax_category,product_type,category1,option1_name,option1_value,option2_name,option2_value,image1_src,width,height,depth,dimensions_unit,weight,weight_unit,available_on,discontinue_on,inventory_count,inventory_backorderable,track_inventory,metafield.custom.wattage,metafield.custom.voltage,metafield.custom.warranty,metafield.custom.capacity,metafield.custom.runtime,metafield.custom.room_coverage,metafield.custom.noise_level,metafield.custom.connectivity
automatic-espresso-machine,AUTOMATIC-ESPRESSO-MACHINE,Automatic Espresso Machine,0,0,USD,active,"Fully automatic bean-to-cup espresso machine with ceramic grinder, 15-bar pressure system, and intuitive touch display. Delivers barista-quality espresso, cappuccino, and latte at the touch of a button.",Automatic Espresso Machine | Spree Commerce Demo,Shop the Automatic Espresso Machine on the Spree Commerce demo store. Bean-to-cup coffee with 15-bar pressure & ceramic grinder. Spree ecommerce in action.,"automatic espresso machine, bean-to-cup coffee machine, home espresso machine, spree commerce, spree demo store, ecommerce platform demo","coffee, espresso, kitchen, coffee machines, bean-to-cup, automatic",Default,Default,Kitchen -> Coffee Machines,,,,,https://raw.githubusercontent.com/spree/spree/main/docs/images/demo-images/automatic-espresso-machine.webp,,,,in,,lb,,,,FALSE,,1400W,220-240V,2 Years,,,,,
automatic-espresso-machine,AUTOMATIC-ESPRESSO-MACHINE-MATTE-BLACK,,879.99,0,USD,,,,,,,,,,Color,Matte Black,,,https://raw.githubusercontent.com/spree/spree/main/docs/images/demo-images/automatic-espresso-machine-matte-black.webp,13.8,15.7,11.8,in,19.8,lb,,,5000,FALSE,TRUE,,,,,,,,
automatic-espresso-machine,AUTOMATIC-ESPRESSO-MACHINE-SILVER,,879.99,0,USD,,,,,,,,,,Color,Silver,,,https://raw.githubusercontent.com/spree/spree/main/docs/images/demo-images/automatic-espresso-machine-silver.webp,13.8,15.7,11.8,in,19.8,lb,,,5000,FALSE,TRUE,,,,,,,,
diff --git a/spree/core/spec/services/spree/sample_data/loader_spec.rb b/spree/core/spec/services/spree/sample_data/loader_spec.rb
index 4f60bd7f32..c74be0f2df 100644
--- a/spree/core/spec/services/spree/sample_data/loader_spec.rb
+++ b/spree/core/spec/services/spree/sample_data/loader_spec.rb
@@ -14,6 +14,10 @@ RSpec.describe Spree::SampleData::Loader, type: :service, without_global_store:
expect(Spree::Product.count).to be > 30
end
+ it 'assigns imported products a product type' do
+ expect(Spree::Product.where(product_type_id: nil)).to be_empty
+ end
+
it 'creates variants' do
expect(Spree::Variant.count).to be > 80
end
diff --git a/spree/core/spec/services/spree/seeds/product_types_spec.rb b/spree/core/spec/services/spree/seeds/product_types_spec.rb
new file mode 100644
index 0000000000..5b2ee6d859
--- /dev/null
+++ b/spree/core/spec/services/spree/seeds/product_types_spec.rb
@@ -0,0 +1,19 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Seeds::ProductTypes do
+ subject { described_class.call }
+
+ it 'creates the default and digital product types for every store' do
+ subject
+
+ types = Spree::ProductType.where(store: @default_store)
+ expect(types.find_by(name: 'Default').fulfillment_types).to eq(['shipping'])
+ expect(types.find_by(name: 'Digital').fulfillment_types).to eq(['digital'])
+ end
+
+ it 'is idempotent' do
+ described_class.call
+
+ expect { subject }.not_to change(Spree::ProductType, :count)
+ end
+end