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 a fully digital product to the sample data

Damian Legawiece9c755806134
diff --git a/spree/core/app/services/spree/sample_data/loader.rb b/spree/core/app/services/spree/sample_data/loader.rb
index 83457d5fe6..b8014e0283 100644
--- a/spree/core/app/services/spree/sample_data/loader.rb
+++ b/spree/core/app/services/spree/sample_data/loader.rb
@@ -29,6 +29,9 @@ module Spree
puts 'Loading sample products...'
load_products
+ puts 'Attaching the sample digital product file...'
+ load_ruby_file('digital_products')
+
puts 'Publishing sample products on the default channel...'
publish_sample_products(Spree::Store.default)
diff --git a/spree/core/db/sample_data/digital_products.rb b/spree/core/db/sample_data/digital_products.rb
new file mode 100644
index 0000000000..2e67b426c2
--- /dev/null
+++ b/spree/core/db/sample_data/digital_products.rb
@@ -0,0 +1,26 @@
+# The digital product itself is a normal row in products.csv (product_type
+# "Digital"), so the import gives it the digital delivery profile — the product
+# type stamps it at creation. The one thing a CSV row cannot carry is the file
+# buyers download, so that is all this step does: attach a PDF to the imported
+# product's default variant.
+store = Spree::Store.default
+
+product = Spree::Product.find_by(store: store, slug: 'the-spree-commerce-handbook')
+
+if product && !product.default_variant.digital_assets.exists?
+ # A minimal valid PDF, built in memory so seeding stays offline. The customer
+ # receives this file through their download link.
+ pdf = +"%PDF-1.4\n"
+ pdf << "1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj\n"
+ pdf << "2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj\n"
+ pdf << "3 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 612 792]>>endobj\n"
+ pdf << "trailer<</Root 1 0 R>>\n%%EOF\n"
+
+ asset = Spree::DigitalAsset.new(variant: product.default_variant)
+ asset.attachment.attach(
+ io: StringIO.new(pdf),
+ filename: 'the-spree-commerce-handbook.pdf',
+ content_type: 'application/pdf'
+ )
+ asset.save!
+end
diff --git a/spree/core/db/sample_data/products.csv b/spree/core/db/sample_data/products.csv
index a5b1c6a9d8..34e0eb2660 100644
--- a/spree/core/db/sample_data/products.csv
+++ b/spree/core/db/sample_data/products.csv
@@ -123,6 +123,7 @@ rotary-shaver-5000,ROTARY-SHAVER-5000-DARK-BLUE,,129.99,0,USD,,,,,,,,,,Color,Dar
rotary-shaver-9000,ROTARY-SHAVER-9000,Rotary Shaver 9000,0,0,USD,active,"Premium rotary shaver with nano-precision blades, skin comfort sensor, 90-minute runtime, wireless charging pad, and cleaning station. Ultra-close shave with zero irritation.",Rotary Shaver 9000 | Spree Commerce Demo,"Browse the Rotary Shaver 9000 on the Spree Commerce demo store. Nano-precision blades, 90-min runtime, wireless charging & cleaning station included.","rotary shaver 9000, premium electric shaver, wireless charging shaver, nano-precision shaver, spree commerce demo, ecommerce example","electric shaver, rotary, personal care, premium, wireless charging",Default,Grooming,Personal Care -> Electric Shavers,,,,,https://raw.githubusercontent.com/spree/spree/main/docs/images/demo-images/rotary-shaver-9000.webp,,,,in,,lb,,,,FALSE,,5W,5V USB-C,3 Years,,90 minutes,,,
rotary-shaver-9000,ROTARY-SHAVER-9000-CHROME,,299.99,0,USD,,,,,,,,,,Color,Chrome,,,https://raw.githubusercontent.com/spree/spree/main/docs/images/demo-images/rotary-shaver-9000-chrome.webp,2.8,5.9,2.4,in,0.8,lb,,,5000,FALSE,TRUE,,,,,,,,
rotary-shaver-9000,ROTARY-SHAVER-9000-MATTE-BLACK,,299.99,0,USD,,,,,,,,,,Color,Matte Black,,,https://raw.githubusercontent.com/spree/spree/main/docs/images/demo-images/rotary-shaver-9000-matte-black.webp,2.8,5.9,2.4,in,0.8,lb,,,5000,FALSE,TRUE,,,,,,,,
+the-spree-commerce-handbook,SPREE-HANDBOOK,The Spree Commerce Handbook,19.99,0,USD,active,"A downloadable guide to building modern commerce with Spree. Delivered as a PDF the moment your order is placed — no shipping, no waiting.",The Spree Commerce Handbook | Spree Commerce Demo,"Download The Spree Commerce Handbook on the Spree Commerce demo store. A digital product delivered instantly as a PDF. Spree ecommerce in action.","spree commerce handbook, digital product, ebook download, spree demo store, ecommerce platform demo","ebook, digital, guide, downloadable",Default,Digital,,,,,,,,,,in,,lb,,,,,FALSE,,,,,,,,
automatic-espresso-machine,AUTOMATIC-ESPRESSO-MACHINE-MATTE-BLACK,,819.99,,EUR,,,,,,,,,,,,,,,,,,,,,,,,FALSE,,,,,,,,,
automatic-espresso-machine,AUTOMATIC-ESPRESSO-MACHINE-SILVER,,819.99,,EUR,,,,,,,,,,,,,,,,,,,,,,,,FALSE,,,,,,,,,
automatic-espresso-machine,AUTOMATIC-ESPRESSO-MACHINE-WHITE,,819.99,,EUR,,,,,,,,,,,,,,,,,,,,,,,,FALSE,,,,,,,,,
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 8e7ead6cf1..00da18fa4b 100644
--- a/spree/core/spec/services/spree/sample_data/loader_spec.rb
+++ b/spree/core/spec/services/spree/sample_data/loader_spec.rb
@@ -94,6 +94,14 @@ RSpec.describe Spree::SampleData::Loader, type: :service, without_global_store:
expect(Spree::Variant.count).to be > 80
end
+ it 'creates a fully digital product with a downloadable file' do
+ product = Spree::Product.find_by(slug: 'the-spree-commerce-handbook')
+
+ expect(product).to be_present
+ expect(product.delivery_profile).to be_digital
+ expect(product.default_variant.digital_assets.first&.attachment).to be_attached
+ end
+
it 'creates customers' do
expect(Spree.customer_class.where.not(email: '[email protected]').count).to be > 5
end