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.

Update storefront for open sourcing analytics (#12695)

Łukasz Adamczykd8ff4cef7ac8
diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb
index 64c3d438d9..12127995e3 100644
--- a/core/lib/spree/core.rb
+++ b/core/lib/spree/core.rb
@@ -31,6 +31,7 @@ require 'request_store'
require 'wannabe_bool'
require 'geocoder'
require 'oembed'
+require 'safely_block'
# This is required because ActiveModel::Validations#invalid? conflicts with the
# invalid state of a Payment. In the future this should be removed.
diff --git a/core/spree_core.gemspec b/core/spree_core.gemspec
index 89cc7d4efd..66a7b9a58f 100644
--- a/core/spree_core.gemspec
+++ b/core/spree_core.gemspec
@@ -63,4 +63,5 @@ Gem::Specification.new do |s|
s.add_dependency 'request_store', '~> 1.7'
s.add_dependency 'wannabe_bool'
s.add_dependency 'any_ascii', '~> 0.3.2'
+ s.add_dependency 'safely_block', '~> 0.4'
end
diff --git a/storefront/app/controllers/spree/checkout_controller.rb b/storefront/app/controllers/spree/checkout_controller.rb
index 3589428212..c9cd1a9656 100644
--- a/storefront/app/controllers/spree/checkout_controller.rb
+++ b/storefront/app/controllers/spree/checkout_controller.rb
@@ -14,6 +14,7 @@ module Spree
before_action :ensure_order_not_completed, only: [:edit, :update]
before_action :ensure_checkout_allowed
+ before_action :check_if_checkout_started, only: :edit
before_action :ensure_valid_state, only: [:edit, :update]
before_action :restart_checkout, only: :edit, if: :should_restart_checkout?
@@ -33,13 +34,6 @@ module Spree
# GET /checkout/<token>
def edit
- if checkout_started?
- track_checkout_started
-
- @order.accept_marketing = true # TODO: move this to store preferences
- @order.signup_for_an_account = true # TODO: move this to store preferences
- end
-
track_checkout_step_viewed
end
@@ -242,6 +236,15 @@ module Spree
end
end
+ def check_if_checkout_started
+ if checkout_started?
+ track_checkout_started
+
+ @order.accept_marketing = true # TODO: move this to store preferences
+ @order.signup_for_an_account = true # TODO: move this to store preferences
+ end
+ end
+
def ensure_valid_state
redirect_to_state(correct_state) if @order.state != correct_state && !skip_state_validation?
end
diff --git a/storefront/app/helpers/spree/checkout_helper.rb b/storefront/app/helpers/spree/checkout_helper.rb
index 8ba309815b..a7dc9ee1b1 100644
--- a/storefront/app/helpers/spree/checkout_helper.rb
+++ b/storefront/app/helpers/spree/checkout_helper.rb
@@ -33,7 +33,7 @@ module Spree
end
def checkout_started?
- @order.state == 'address' && @order.state_was == 'cart'
+ (@order.state == 'address' && @order.state_was == 'cart') || (@order.state == 'cart' && correct_state == 'address')
end
def already_have_an_account?
diff --git a/storefront/app/helpers/spree/products_helper.rb b/storefront/app/helpers/spree/products_helper.rb
index 22e6b16320..5b3e2bdbc4 100644
--- a/storefront/app/helpers/spree/products_helper.rb
+++ b/storefront/app/helpers/spree/products_helper.rb
@@ -25,7 +25,7 @@ module Spree
end
def brand_name(product)
- product.brand&.name || product.vendor&.display_name
+ product.brand&.name || product.try(:vendor)&.display_name
end
def product_not_selected_options(product, selected_variant, options_param_name: :options)
diff --git a/storefront/app/javascript/spree/storefront/controllers/slideover_controller.js b/storefront/app/javascript/spree/storefront/controllers/slideover_controller.js
index e7dec522bd..524a5103e2 100644
--- a/storefront/app/javascript/spree/storefront/controllers/slideover_controller.js
+++ b/storefront/app/javascript/spree/storefront/controllers/slideover_controller.js
@@ -27,6 +27,8 @@ export default class extends Slideover {
// Don't scroll the background when slideover is open
lockScroll()
+
+ window.dispatchEvent(new Event('slideover:open'))
}
_hide() {
diff --git a/storefront/app/views/spree/account/wished_items/create.turbo_stream.erb b/storefront/app/views/spree/account/wished_items/create.turbo_stream.erb
index 38b6de7e42..e3f3c39828 100644
--- a/storefront/app/views/spree/account/wished_items/create.turbo_stream.erb
+++ b/storefront/app/views/spree/account/wished_items/create.turbo_stream.erb
@@ -5,4 +5,5 @@
<%= turbo_stream.update "wishlist-icon" do %>
<%= javascript_tag "wishedVariantIds = #{(current_wishlist.variant_ids).to_json.html_safe};" %>
<%= render 'spree/shared/wishlist_icon' %>
+ <%= render_storefront_partials(:add_to_wishlist_partials) %>
<% end %>
diff --git a/storefront/app/views/spree/line_items/create.turbo_stream.erb b/storefront/app/views/spree/line_items/create.turbo_stream.erb
index 6e6231562f..16ef32bf60 100644
--- a/storefront/app/views/spree/line_items/create.turbo_stream.erb
+++ b/storefront/app/views/spree/line_items/create.turbo_stream.erb
@@ -6,6 +6,7 @@
<%= turbo_stream.replace_all '.cart-contents' do %>
<%= turbo_frame_tag cart_id(@order), class: 'cart-contents' do %>
<%= render 'spree/orders/cart' %>
+ <%= render_storefront_partials(:add_to_cart_partials) %>
<% end %>
<% end %>
diff --git a/storefront/app/views/spree/line_items/destroy.turbo_stream.erb b/storefront/app/views/spree/line_items/destroy.turbo_stream.erb
index 7d33d04b83..7eb5a28fc3 100644
--- a/storefront/app/views/spree/line_items/destroy.turbo_stream.erb
+++ b/storefront/app/views/spree/line_items/destroy.turbo_stream.erb
@@ -5,6 +5,8 @@
<% else %>
<%= render 'spree/orders/cart' %>
<% end %>
+
+ <%= render_storefront_partials(:remove_from_cart_partials) %>
<% end %>
<% end %>
diff --git a/storefront/app/views/spree/products/show.html.erb b/storefront/app/views/spree/products/show.html.erb
index f96cdcdd73..724c7b35f3 100644
--- a/storefront/app/views/spree/products/show.html.erb
+++ b/storefront/app/views/spree/products/show.html.erb
@@ -1 +1,2 @@
-<%= render_page(current_page_or_preview, product: @product) %>
\ No newline at end of file
+<%= render_page(current_page_or_preview, product: @product) %>
+<%= render_storefront_partials(:product_partials) %>
diff --git a/storefront/app/views/themes/default/spree/orders/edit.html.erb b/storefront/app/views/themes/default/spree/orders/edit.html.erb
index 0195c09fcb..95854e83f1 100644
--- a/storefront/app/views/themes/default/spree/orders/edit.html.erb
+++ b/storefront/app/views/themes/default/spree/orders/edit.html.erb
@@ -11,6 +11,8 @@
<% else %>
<%= render 'spree/orders/cart' %>
<% end %>
+
+ <%= render_storefront_partials(:cart_partials) %>
<% end %>
<% if !turbo_frame_request? && !turbo_stream_request? %>
diff --git a/storefront/lib/spree/storefront/engine.rb b/storefront/lib/spree/storefront/engine.rb
index ebdfe20c0d..0699f7bcb1 100644
--- a/storefront/lib/spree/storefront/engine.rb
+++ b/storefront/lib/spree/storefront/engine.rb
@@ -7,11 +7,14 @@ module Spree
:head_partials,
:body_start_partials,
:body_end_partials,
+ :cart_partials,
:add_to_cart_partials,
:remove_from_cart_partials,
:checkout_partials,
:checkout_complete_partials,
- :quick_checkout_partials
+ :quick_checkout_partials,
+ :product_partials,
+ :add_to_wishlist_partials
)
# accessible via Rails.application.config.spree_storefront
@@ -44,11 +47,14 @@ module Spree
Rails.application.config.spree_storefront.head_partials = []
Rails.application.config.spree_storefront.body_start_partials = []
Rails.application.config.spree_storefront.body_end_partials = []
+ Rails.application.config.spree_storefront.cart_partials = []
Rails.application.config.spree_storefront.add_to_cart_partials = []
Rails.application.config.spree_storefront.remove_from_cart_partials = []
Rails.application.config.spree_storefront.checkout_partials = []
Rails.application.config.spree_storefront.checkout_complete_partials = []
Rails.application.config.spree_storefront.quick_checkout_partials = []
+ Rails.application.config.spree_storefront.product_partials = []
+ Rails.application.config.spree_storefront.add_to_wishlist_partials = []
end
end
end
diff --git a/storefront/spec/controllers/spree/checkout_controller_spec.rb b/storefront/spec/controllers/spree/checkout_controller_spec.rb
index 87297030ad..618f14cd4b 100644
--- a/storefront/spec/controllers/spree/checkout_controller_spec.rb
+++ b/storefront/spec/controllers/spree/checkout_controller_spec.rb
@@ -65,6 +65,9 @@ describe Spree::CheckoutController, type: :controller do
expect(controller).to receive(:track_event).with('checkout_started', { order: order })
expect(controller).to receive(:track_event).with('checkout_step_viewed', { order: order, step: 'address' })
get :edit, params: { token: order.token, state: 'address' }
+
+ expect(assigns(:order).accept_marketing).to be(true)
+ expect(assigns(:order).signup_for_an_account).to be(true)
end
context 'when user is not signed in' do