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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 70bc8e3450..87a5fe8c61 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -376,7 +376,7 @@ module Spreeend# Returns tax category for Product- # @return [Spree::TaxCategory]+ # @return [Spree::TaxCategory, nil]def tax_category@tax_category ||= super || TaxCategory.defaultenddiff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rbindex ebccc9a1c6..4c15354146 100644--- a/core/app/models/spree/shipment.rb+++ b/core/app/models/spree/shipment.rb@@ -358,14 +358,27 @@ module Spreeself.shipped_at = Time.currentend+ # Returns the shipping method of the selected shipping rate+ #+ # @return [Spree::ShippingMethod]def shipping_methodselected_shipping_rate&.shipping_method || shipping_rates.first&.shipping_methodend+ # Returns the tax category of the selected shipping rate+ #+ # @return [Spree::TaxCategory]def tax_categoryselected_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.rbindex 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_allrelevant_items = items.select do |item|- tax_categories.include?(item.tax_category)+ tax_category_ids.include?(item.tax_category_id)endrelevant_items.each do |item|relevant_rates = rates.select do |rate|- rate.tax_category == item.tax_category+ rate.tax_category_id == item.tax_category_idendstore_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.rbindex d0312cf6ec..f2e21cab47 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -223,6 +223,8 @@ module Spreeself.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 Spreeendend+ # 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_sentenceenddiff --git a/core/spec/models/spree/tax_rate_spec.rb b/core/spec/models/spree/tax_rate_spec.rbindex 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 dobefore doallow(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.idendit 'applies adjustments for two tax rates to the order' do