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.

Speed up `TaxRate#adjust` by operating on IDs rather than full objects (#13040)

Damian Legawiec833a3b93bf6d
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 70bc8e3450..87a5fe8c61 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -376,7 +376,7 @@ module Spree
end
# Returns tax category for Product
- # @return [Spree::TaxCategory]
+ # @return [Spree::TaxCategory, nil]
def tax_category
@tax_category ||= super || TaxCategory.default
end
diff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rb
index ebccc9a1c6..4c15354146 100644
--- a/core/app/models/spree/shipment.rb
+++ b/core/app/models/spree/shipment.rb
@@ -358,14 +358,27 @@ module Spree
self.shipped_at = Time.current
end
+ # Returns the shipping method of the selected shipping rate
+ #
+ # @return [Spree::ShippingMethod]
def shipping_method
selected_shipping_rate&.shipping_method || shipping_rates.first&.shipping_method
end
+ # Returns the tax category of the selected shipping rate
+ #
+ # @return [Spree::TaxCategory]
def tax_category
selected_shipping_rate.try(:tax_rate).try(:tax_category)
end
+ # Returns the tax category ID of the selected shipping rate
+ #
+ # @return [Integer]
+ def tax_category_id
+ selected_shipping_rate.try(:tax_rate).try(:tax_category_id)
+ end
+
# Only one of either included_tax_total or additional_tax_total is set
# This method returns the total of the two. Saves having to check if
# tax is included or additional.
diff --git a/core/app/models/spree/tax_rate.rb b/core/app/models/spree/tax_rate.rb
index ebbaf63099..757daaf65f 100644
--- a/core/app/models/spree/tax_rate.rb
+++ b/core/app/models/spree/tax_rate.rb
@@ -72,18 +72,18 @@ module Spree
# to relevant items.
def self.adjust(order, items)
rates = match(order.tax_zone)
- tax_categories = rates.map(&:tax_category)
+ tax_category_ids = rates.map(&:tax_category_id)
# using destroy_all to ensure adjustment destroy callback fires.
Spree::Adjustment.where(adjustable: items).tax.destroy_all
relevant_items = items.select do |item|
- tax_categories.include?(item.tax_category)
+ tax_category_ids.include?(item.tax_category_id)
end
relevant_items.each do |item|
relevant_rates = rates.select do |rate|
- rate.tax_category == item.tax_category
+ rate.tax_category_id == item.tax_category_id
end
store_pre_tax_amount(item, relevant_rates)
relevant_rates.each do |rate|
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index d0312cf6ec..f2e21cab47 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -223,6 +223,8 @@ module Spree
self.class.in_stock_or_backorderable.exists?(id: id)
end
+ # Returns tax category for Variant
+ # @return [Spree::TaxCategory]
def tax_category
@tax_category ||= if self[:tax_category_id].nil?
product.tax_category
@@ -231,6 +233,16 @@ module Spree
end
end
+ # Returns tax category ID for Variant
+ # @return [Integer]
+ def tax_category_id
+ @tax_category_id ||= if self[:tax_category_id].nil?
+ product.tax_category_id
+ else
+ self[:tax_category_id]
+ end
+ end
+
def options_text
@options_text ||= Spree::Variants::OptionsPresenter.new(self).to_sentence
end
diff --git a/core/spec/models/spree/tax_rate_spec.rb b/core/spec/models/spree/tax_rate_spec.rb
index c3eeada88c..d3470fd5d0 100644
--- a/core/spec/models/spree/tax_rate_spec.rb
+++ b/core/spec/models/spree/tax_rate_spec.rb
@@ -176,7 +176,7 @@ describe Spree::TaxRate, type: :model do
before do
allow(Spree::TaxRate).to receive_messages match: [rate_1, rate_2]
- allow(shipment).to receive_messages cost: 10.0, tax_category: tax_category_1
+ allow(shipment).to receive_messages cost: 10.0, tax_category: tax_category_1, tax_category_id: tax_category_1.id
end
it 'applies adjustments for two tax rates to the order' do