Skip to content

Commit

Permalink
Merge pull request #2327 from nebulab/coupon-code-into-summary
Browse files Browse the repository at this point in the history
Move checkout coupon code section into summary
  • Loading branch information
kennyadsl authored May 25, 2018
2 parents 146c69d + 212287a commit 4857815
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Spree.onCouponCodeApply = function(e) {
e.preventDefault();
var couponCodeField = $("#order_coupon_code");
var couponCode = $.trim(couponCodeField.val());
if (couponCode === "") {
Expand Down
28 changes: 27 additions & 1 deletion frontend/app/assets/stylesheets/spree/frontend/screen.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -948,9 +948,35 @@ p[data-hook="use_billing"] {
padding: 5px;
}

.coupon-code {
margin-top: 20px;
padding: 10px;

form {
display: flex;
flex-flow: row wrap;
}

label {
flex: 1 100%;
text-align: left;
}

input[type="text"] {
flex: 3 0;
width: 100%;
margin-right: 5px;
}

&-apply-button {
white-space: nowrap;
}
}

#coupon_status {
margin-top: 10px;
font-weight: bold;
font-size: 125%;
font-size: 100%;
&.success {
color: $c_green;
}
Expand Down
17 changes: 17 additions & 0 deletions frontend/app/controllers/spree/checkout_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ def completion_route
spree.order_path(@order)
end

def apply_coupon_code
if update_params[:coupon_code].present?
@order.coupon_code = update_params[:coupon_code]

handler = PromotionHandler::Coupon.new(@order).apply

if handler.error.present?
flash.now[:error] = handler.error
elsif handler.success
flash[:success] = handler.success
end

setup_for_current_state
respond_with(@order) { |format| format.html { render :edit } } && return
end
end

def setup_for_current_state
method_name = :"before_#{@order.state}"
send(method_name) if respond_to?(method_name, true)
Expand Down
15 changes: 15 additions & 0 deletions frontend/app/controllers/spree/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,20 @@ def assign_order
redirect_to(root_path) && return
end
end

def apply_coupon_code
if order_params[:coupon_code].present?
@order.coupon_code = order_params[:coupon_code]

handler = PromotionHandler::Coupon.new(@order).apply

if handler.error.present?
flash.now[:error] = handler.error
respond_with(@order) { |format| format.html { render :edit } } && return
elsif handler.success
flash[:success] = handler.success
end
end
end
end
end
18 changes: 0 additions & 18 deletions frontend/app/controllers/spree/store_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,6 @@ def cart_link

private

# This method is placed here so that the CheckoutController
# and OrdersController can both reference it (or any other controller
# which needs it)
def apply_coupon_code
if params[:order] && params[:order][:coupon_code]
@order.coupon_code = params[:order][:coupon_code]

handler = PromotionHandler::Coupon.new(@order).apply

if handler.error.present?
flash.now[:error] = handler.error
respond_with(@order) { |format| format.html { render :edit } } && return
elsif handler.success
flash[:success] = handler.success
end
end
end

def config_locale
Spree::Frontend::Config[:locale]
end
Expand Down
12 changes: 12 additions & 0 deletions frontend/app/views/spree/checkout/_coupon_code.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="coupon-code" data-hook='coupon_code'>
<%= form_for order, url: update_checkout_path(order.state) do |form| %>
<%= form.label :coupon_code %>
<%= form.text_field :coupon_code, placeholder: :coupon_code %>

<button type="submit" class="button coupon-code-apply-button" id="coupon-code-apply-button">
<%= t('spree.apply_code') %>
</button>
<% end %>

<div id='coupon_status'></div>
</div>
10 changes: 0 additions & 10 deletions frontend/app/views/spree/checkout/_payment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,6 @@
<% end %>
</ul>
<br style="clear:both;" />
<p class='field' data-hook='coupon_code'>
<%= form.label :coupon_code %>
<%= form.text_field :coupon_code %>
<button type="button" class="button" id="coupon-code-apply-button">
<%= t('spree.apply_code') %>
</button>

</p>
<div id='coupon_status'></div>

</div>
</fieldset>

Expand Down
4 changes: 4 additions & 0 deletions frontend/app/views/spree/checkout/_summary.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@
</tr>
</tbody>
</table>

<% if order.state == 'payment' %>
<%= render 'coupon_code', order: order %>
<% end %>
58 changes: 58 additions & 0 deletions frontend/spec/controllers/spree/checkout_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,62 @@ def post_address
post :update, params: { state: "payment" }
}.to change { order.line_items.to_a.size }.from(1).to(0)
end

context 'trying to apply a coupon code' do
let(:order) { create(:order_with_line_items, state: 'payment', guest_token: 'a token') }
let(:coupon_code) { "coupon_code" }

before { cookies.signed[:guest_token] = order.guest_token }

context "when coupon code is empty" do
let(:coupon_code) { "" }

it 'does not try to apply coupon code' do
expect(Spree::PromotionHandler::Coupon).not_to receive :new

put :update, params: { state: order.state, order: { coupon_code: coupon_code } }

expect(response).to redirect_to(spree.checkout_state_path('confirm'))
end
end

context "when coupon code is applied" do
let(:promotion_handler) { instance_double('Spree::PromotionHandler::Coupon', error: nil, success: 'Coupon Applied!') }

it "continues checkout flow normally" do
expect(Spree::PromotionHandler::Coupon)
.to receive_message_chain(:new, :apply)
.and_return(promotion_handler)

put :update, params: { state: order.state, order: { coupon_code: coupon_code } }

expect(response).to render_template :edit
expect(flash.now[:success]).to eq('Coupon Applied!')
end

context "when coupon code is not applied" do
let(:promotion_handler) { instance_double('Spree::PromotionHandler::Coupon', error: 'Some error', success: false) }

it "setups the current step correctly before rendering" do
expect(Spree::PromotionHandler::Coupon)
.to receive_message_chain(:new, :apply)
.and_return(promotion_handler)
expect(controller).to receive(:setup_for_current_state)

put :update, params: { state: order.state, order: { coupon_code: coupon_code } }
end

it "render cart with coupon error" do
expect(Spree::PromotionHandler::Coupon)
.to receive_message_chain(:new, :apply)
.and_return(promotion_handler)

put :update, params: { state: order.state, order: { coupon_code: coupon_code } }

expect(response).to render_template :edit
expect(flash.now[:error]).to eq('Some error')
end
end
end
end
end
47 changes: 47 additions & 0 deletions frontend/spec/controllers/spree/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,53 @@
put :update, params: { checkout: true }
expect(response).to redirect_to checkout_state_path('address')
end

context 'trying to apply a coupon code' do
let(:order) { create(:order_with_line_items, state: 'cart') }
let(:coupon_code) { "coupon_code" }

context "when coupon code is empty" do
let(:coupon_code) { "" }

it 'does not try to apply coupon code' do
expect(Spree::PromotionHandler::Coupon).not_to receive :new

put :update, params: { state: order.state, order: { coupon_code: coupon_code } }

expect(response).to redirect_to(spree.cart_path)
end
end

context "when coupon code is applied" do
let(:promotion_handler) { instance_double('Spree::PromotionHandler::Coupon', error: nil, success: 'Coupon Applied!') }

it "continues checkout flow normally" do
expect(Spree::PromotionHandler::Coupon)
.to receive_message_chain(:new, :apply)
.and_return(promotion_handler)

put :update, params: { state: order.state, order: { coupon_code: coupon_code } }

expect(response).to redirect_to(spree.cart_path)
expect(flash.now[:success]).to eq('Coupon Applied!')
end

context "when coupon code is not applied" do
let(:promotion_handler) { instance_double('Spree::PromotionHandler::Coupon', error: 'Some error', success: false) }

it "render cart with coupon error" do
expect(Spree::PromotionHandler::Coupon)
.to receive_message_chain(:new, :apply)
.and_return(promotion_handler)

put :update, params: { state: order.state, order: { coupon_code: coupon_code } }

expect(response).to render_template :edit
expect(flash.now[:error]).to eq('Some error')
end
end
end
end
end
end

Expand Down
Loading

0 comments on commit 4857815

Please sign in to comment.