diff --git a/app/controllers/spree/admin/promotion_batches_controller.rb b/app/controllers/spree/admin/promotion_batches_controller.rb index 5adcdb5733..9161d4fdb2 100644 --- a/app/controllers/spree/admin/promotion_batches_controller.rb +++ b/app/controllers/spree/admin/promotion_batches_controller.rb @@ -14,6 +14,14 @@ def csv_export disposition: :attachment, type: "text/csv" end + + def csv_import + content = params[:file]&.read.to_s + Spree::PromotionBatches::PromoCodesImporter.new(content: content, promotion_batch_id: params[:id]).call + redirect_back fallback_location: admin_promotions_path, notice: "Promo codes uploaded" + rescue Spree::PromotionBatches::PromoCodesImporter::Error => e + redirect_back fallback_location: admin_promotions_path, alert: e.message + end end end end diff --git a/app/views/spree/admin/promotion_batches/show.html.erb b/app/views/spree/admin/promotion_batches/show.html.erb index 849f06233e..cc111c6a05 100644 --- a/app/views/spree/admin/promotion_batches/show.html.erb +++ b/app/views/spree/admin/promotion_batches/show.html.erb @@ -14,6 +14,11 @@ <% end %> <% end %> +<%= form_tag csv_import_admin_promotion_batch_path, method: :post, multipart: true do %> + <%= file_field_tag :file, accept: ".csv" %> + <%= submit_tag 'Import codes from CSV' %> +<% end %> +