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
Update variant.out_of_stock.
Sebastian Palma694f13d1491e
diff --git a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb b/api/app/models/spree/api/webhooks/stock_movement_decorator.rbindex 1bfcbfea4d..141325d0bb 100644--- a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb+++ b/api/app/models/spree/api/webhooks/stock_movement_decorator.rb@@ -3,23 +3,25 @@ module Spreemodule Webhooksmodule StockMovementDecoratordef self.prepended(base)+ base.around_save :queue_webhooks_requests_for_variant_out_of_stock!base.around_save :queue_webhooks_requests_for_variant_back_in_stock!base.around_save :queue_webhooks_requests_for_product_back_in_stock!end- # [TODO]: update, this remains as it was- def update_stock_item_quantity- variant_in_stock_before_update = stock_item.variant.in_stock?- super- variant = stock_item.variant- variant.queue_webhooks_requests!('variant.out_of_stock') if variant_in_stock_before_update && !stock_item.variant.in_stock?- end-privatedelegate :variant, to: :stock_itemdelegate :product, to: :variant+ def queue_webhooks_requests_for_variant_out_of_stock!+ variant_in_stock_before_update = variant.in_stock_or_backorderable?+ yield+ if variant_in_stock_before_update && !variant.in_stock_or_backorderable?+ reload+ stock_item.variant.queue_webhooks_requests!('variant.out_of_stock')+ end+ end+def queue_webhooks_requests_for_variant_back_in_stock!variant_was_out_of_stock = !variant.in_stock_or_backorderable?yielddiff --git a/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb b/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rbindex f8cdae2211..4f2db81baf 100644--- a/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb+++ b/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb@@ -3,8 +3,7 @@ require 'spec_helper'describe Spree::Api::Webhooks::StockMovementDecorator dolet(:stock_item) { create(:stock_item) }let(:stock_location) { variant.stock_locations.first }- let(:body) { Spree::Api::V2::Platform::VariantSerializer.new(variant).serializable_hash.to_json }- let!(:f) { create_list(:image, 2) }+ let(:body) { Spree::Api::V2::Platform::VariantSerializer.new(variant.reload).serializable_hash.to_json }describe 'emitting product.back_in_stock' dolet!(:store) { create(:store) }@@ -124,7 +123,9 @@ describe Spree::Api::Webhooks::StockMovementDecorator dosubject { stock_movement }let(:stock_movement) { create(:stock_movement, stock_item: stock_item, quantity: movement_quantity) }- let(:variant) { stock_item.variant }+ let!(:variant) { stock_item.variant }++ before { Spree::StockItem.update_all(backorderable: false) }describe 'when the variant goes out of stock' dolet(:movement_quantity) { -stock_item.count_on_hand }diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rbindex c9b998c344..aa113d4d1a 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -323,7 +323,7 @@ module Spreeenddef full_in_stock?- variants.full_in_stock.exists?+ variants.in_stock_or_backorderable.exists?(product_id: id)endprivatediff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex 301c9ee4cb..00a8be00a0 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -132,6 +132,7 @@ module Spree!discontinued? && product.available?end+ # [TODO]: testdef in_stock_or_backorderable?self.class.in_stock_or_backorderable.exists?(id: id)end