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.

Change `Product#digital?` check to be more reliable

Damian Legawiecbd30da60b6ce
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 29a517a18e..b7cc032b46 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -449,8 +449,13 @@ module Spree
end
end
+ # Check if the product is digital by checking if any of its shipping methods are digital delivery
+ # This is used to determine if the product is digital and should have a digital delivery price
+ # instead of a physical shipping price
+ #
+ # @return [Boolean]
def digital?
- shipping_category&.name == I18n.t('spree.seed.shipping.categories.digital')
+ shipping_category&.shipping_methods&.any? { |method| method.calculator.is_a?(Spree::Calculator::Shipping::DigitalDelivery) }
end
private
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index 24c541460c..6808c13cef 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -943,16 +943,19 @@ describe Spree::Product, type: :model do
end
describe '#digital?' do
- context 'when product is not digital' do
- let(:product) { create(:product, stores: [store]) }
+ let(:product) { create(:product, stores: [store], shipping_category: shipping_category) }
+ let(:shipping_category) { create(:shipping_category) }
- it { expect(product.digital?).to eq(false) }
+ context 'when product has a shipping method with DigitalDelivery calculator' do
+ let!(:shipping_method) { create(:shipping_method, calculator: Spree::Calculator::Shipping::DigitalDelivery.new, shipping_categories: [shipping_category]) }
+
+ it { expect(product.digital?).to eq(true) }
end
- context 'when product is digital' do
- let(:product) { create(:product, stores: [store], shipping_category: create(:shipping_category, name: 'Digital')) }
+ context 'when product does not have a shipping method with DigitalDelivery calculator' do
+ let!(:shipping_method) { create(:shipping_method, calculator: Spree::Calculator::Shipping::FlatRate.new, shipping_categories: [shipping_category]) }
- it { expect(product.digital?).to eq(true) }
+ it { expect(product.digital?).to eq(false) }
end
end
end