Skip to content

Commit 997f46a

Browse files
authored
Merge pull request #6423 from sascha-karnatz/add-tax-category-id-to-product
Add tax_category_id method to Spree::Product
2 parents a339926 + fc8e4ee commit 997f46a

2 files changed

Lines changed: 63 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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,64 @@ class Extension < Spree::Base
256256
end
257257
end
258258

259+
describe "#tax_category" do
260+
context "when the product has a tax category" do
261+
let(:tax_category) { create(:tax_category) }
262+
let(:product) { create(:product, tax_category:) }
263+
264+
it "returns the product's tax category" do
265+
expect(product.tax_category).to eq(tax_category)
266+
end
267+
end
268+
269+
context "when the product has no tax category" do
270+
let(:product) { create(:product, tax_category: nil) }
271+
272+
context "and a default tax category exists" do
273+
let!(:default_tax_category) { create(:tax_category, is_default: true) }
274+
275+
it "returns the default tax category" do
276+
expect(product.tax_category).to eq(default_tax_category)
277+
end
278+
end
279+
280+
context "and no default tax category exists" do
281+
it "returns nil" do
282+
expect(product.tax_category).to be_nil
283+
end
284+
end
285+
end
286+
end
287+
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+
259317
context "variants_and_option_values_for" do
260318
let!(:high) { create(:variant, product:) }
261319
let!(:low) { create(:variant, product:) }

0 commit comments

Comments
 (0)