Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix issue in admin order checkout, by Matthew

Przemek Kosakowskif0f67e5f5b60
diff --git a/backend/app/assets/javascripts/spree/backend/checkouts/edit.js b/backend/app/assets/javascripts/spree/backend/checkouts/edit.js
index 0a8175313f..88a4411347 100644
--- a/backend/app/assets/javascripts/spree/backend/checkouts/edit.js
+++ b/backend/app/assets/javascripts/spree/backend/checkouts/edit.js
@@ -1,91 +1,141 @@
//= require_self
/* global customerTemplate, update_state */
// eslint-disable-next-line camelcase
-var clear_address_fields = function () {
- var fields = ['firstname', 'lastname', 'company', 'address1', 'address2', 'city', 'zipcode', 'state_id', 'country_id', 'phone']
+
+function clear_billing_address_fields () {
+ var fields = ['firstname', 'lastname', 'company', 'address1', 'address2',
+ 'city', 'zipcode', 'state_id', 'country_id', 'phone']
$.each(fields, function (i, field) {
$('#order_bill_address_attributes_' + field).val('')
+ })
+}
+
+function clear_shipping_address_fields () {
+ var fields = ['firstname', 'lastname', 'company', 'address1', 'address2',
+ 'city', 'zipcode', 'state_id', 'country_id', 'phone']
+ $.each(fields, function (i, field) {
$('#order_ship_address_attributes_' + field).val('')
})
}
-$(document).ready(function () {
- if ($('#customer_autocomplete_template').length > 0) {
- window.customerTemplate = Handlebars.compile($('#customer_autocomplete_template').text())
- }
+function clear_address_fields () {
+ clear_billing_address_fields ()
+ clear_shipping_address_fields ()
+}
- function formatCustomerResult (customer) {
- if (customer.loading) {
- return customer.text
- }
+function formatCustomerResult (customer) {
+ var escapedResult = customerTemplate({
+ customer: customer,
+ bill_address: customer.bill_address,
+ ship_address: customer.ship_address
+ })
+ return $(escapedResult)
+}
+
+function formatCustomerSelection (customer) {
+ $('#order_email').val(customer.email)
+ $('#order_user_id').val(customer.id)
+ $('#guest_checkout_true').prop('checked', false)
+ $('#guest_checkout_false').prop('checked', true)
+ $('#guest_checkout_false').prop('disabled', false)
+
+ var billAddress = customer.bill_address
+ var shipAddress = customer.ship_address
- return $(customerTemplate({
- customer: customer,
- bill_address: customer.bill_address,
- ship_address: customer.ship_address
- }))
+ if (billAddress) {
+ $('#order_bill_address_attributes_firstname').val(billAddress.firstname)
+ $('#order_bill_address_attributes_lastname').val(billAddress.lastname)
+ $('#order_bill_address_attributes_address1').val(billAddress.address1)
+ $('#order_bill_address_attributes_company').val(billAddress.company)
+ $('#order_bill_address_attributes_address2').val(billAddress.address2)
+ $('#order_bill_address_attributes_city').val(billAddress.city)
+ $('#order_bill_address_attributes_zipcode').val(billAddress.zipcode)
+ $('#order_bill_address_attributes_phone').val(billAddress.phone)
+ $('#order_bill_address_attributes_phone').val(billAddress.phone)
+ $('#order_bill_address_attributes_country_id').select2().val(billAddress.country_id)
+ $('#order_bill_address_attributes_country_id').select2().trigger('change.select2').promise().done(function () {
+ update_state('b', function () {
+ if ($('span#bstate select.select2').find("option[value='" + billAddress.state_id + "']").length) {
+ $('span#bstate select.select2').val(billAddress.state_id).trigger('change.select2')
+ }
+ })
+ })
+ } else {
+ clear_billing_address_fields ()
}
- if ($('#customer_search').length > 0) {
- $('#customer_search').select2({
- placeholder: Spree.translations.choose_a_customer,
- dropdownCssClass: 'customer_search',
- ajax: {
- url: Spree.routes.users_api,
- datatype: 'json',
- cache: true,
- data: function (params) {
- var query = {
- q: {
- 'm': 'or',
- 'email_start': params.term,
- 'ship_address_firstname_start': params.term,
- 'ship_address_lastname_start': params.term,
- 'bill_address_firstname_start': params.term,
- 'bill_address_lastname_start': params.term
- },
- token: Spree.api_key
- }
-
- return query
- },
- processResults: function (data, page) {
- return { results: data.users }
+ if (shipAddress) {
+ $('#order_ship_address_attributes_firstname').val(shipAddress.firstname)
+ $('#order_ship_address_attributes_lastname').val(shipAddress.lastname)
+ $('#order_ship_address_attributes_address1').val(shipAddress.address1)
+ $('#order_ship_address_attributes_company').val(shipAddress.company)
+ $('#order_ship_address_attributes_address2').val(shipAddress.address2)
+ $('#order_ship_address_attributes_city').val(shipAddress.city)
+ $('#order_ship_address_attributes_zipcode').val(shipAddress.zipcode)
+ $('#order_ship_address_attributes_phone').val(shipAddress.phone)
+ $('#order_ship_address_attributes_phone').val(shipAddress.phone)
+ $('#order_ship_address_attributes_country_id').select2().val(shipAddress.country_id)
+ $('#order_ship_address_attributes_country_id').select2().trigger('change.select2').promise().done(function () {
+ update_state('s', function () {
+ if ($('span#sstate select.select2').find("option[value='" + shipAddress.state_id + "']").length) {
+ $('span#sstate select.select2').val(shipAddress.state_id).trigger('change.select2')
}
- },
- templateResult: formatCustomerResult,
- templateSelection: function (customer) {
- $('#order_email').val(customer.email)
- $('#order_user_id').val(customer.id)
- $('#guest_checkout_true').prop('checked', false)
- $('#guest_checkout_false').prop('checked', true)
- $('#guest_checkout_false').prop('disabled', false)
-
- var billAddress = customer.bill_address
- if (billAddress) {
- $('#order_bill_address_attributes_firstname').val(billAddress.firstname)
- $('#order_bill_address_attributes_lastname').val(billAddress.lastname)
- $('#order_bill_address_attributes_company').val(billAddress.company)
- $('#order_bill_address_attributes_address1').val(billAddress.address1)
- $('#order_bill_address_attributes_address2').val(billAddress.address2)
- $('#order_bill_address_attributes_city').val(billAddress.city)
- $('#order_bill_address_attributes_zipcode').val(billAddress.zipcode)
- $('#order_bill_address_attributes_phone').val(billAddress.phone)
-
- $('#order_bill_address_attributes_country_id').val(billAddress.country_id).trigger('change').promise().done(function () {
- update_state('b', function () {
- $('#order_bill_address_attributes_state_id').val(billAddress.state_id).trigger('change')
- })
- })
- } else {
- clear_address_fields()
+ })
+ })
+ } else {
+ clear_shipping_address_fields ()
+ }
+
+ return customer.email
+}
+
+// Select2-AJAX
+// Searches for Users
+// Used in Order -> Customer
+function set_customer_search_select (selector) {
+ $(selector).select2({
+ minimumInputLength: 3,
+ placeholder: Spree.translations.choose_a_customer,
+ ajax: {
+ url: Spree.routes.users_api,
+ datatype: 'json',
+ data: function (params, page) {
+ return {
+ q: {
+ 'm': 'or',
+ email_start: params.term,
+ ship_address_firstname_start: params.term,
+ ship_address_lastname_start: params.term,
+ bill_address_firstname_start: params.term,
+ bill_address_lastname_start: params.term
+ },
+ token: Spree.api_key
}
- return customer.email
+ },
+ processResults: function (data, page) {
+ return { results: data['users'] }
}
- })
+ },
+ templateResult: formatCustomerResult
+ }).on('select2:select', function (e) {
+ var data = e.params.data;
+ formatCustomerSelection(data)
+ });
+}
+
+document.addEventListener('DOMContentLoaded', function() {
+ // Set up Select 2 on page ready
+ if ($('#customer_search').length > 0) {
+ set_customer_search_select('#customer_search')
+ }
+
+ if ($('#customer_autocomplete_template').length > 0) {
+ window.customerTemplate = Handlebars.compile($('#customer_autocomplete_template').text())
}
+ // Handle Billing Shipping Address
var orderUseBillingInput = $('input#order_use_billing')
+
var orderUseBilling = function () {
if (!orderUseBillingInput.is(':checked')) {
$('#shipping').show()
@@ -94,9 +144,13 @@ $(document).ready(function () {
}
}
- orderUseBillingInput.click(orderUseBilling)
+ // On page load hide shipping address from
orderUseBilling()
+ // On click togggle shipping address from
+ orderUseBillingInput.click(orderUseBilling)
+
+ // If guest checkout clear fields
$('#guest_checkout_true').change(function () {
$('#customer_search').val('')
$('#order_user_id').val('')
diff --git a/backend/app/views/spree/admin/orders/customer_details/_autocomplete.js.erb b/backend/app/views/spree/admin/orders/customer_details/_autocomplete.js.erb
index 9484fa0dbb..21945c29b5 100644
--- a/backend/app/views/spree/admin/orders/customer_details/_autocomplete.js.erb
+++ b/backend/app/views/spree/admin/orders/customer_details/_autocomplete.js.erb
@@ -1,19 +1,42 @@
<script type='text/template' id='customer_autocomplete_template'>
- <div class='customer-autocomplete-item'>
- <div class='customer-details'>
- <h5>{{customer.email}}</h5>
- {{#if bill_address.firstname }}
- <strong>{{t 'bill_address' }}</strong>
- {{bill_address.firstname}} {{bill_address.lastname}}<br>
- {{bill_address.address1}}, {{bill_address.address2}}<br>
- {{bill_address.city}}<br>
- {{#if bill_address.state_id }}
- {{bill_address.state.name}}
+ <div class='customer-autocomplete-item container'>
+ <div class="row">
+ <div class="col">
+ <h5><strong>Email:</strong> {{customer.email}}</h5>
+ </div>
+ </div>
+
+ <div class="row">
+ <div class='customer-details col-6'>
+ {{#if bill_address.firstname }}
+ <strong>{{t 'bill_address' }}</strong><br>
+ {{bill_address.firstname}} {{bill_address.lastname}}<br>
+ {{bill_address.address1}}, {{bill_address.address2}}<br>
+ {{bill_address.city}}<br>
+ {{#if bill_address.state_id }}
+ {{bill_address.state.name}}
+ {{else}}
+ {{bill_address.state_name}}
+ {{/if}}
+ {{bill_address.country.name}}
+ {{/if}}
+ </div>
+
+ <div class='customer-details col-6'>
+ {{#if ship_address.firstname }}
+ <strong>Shipping Address</strong><br>
+ {{ship_address.firstname}} {{ship_address.lastname}}<br>
+ {{ship_address.address1}}, {{ship_address.address2}}<br>
+ {{ship_address.city}}<br>
+ {{#if ship_address.state_id }}
+ {{ship_address.state.name}}
{{else}}
- {{bill_address.state_name}}
+ {{ship_address.state_name}}
{{/if}}
- {{bill_address.country.name}}
+ {{ship_address.country.name}}
{{/if}}
</div>
+
</div>
+</div>
</script>
diff --git a/backend/app/views/spree/admin/orders/customer_details/edit.html.erb b/backend/app/views/spree/admin/orders/customer_details/edit.html.erb
index 05a92a3f11..190715c0d6 100644
--- a/backend/app/views/spree/admin/orders/customer_details/edit.html.erb
+++ b/backend/app/views/spree/admin/orders/customer_details/edit.html.erb
@@ -12,7 +12,7 @@
</h1>
</div>
<div class="card-body" id="customer-search-field">
- <%= select_tag :customer_search, nil, class: 'error-message d-block w-100' %>
+ <%= select_tag :customer_search, nil, class: 'd-block w-100' %>
<%= render partial: "spree/admin/orders/customer_details/autocomplete", formats: :js %>
</div>
</div>