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 %> + diff --git a/config/routes.rb b/config/routes.rb index 1fc599b3ea..5d818e42c9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -14,6 +14,7 @@ resources :promotion_batches do member do get :csv_export, to: 'promotion_batches#csv_export' + post :csv_import, to: 'promotion_batches#csv_import' end end