From 2cc751cb30c8ffb6661641d9277226fb0077dc8d Mon Sep 17 00:00:00 2001 From: Cillian O'Ruanaidh Date: Fri, 16 Oct 2020 11:38:49 +0100 Subject: [PATCH] When resetting stock to 0 on absent products in product import also reset the on demand setting Before when you imported products and clicked the 'Set stock to zero for all existing products not present in the file' option it would set the on hand stock to 0 but if the variant was also set to be on demand the product would still be available for sale. This change makes sure the on demand setting is turned off too. Fixes #6064. --- .../product_import/products_reset_strategy.rb | 12 +++++++----- .../product_import/products_reset_strategy_spec.rb | 10 ++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/engines/catalog/app/services/catalog/product_import/products_reset_strategy.rb b/engines/catalog/app/services/catalog/product_import/products_reset_strategy.rb index c4cf28cdf1..da963717e7 100644 --- a/engines/catalog/app/services/catalog/product_import/products_reset_strategy.rb +++ b/engines/catalog/app/services/catalog/product_import/products_reset_strategy.rb @@ -12,7 +12,7 @@ module Catalog return 0 if enterprise_ids.blank? - reset_variants_on_hand(enterprise_variants_relation) + reset_variants_on_hand_and_on_demand(enterprise_variants_relation) end private @@ -32,17 +32,19 @@ module Catalog relation.where('spree_variants.id NOT IN (?)', excluded_items_ids) end - def reset_variants_on_hand(variants) + def reset_variants_on_hand_and_on_demand(variants) updated_records_count = 0 variants.each do |variant| - updated_records_count += 1 if reset_variant_on_hand(variant) + updated_records_count += 1 if reset_variant_on_hand_and_on_demand(variant) end updated_records_count end - def reset_variant_on_hand(variant) + def reset_variant_on_hand_and_on_demand(variant) + was_on_demand = variant.on_demand + variant.on_demand = false variant.on_hand = 0 - variant.on_hand.zero? + variant.on_hand.zero? || was_on_demand end end end diff --git a/engines/catalog/spec/services/catalog/product_import/products_reset_strategy_spec.rb b/engines/catalog/spec/services/catalog/product_import/products_reset_strategy_spec.rb index a2fd01d01b..823ed5d6b1 100644 --- a/engines/catalog/spec/services/catalog/product_import/products_reset_strategy_spec.rb +++ b/engines/catalog/spec/services/catalog/product_import/products_reset_strategy_spec.rb @@ -91,6 +91,16 @@ module Catalog expect { products_reset.reset(supplier_ids) }.to raise_error RuntimeError end end + + context 'and the variant is on demand' do + before { variant.on_demand = true } + + it 'turns off the on demand setting on the variant' do + products_reset.reset(supplier_ids) + + expect(variant.reload.on_demand).to eq(false) + end + end end end