diff --git a/app/models/concerns/variant_stock.rb b/app/models/concerns/variant_stock.rb index 7489aa5893..7e40bd2afa 100644 --- a/app/models/concerns/variant_stock.rb +++ b/app/models/concerns/variant_stock.rb @@ -67,6 +67,18 @@ module VariantStock end end + def on_demand_desired_or_current + return on_demand_desired if new_record? + + on_demand + end + + def on_hand_desired_or_current + return on_hand_desired if new_record? + + on_hand + end + # Moving Spree::Stock::Quantifier.can_supply? to the variant enables us # to override this behaviour for variant overrides # We can have this responsibility here in the variant because there is diff --git a/app/services/sets/product_set.rb b/app/services/sets/product_set.rb index 2a692a08a0..75034ebf76 100644 --- a/app/services/sets/product_set.rb +++ b/app/services/sets/product_set.rb @@ -131,7 +131,11 @@ module Sets return variant if variant.errors.present? begin - create_stock_for_variant(variant, on_demand, on_hand) + if on_hand || on_demand + create_stock_for_variant(variant, on_demand, on_hand) + else + create_stock_for_variant_from_desired(variant) + end rescue StandardError => e notify_bugsnag(e, product, variant, variant_attributes) raise e @@ -156,8 +160,11 @@ module Sets def create_stock_for_variant(variant, on_demand, on_hand) variant.on_demand = on_demand if on_demand.present? - variant.on_demand = variant.on_demand_desired if variant.on_demand_desired.present? variant.on_hand = on_hand.to_i if on_hand.present? + end + + def create_stock_for_variant_from_desired(variant) + variant.on_demand = variant.on_demand_desired if variant.on_demand_desired.present? variant.on_hand = variant.on_hand_desired.to_i if variant.on_hand_desired.present? end end diff --git a/app/views/admin/products_v3/_variant_row.html.haml b/app/views/admin/products_v3/_variant_row.html.haml index 8624121eca..f32474e50e 100644 --- a/app/views/admin/products_v3/_variant_row.html.haml +++ b/app/views/admin/products_v3/_variant_row.html.haml @@ -38,10 +38,10 @@ = error_message_on variant, :price %td.col-on_hand.field.popout{'data-controller': "popout"} %button.popout__button{'data-popout-target': "button", 'aria-label': t('admin.products_page.columns.on_hand')} - = variant.send(method_on_demand) ? t(:on_demand) : variant.send(method_on_hand) + = variant.on_demand_desired_or_current ? t(:on_demand) : variant.on_hand_desired_or_current %div.popout__container{ style: 'display: none;', 'data-controller': 'toggle-control', 'data-popout-target': "dialog" } .field - = f.number_field method_on_hand, min: 0, 'aria-label': t('admin.products_page.columns.on_hand'), 'data-toggle-control-target': 'control', disabled: f.object.send(method_on_demand) + = f.number_field method_on_hand, min: 0, 'aria-label': t('admin.products_page.columns.on_hand'), 'data-toggle-control-target': 'control', disabled: f.object.on_demand_desired_or_current = error_message_on variant, method_on_hand .field.checkbox = f.label method_on_demand do