Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] main from solidusio:main #378

Merged
merged 7 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

<%= page_with_sidebar_aside do %>
<%= render component('ui/panel').new(title: panel_title_with_more_links(t(".customer"), [
link_to(t(".edit_email"), "#", class: "p-2 hover:bg-gray-25 rounded-sm text-black"),
link_to(t(".edit_email"), solidus_admin.order_customer_path(@order), class: "p-2 hover:bg-gray-25 rounded-sm text-black"),
link_to(t(".edit_shipping"), "#", class: "p-2 hover:bg-gray-25 rounded-sm text-black"),
link_to(t(".edit_billing"), "#", class: "p-2 hover:bg-gray-25 rounded-sm text-black"),
link_to(t(".remove_customer"), "#", 'data-turbo-method': :delete, class: "p-2 hover:bg-gray-25 rounded-sm text-red-500"),
link_to(t(".remove_customer"), solidus_admin.order_customer_path(@order), 'data-turbo-method': :delete, class: "p-2 hover:bg-gray-25 rounded-sm text-red-500"),
])) do %>
<div class="flex flex-col -m-6 p-6 gap-6 border-t border-gray-100 mt-0">
<%# CUSTOMER %>
Expand Down
4 changes: 2 additions & 2 deletions admin/app/components/solidus_admin/orders/show/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ def format_address(address)
def panel_title_with_more_links(title, links)
tag.details(
tag.summary(
tag.div(
tag.span(
safe_join([
title,
component("ui/button").new(
icon: "more-line",
scheme: :ghost,
tag: :div,
tag: :span,
alt: t("spree.edit"),
class: "cursor-pointer"
).render_in(self),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div class="<%= stimulus_id %>">
<%= render component("orders/show").new(order: @order) %>
<%= render component("ui/modal").new(title: t(".title"), close_path: close_path) do |modal| %>
<%= form_for @order, url: close_path, html: { id: form_id} do |f| %>
<%= render component("ui/forms/field").text_field(f, :email) %>
<label class="body-small mt-4 block">
<%= t('.guest_checkout') %>:
<output class="body-small-bold"><%= @order.user ? t('.yes') : t('.no') %></output>
<%= render component('ui/toggletip').new(text: t('.guest_checkout_tip'), class: "align-middle") %>
</label>
<% end %>

<% modal.with_actions do %>
<%= render component("ui/button").new(tag: :a, scheme: :secondary, href: close_path, type: :submit, text: t('.cancel')) %>
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
<% end %>
<% end %>
</div>
15 changes: 15 additions & 0 deletions admin/app/components/solidus_admin/orders/show/email/component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class SolidusAdmin::Orders::Show::Email::Component < SolidusAdmin::BaseComponent
def initialize(order:)
@order = order
end

def form_id
dom_id(@order, "#{stimulus_id}_email_form")
end

def close_path
@close_path ||= solidus_admin.order_path(@order)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
en:
title: "Edit order email"
submit: "Save"
cancel: "Cancel"
guest_checkout: "Guest checkout"
guest_checkout_tip: "An order without an associated user account is considered a guest checkout."
"yes": "Yes"
"no": "No"
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
>
<a href="<%= @close_path %>" aria-hidden="true" class="cursor-default fixed inset-0 bg-full-black/50 overflow-y-auto"></a>
<div class="fixed inset-0 z-10 pointer-events-none flex min-h-full justify-center p-4 text-center items-center">
<div class="pointer-events-auto cursor-auto relative transform overflow-auto rounded-lg bg-white text-left shadow-xl max-w-lg divide-y divide-gray-100">
<div class="min-w-[40rem] pointer-events-auto cursor-auto relative transform overflow-auto rounded-lg bg-white text-left shadow-xl max-w-lg divide-y divide-gray-100">

<header class="flex items-center justify-between p-4">
<h3 class="text-xl font-semibold text-gray-900">
Expand Down
19 changes: 19 additions & 0 deletions admin/app/controllers/solidus_admin/customers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

class SolidusAdmin::CustomersController < SolidusAdmin::BaseController
before_action :load_order, only: :show

def show
render component('orders/show/email').new(order: @order)
end

private

def load_order
@order = Spree::Order.find_by!(number: params[:order_id])
end

def authorization_subject
@order || Spree::Order
end
end
21 changes: 21 additions & 0 deletions admin/app/controllers/solidus_admin/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module SolidusAdmin
class OrdersController < SolidusAdmin::BaseController
include Spree::Core::ControllerHelpers::StrongParameters

def index
orders = Spree::Order
.order(created_at: :desc, id: :desc)
Expand All @@ -26,6 +28,21 @@ def show
end
end

def update
load_order

@order.assign_attributes(order_params)
@order.email ||= @order.user.email if @order.user && @order.user.changed?

if @order.save
flash[:notice] = t('.success')
else
flash[:error] = t('.error')
end

redirect_to spree.edit_admin_order_path(@order)
end

def edit
redirect_to action: :show
end
Expand Down Expand Up @@ -63,5 +80,9 @@ def load_order
@order = Spree::Order.find_by!(number: params[:id])
authorize! action_name, @order
end

def order_params
params.require(:order).permit(:user_id, permitted_order_attributes)
end
end
end
2 changes: 2 additions & 0 deletions admin/config/locales/orders.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ en:
solidus_admin:
orders:
title: "Orders"
update:
success: "Order was updated successfully"
3 changes: 2 additions & 1 deletion admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
get 'states', to: 'countries#states'
end

resources :orders, only: [:index, :show, :edit] do
resources :orders, only: [:index, :show, :edit, :update] do
resources :line_items, only: [:destroy, :create, :update]
resource :customer

member do
get :variants_for
Expand Down
20 changes: 18 additions & 2 deletions admin/spec/features/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@

require 'spec_helper'

describe "Order", type: :feature do
describe "Order", :js, type: :feature do
before { sign_in create(:admin_user, email: 'admin@example.com') }

it "allows changing the order email" do
create(:order, number: "R123456789", total: 19.99)

visit "/admin/orders/R123456789/edit"

expect(page).to have_content("Order R123456789")
find("summary", text: "Customer").click
click_on "Edit order email"
within("dialog") do
fill_in "Customer Email", with: "a@b.c"
click_on "Save"
end
expect(page).to have_content("Order was updated successfully")
expect(page).to have_content("Order contact email a@b.c", normalize_ws: true)
end

context "in cart state" do
it "allows managing the cart", :js do
it "allows managing the cart" do
create(:product, name: "Just a product", slug: 'just-a-prod', price: 19.99)
create(:product, name: "Just another product", slug: 'just-another-prod', price: 29.99)
create(:order, number: "R123456789", total: 19.99, state: "cart")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
}
}

#actions_container {
.edit_promotion {
margin-top: 73px;
}
}

.promotion-block {
padding: 0 1.25rem 0.5rem;
background-color: lighten($color-border, 5);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="field">
<% field_name = "#{param_prefix}[preferred_minimum_quantity]" %>
<%= label_tag field_name, promotion_rule.model_name.human %>
<%= number_field_tag field_name, promotion_rule.preferred_minimum_quantity, class: "fullwidth", min: 1 %>
</div>
59 changes: 59 additions & 0 deletions core/app/models/spree/promotion/rules/minimum_quantity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

module Spree
class Promotion
module Rules
# Promotion rule for ensuring an order contains a minimum quantity of
# actionable items.
#
# This promotion rule is only compatible with the "all" match policy. It
# doesn't make a lot of sense to use it without that policy as it reduces
# it to a simple quantity check across the entire order which would be
# better served by an item total rule.
class MinimumQuantity < PromotionRule
validates :preferred_minimum_quantity, numericality: { only_integer: true, greater_than: 0 }

preference :minimum_quantity, :integer, default: 1

# What type of objects we should run our eligiblity checks against. In
# this case, our rule only applies to an entire order.
#
# @param promotable [Spree::Order,Spree::LineItem]
# @return [Boolean] true if promotable is a Spree::Order, false
# otherwise
def applicable?(promotable)
promotable.is_a?(Spree::Order)
end

# Will look at all of the "actionable" line items in the order and
# determine if the sum of their quantity is greater than the minimum.
#
# "Actionable" items are ones where they pass the "actionable?" check of
# all rules on the promotion. (e.g.: Match product/taxon when one of
# those rules is present.)
#
# When false is returned, the reason will be included in the
# `eligibility_errors` object.
#
# @param order [Spree::Order] the order we want to check eligibility on
# @param _options [Hash] ignored
# @return [Boolean] true if promotion is eligible, false otherwise
def eligible?(order, _options = {})
actionable_line_items = order.line_items.select do |line_item|
promotion.rules.all? { _1.actionable?(line_item) }
end

if actionable_line_items.sum(&:quantity) < preferred_minimum_quantity
eligibility_errors.add(
:base,
eligibility_error_message(:quantity_less_than_minimum, count: preferred_minimum_quantity),
error_code: :quantity_less_than_minimum
)
end

eligibility_errors.empty?
end
end
end
end
end
6 changes: 6 additions & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ en:
description: Order total meets these criteria
spree/promotion/rules/landing_page:
description: Customer must have visited the specified page
spree/promotion/rules/minimum_quantity:
description: Order contains minimum quantity of applicable items
spree/promotion/rules/nth_order:
description: Apply a promotion to every nth order a user has completed.
form_text: 'Apply this promotion on the users Nth order: '
Expand Down Expand Up @@ -654,6 +656,7 @@ en:
spree/promotion/rules/first_repeat_purchase_since: First Repeat Purchase Since
spree/promotion/rules/item_total: Item Total
spree/promotion/rules/landing_page: Landing Page
spree/promotion/rules/minimum_quantity: Minimum Quantity
spree/promotion/rules/nth_order: Nth Order
spree/promotion/rules/one_use_per_user: One Use Per User
spree/promotion/rules/option_value: Option Value(s)
Expand Down Expand Up @@ -1535,6 +1538,9 @@ en:
no_user_or_email_specified: You need to login or provide your email before applying this coupon code.
no_user_specified: You need to login before applying this coupon code.
not_first_order: This coupon code can only be applied to your first order.
quantity_less_than_minimum:
one: You need to add a least 1 applicable item to your order.
other: You need to add a least %{count} applicable items to your order.
email: Email
empty: Empty
empty_cart: Empty Cart
Expand Down
1 change: 1 addition & 0 deletions core/lib/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ def environment
Spree::Promotion::Rules::UserLoggedIn
Spree::Promotion::Rules::OneUsePerUser
Spree::Promotion::Rules::Taxon
Spree::Promotion::Rules::MinimumQuantity
Spree::Promotion::Rules::NthOrder
Spree::Promotion::Rules::OptionValue
Spree::Promotion::Rules::FirstRepeatPurchaseSince
Expand Down
Loading
Loading