-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ba63f5
commit 052c89a
Showing
7 changed files
with
169 additions
and
10 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
admin/app/components/solidus_admin/orders/customer/component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
admin/app/components/solidus_admin/orders/show/component.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,33 @@ | ||
import { Controller } from '@hotwired/stimulus' | ||
|
||
export default class extends Controller { | ||
static targets = ["country", "state"] | ||
|
||
connect() { | ||
this.loadStates() | ||
} | ||
|
||
loadStates() { | ||
const countryId = this.countryTarget.value | ||
|
||
fetch(`/admin/countries/${countryId}/states`) | ||
.then(response => response.json()) | ||
.then(data => { | ||
this.updateStateOptions(data) | ||
}) | ||
} | ||
|
||
updateStateOptions(data) { | ||
const stateSelect = this.stateTarget | ||
|
||
stateSelect.innerHTML = "" | ||
|
||
data.forEach(state => { | ||
const option = document.createElement("option") | ||
|
||
option.value = state.id | ||
option.innerText = state.name | ||
stateSelect.appendChild(option) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
admin/app/controllers/solidus_admin/countries_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusAdmin | ||
class CountriesController < SolidusAdmin::BaseController | ||
skip_before_action :authorize_solidus_admin_user! | ||
|
||
def states | ||
@states = Spree::State.where(country_id: params[:country_id]) | ||
render json: @states.select(:id, :name) | ||
end | ||
end | ||
end |
79 changes: 79 additions & 0 deletions
79
admin/app/controllers/solidus_admin/customers_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusAdmin | ||
class CustomersController < SolidusAdmin::BaseController | ||
skip_before_action :authorize_solidus_admin_user! | ||
|
||
before_action :load_order | ||
|
||
def show | ||
edit | ||
end | ||
|
||
def edit | ||
country_id = default_country_id | ||
|
||
@order.build_bill_address(country_id: country_id) if @order.bill_address.nil? | ||
@order.build_ship_address(country_id: country_id) if @order.ship_address.nil? | ||
|
||
@order.bill_address.country_id = country_id if @order.bill_address.country.nil? | ||
@order.ship_address.country_id = country_id if @order.ship_address.country.nil? | ||
|
||
respond_to do |format| | ||
format.html { render component('orders/show').new(order: @order) } | ||
end | ||
end | ||
|
||
def update | ||
binding.irb | ||
if @order.contents.update_cart(order_params) | ||
|
||
if should_associate_user? | ||
requested_user = Spree.user_class.find(params[:user_id]) | ||
@order.associate_user!(requested_user, @order.email.blank?) | ||
end | ||
|
||
unless @order.completed? | ||
@order.contents.advance | ||
@order.refresh_shipment_rates | ||
end | ||
|
||
flash[:success] = t('spree.customer_details_updated') | ||
render action: :edit | ||
end | ||
end | ||
|
||
private | ||
|
||
def default_country_id | ||
country_id = Spree::Country.default.id | ||
|
||
country_id if Spree::Country.available.pluck(:id).include?(country_id) | ||
end | ||
|
||
def order_params | ||
address_attributes = [ | ||
:id, :name, :address1, :address2, :city, :country_id, :state_id, | ||
:zipcode, :phone, :state_name, :country_iso, :alternative_phone, :company, | ||
country: [:iso, :name, :iso3, :iso_name], | ||
state: [:name, :abbr] | ||
] | ||
|
||
params.require(:order).permit( | ||
:email, | ||
:use_billing, | ||
bill_address_attributes: address_attributes, | ||
ship_address_attributes: address_attributes | ||
) | ||
end | ||
|
||
def should_associate_user? | ||
params[:guest_checkout] == "false" && params[:user_id] && params[:user_id].to_i != @order.user_id | ||
end | ||
|
||
def load_order | ||
@order = Spree::Order.find_by!(number: params[:order_id]) | ||
authorize! action_name, @order | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters