From 0948ce47ed73b568b61d70c237de9a4d0a9f560e Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Wed, 16 Mar 2022 09:53:08 +0100 Subject: [PATCH 1/2] Use EnterprisesHelper to retrieve shipping methods that apply tag filtering on shipping methods --- app/controllers/concerns/checkout_callbacks.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/controllers/concerns/checkout_callbacks.rb b/app/controllers/concerns/checkout_callbacks.rb index af87da469f..acc168251e 100644 --- a/app/controllers/concerns/checkout_callbacks.rb +++ b/app/controllers/concerns/checkout_callbacks.rb @@ -2,6 +2,7 @@ module CheckoutCallbacks extend ActiveSupport::Concern + include EnterprisesHelper included do # We need pessimistic locking to avoid race conditions. @@ -46,10 +47,7 @@ module CheckoutCallbacks end def load_shipping_methods - @shipping_methods = Spree::ShippingMethod. - for_distributor(@order.distributor). - display_on_checkout. - order(:name) + @shipping_methods = available_shipping_methods end def redirect_to_shop? From c9e40d084cca536c30a24c8be7dadcaabae2f270 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Wed, 16 Mar 2022 11:14:02 +0100 Subject: [PATCH 2/2] Add spec around shipping methods filter for a guest + logged user --- spec/system/consumer/split_checkout_spec.rb | 58 ++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/spec/system/consumer/split_checkout_spec.rb b/spec/system/consumer/split_checkout_spec.rb index be816b5ae3..4f3c314e1b 100644 --- a/spec/system/consumer/split_checkout_spec.rb +++ b/spec/system/consumer/split_checkout_spec.rb @@ -49,6 +49,9 @@ describe "As a consumer, I want to checkout my order", js: true do let(:free_shipping_without_required_address) { create(:shipping_method, require_ship_address: false, name: "Z Free Shipping without required address") } + let(:tagged_shipping) { + create(:shipping_method, require_ship_address: false, name: "Local", tag_list: "local") + } let!(:payment_with_fee) { create(:payment_method, distributors: [distributor], name: "Payment with Fee", description: "Payment with fee", @@ -65,7 +68,7 @@ describe "As a consumer, I want to checkout my order", js: true do add_enterprise_fee enterprise_fee set_order order - distributor.shipping_methods.push(free_shipping_with_required_address, free_shipping, shipping_with_fee, free_shipping_without_required_address) + distributor.shipping_methods.push(free_shipping_with_required_address, free_shipping, shipping_with_fee, free_shipping_without_required_address, tagged_shipping) end context "guest checkout when distributor doesn't allow guest orders" do @@ -183,6 +186,59 @@ describe "As a consumer, I want to checkout my order", js: true do it_behaves_like "when I have an out of stock product in my cart" end + + describe "hidding a shipping method" do + let(:user) { create(:user) } + let(:customer) { create(:customer, user: user, enterprise: distributor) } + + it "shows shipping methods allowed by the rule" do + visit checkout_path + click_on "Checkout as guest" + + # No rules in effect + expect(page).to have_content free_shipping.name + expect(page).to have_content shipping_with_fee.name + expect(page).to have_content free_shipping_without_required_address.name + expect(page).to have_content tagged_shipping.name + + create(:filter_shipping_methods_tag_rule, + enterprise: distributor, + preferred_customer_tags: "local", + preferred_shipping_method_tags: "local", + preferred_matched_shipping_methods_visibility: 'visible') + create(:filter_shipping_methods_tag_rule, + enterprise: distributor, + is_default: true, + preferred_shipping_method_tags: "local", + preferred_matched_shipping_methods_visibility: 'hidden') + + visit checkout_path + + # Default rule in effect, disallows access to 'Local' + expect(page).to have_content free_shipping.name + expect(page).to have_content shipping_with_fee.name + expect(page).to have_content free_shipping_without_required_address.name + expect(page).not_to have_content tagged_shipping.name + + login_as(user) + visit checkout_path + + # Default rule in still effect, disallows access to 'Local' + expect(page).to have_content free_shipping.name + expect(page).to have_content shipping_with_fee.name + expect(page).to have_content free_shipping_without_required_address.name + expect(page).not_to have_content tagged_shipping.name + + customer.update_attribute(:tag_list, "local") + visit checkout_path + + # #local Customer can access 'Local' shipping method + expect(page).to have_content free_shipping.name + expect(page).to have_content shipping_with_fee.name + expect(page).to have_content free_shipping_without_required_address.name + expect(page).to have_content tagged_shipping.name + end + end end context "as a logged in user" do