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.

Improve `StockItem#conditional_variant_touch`

Damian Legawiec9ee0dbd505b2
diff --git a/core/app/models/spree/stock_item.rb b/core/app/models/spree/stock_item.rb
index eac0efa8e9..006ee326ac 100644
--- a/core/app/models/spree/stock_item.rb
+++ b/core/app/models/spree/stock_item.rb
@@ -93,12 +93,15 @@ module Spree
end
def conditional_variant_touch
- # the variant_id changes from nil when a new stock location is added
- stock_changed = (saved_change_to_count_on_hand? &&
- saved_change_to_count_on_hand.any?(&:zero?)) ||
- saved_change_to_variant_id?
+ variant.touch if !Spree::Config.binary_inventory_cache || stock_changed?
+ end
- variant.touch if !Spree::Config.binary_inventory_cache || stock_changed
+ def stock_changed?
+ # the variant_id changes from nil when a new stock location is added
+ (
+ saved_change_to_count_on_hand? &&
+ saved_change_to_count_on_hand.any?(&:zero?)
+ ) || saved_change_to_variant_id?
end
end
end
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 29dc4620ef..3c60c75519 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -399,6 +399,10 @@ module Spree
digitals.present?
end
+ def clear_in_stock_cache
+ Rails.cache.delete(in_stock_cache_key)
+ end
+
private
def ensure_not_in_complete_orders
@@ -450,10 +454,6 @@ module Spree
"variant-#{id}-in_stock"
end
- def clear_in_stock_cache
- Rails.cache.delete(in_stock_cache_key)
- end
-
def disable_sku_validation?
Spree::Config[:disable_sku_validation]
end