mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Merge pull request #3892 from coopdevs/hide-product-from-shopfront-when-out-of-stock
Hide product from shopfront when out of stock
This commit is contained in:
7
app/models/spree/stock_item_decorator.rb
Normal file
7
app/models/spree/stock_item_decorator.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
Spree::StockItem.class_eval do
|
||||
after_save :refresh_products_cache
|
||||
|
||||
def refresh_products_cache
|
||||
OpenFoodNetwork::ProductsCache.variant_changed(variant)
|
||||
end
|
||||
end
|
||||
10
spec/models/spree/stock_item_spec.rb
Normal file
10
spec/models/spree/stock_item_spec.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Spree::StockItem do
|
||||
let!(:variant) { create(:variant) }
|
||||
|
||||
it 'refreshes the products cache on save' do
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_changed).with(variant)
|
||||
variant.on_hand = -2
|
||||
end
|
||||
end
|
||||
@@ -166,7 +166,8 @@ module Spree
|
||||
let(:variant) { create(:variant) }
|
||||
|
||||
it "refreshes the products cache on save" do
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_changed).with(variant)
|
||||
# When creating the variant both the Variant and StockItem callbacks get executed
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_changed).with(variant).twice
|
||||
variant.sku = 'abc123'
|
||||
variant.save
|
||||
end
|
||||
@@ -182,7 +183,8 @@ module Spree
|
||||
|
||||
it "refreshes the products cache for the entire product on save" do
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:product_changed).with(product)
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_changed).never
|
||||
# The StockItem callback is still executed
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_changed).once
|
||||
master.sku = 'abc123'
|
||||
master.save
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user