Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ def tax_category
super || Spree::TaxCategory.find_by(is_default: true)
end

# @return [Integer] tax category id for this product, or the default tax category id
def tax_category_id
super || tax_category&.id
end

# Ensures option_types and product_option_types exist for keys in
# option_values_hash.
#
Expand Down
58 changes: 58 additions & 0 deletions core/spec/models/spree/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,64 @@ class Extension < Spree::Base
end
end

describe "#tax_category" do
context "when the product has a tax category" do
let(:tax_category) { create(:tax_category) }
let(:product) { create(:product, tax_category:) }

it "returns the product's tax category" do
expect(product.tax_category).to eq(tax_category)
end
end

context "when the product has no tax category" do
let(:product) { create(:product, tax_category: nil) }

context "and a default tax category exists" do
let!(:default_tax_category) { create(:tax_category, is_default: true) }

it "returns the default tax category" do
expect(product.tax_category).to eq(default_tax_category)
end
end

context "and no default tax category exists" do
it "returns nil" do
expect(product.tax_category).to be_nil
end
end
end
end

describe "#tax_category_id" do
context "when the product has a tax category" do
let(:tax_category) { create(:tax_category) }
let(:product) { create(:product, tax_category:) }

it "returns the product's tax category id" do
expect(product.tax_category_id).to eq(tax_category.id)
end
end

context "when the product has no tax category" do
let(:product) { create(:product, tax_category: nil) }

context "and a default tax category exists" do
let!(:default_tax_category) { create(:tax_category, is_default: true) }

it "returns the default tax category's id" do
expect(product.tax_category_id).to eq(default_tax_category.id)
end
end

context "and no default tax category exists" do
it "returns nil" do
expect(product.tax_category_id).to be_nil
end
end
end
end

context "variants_and_option_values_for" do
let!(:high) { create(:variant, product:) }
let!(:low) { create(:variant, product:) }
Expand Down
Loading