Apply import defaults to variants of new products

Spree 2 doesn't have `Product#on_hand=` any more. We actually can't save
stock levels without saving the product first which creates a stock
item.

This hacky solution overwrites the attribute in the entry so that it
gets copied to the variant later on. I think a better solution needs
some refactoring as proposed in:
https://github.com/openfoodfoundation/openfoodnetwork/issues/2783#issuecomment-459601530
This commit is contained in:
Maikel Linke
2019-02-01 16:51:52 +11:00
parent 5a4205d151
commit dbfcbc6db9

View File

@@ -212,12 +212,20 @@ module ProductImport
case setting['mode']
when 'overwrite_all'
object.assign_attributes(attribute => setting['value'])
# In case of new products, some attributes are saved on the variant.
# We write them to the entry here to be copied to the variant later.
if entry.respond_to? "#{attribute}="
entry.public_send("#{attribute}=", setting['value'])
end
when 'overwrite_empty'
if object.public_send(attribute).blank? ||
((attribute == 'on_hand' || attribute == 'count_on_hand') &&
entry.on_hand_nil)
object.assign_attributes(attribute => setting['value'])
if entry.respond_to? "#{attribute}="
entry.public_send("#{attribute}=", setting['value'])
end
end
end
end