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.
This commit is contained in:
Cillian O'Ruanaidh
2020-10-16 11:38:49 +01:00
parent 22bd0f3a52
commit 2cc751cb30
2 changed files with 17 additions and 5 deletions

View File

@@ -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

View File

@@ -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