From f94d10793a3b25ad82903c30efe4aa10bdb43872 Mon Sep 17 00:00:00 2001 From: Dumitru Ceban Date: Fri, 15 May 2020 11:38:48 +0200 Subject: [PATCH] Add admin UI for shipping methods - stock loc association Introduce the admin UI to be able to associate a shipping method to specific stock locations. This makes editable also `Spree::ShippingMethod#available_to_all` from admin, being directly related to stock location association behaviour. --- .../app/assets/javascripts/spree/backend.js | 1 + .../shipping_methods/stock_locations_picker.js | 18 ++++++++++++++++++ .../admin/shipping_methods/_form.html.erb | 18 ++++++++++++++++++ core/config/locales/en.yml | 8 ++++++++ 4 files changed, 45 insertions(+) create mode 100644 backend/app/assets/javascripts/spree/backend/shipping_methods/stock_locations_picker.js diff --git a/backend/app/assets/javascripts/spree/backend.js b/backend/app/assets/javascripts/spree/backend.js index bad500fdd51..80d9917ada9 100644 --- a/backend/app/assets/javascripts/spree/backend.js +++ b/backend/app/assets/javascripts/spree/backend.js @@ -61,3 +61,4 @@ //= require spree/backend/user_picker //= require spree/backend/variant_autocomplete //= require spree/backend/zone +//= require spree/backend/shipping_methods/stock_locations_picker diff --git a/backend/app/assets/javascripts/spree/backend/shipping_methods/stock_locations_picker.js b/backend/app/assets/javascripts/spree/backend/shipping_methods/stock_locations_picker.js new file mode 100644 index 00000000000..bb9e34dccf9 --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/shipping_methods/stock_locations_picker.js @@ -0,0 +1,18 @@ +Spree.ready(function() { + var $stockLocationsSelect = $('#shipping_method_stock_location_ids'), + $availableToAllCheckbox = $('#shipping_method_available_to_all'); + + if ($stockLocationsSelect.length === 0 || $availableToAllCheckbox.length === 0) { + return; + } + + function toggleLocationSelectVisibility() { + $stockLocationsSelect.toggleClass('hidden', $availableToAllCheckbox[0].checked); + } + + $availableToAllCheckbox.on('click', function() { + toggleLocationSelectVisibility(); + }); + + toggleLocationSelectVisibility(); +}) diff --git a/backend/app/views/spree/admin/shipping_methods/_form.html.erb b/backend/app/views/spree/admin/shipping_methods/_form.html.erb index 7e2f07ccc7e..fd69fca38ff 100644 --- a/backend/app/views/spree/admin/shipping_methods/_form.html.erb +++ b/backend/app/views/spree/admin/shipping_methods/_form.html.erb @@ -54,6 +54,24 @@ <%= error_message_on :shipping_method, :tracking_url %> <% end %> + +
+ <%= f.field_container :stock_locations do %> + + <%= f.collection_select :stock_location_ids, + Spree::StockLocation.order_default, :id, :name, + {}, + class: 'fullwidth select2', + multiple: true, + placeholder: t('spree.admin.shipping_methods.form.stock_locations_placeholder') %> + <%= error_message_on :shipping_method, :stock_locations %> + <% end %> +
<%= f.field_container :available_to_users, class: %w(checkbox) do %> diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml index 281b4c7668b..862c95dde40 100644 --- a/core/config/locales/en.yml +++ b/core/config/locales/en.yml @@ -319,6 +319,8 @@ en: name: Name service_level: Service Level tracking_url: Tracking URL + stock_locations: Stock Locations + available_to_all: Available to all stock locations spree/shipping_rate: amount: Amount label: Label @@ -883,6 +885,9 @@ en: expired: Expired inactive: Inactive not_started: Not started + shipping_methods: + form: + stock_locations_placeholder: 'Choose stock locations' stock_locations: form: address: Address @@ -1411,6 +1416,9 @@ en: If no value is specified, the promotion will be immediately available. promo_code_will_be_disabled: Selecting this option, promo codes will be disabled for this promotion because all its rules / actions will be applied automatically to all orders. + spree/shipping_method: + available_to_all: Uncheck to select specific stock locations this + shipping method will be available. spree/stock_location: active: 'This determines whether stock from this location can be used when building packages.
Default: Checked'