Skip to content

Commit fc8e4ee

Browse files
Add tax_category_id to Product model
The LineItem and the Variant are referencing the tax_category_id, but does not have the same logic as the tax_category (the fallback to the default tax category). This method mirrors the same logic as the tax_category method.
1 parent 7180bdc commit fc8e4ee

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

core/app/models/spree/product.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ def tax_category
147147
super || Spree::TaxCategory.find_by(is_default: true)
148148
end
149149

150+
# @return [Integer] tax category id for this product, or the default tax category id
151+
def tax_category_id
152+
super || tax_category&.id
153+
end
154+
150155
# Ensures option_types and product_option_types exist for keys in
151156
# option_values_hash.
152157
#

core/spec/models/spree/product_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,35 @@ class Extension < Spree::Base
285285
end
286286
end
287287

288+
describe "#tax_category_id" do
289+
context "when the product has a tax category" do
290+
let(:tax_category) { create(:tax_category) }
291+
let(:product) { create(:product, tax_category:) }
292+
293+
it "returns the product's tax category id" do
294+
expect(product.tax_category_id).to eq(tax_category.id)
295+
end
296+
end
297+
298+
context "when the product has no tax category" do
299+
let(:product) { create(:product, tax_category: nil) }
300+
301+
context "and a default tax category exists" do
302+
let!(:default_tax_category) { create(:tax_category, is_default: true) }
303+
304+
it "returns the default tax category's id" do
305+
expect(product.tax_category_id).to eq(default_tax_category.id)
306+
end
307+
end
308+
309+
context "and no default tax category exists" do
310+
it "returns nil" do
311+
expect(product.tax_category_id).to be_nil
312+
end
313+
end
314+
end
315+
end
316+
288317
context "variants_and_option_values_for" do
289318
let!(:high) { create(:variant, product:) }
290319
let!(:low) { create(:variant, product:) }

0 commit comments

Comments
 (0)