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.

Memoize order/item_count when displaying cart icon/pane

Damian Legawiec9ec9bdee2b27
diff --git a/storefront/app/views/themes/default/spree/shared/_cart_icon.html.erb b/storefront/app/views/themes/default/spree/shared/_cart_icon.html.erb
index 81a9527093..31a317a9c8 100644
--- a/storefront/app/views/themes/default/spree/shared/_cart_icon.html.erb
+++ b/storefront/app/views/themes/default/spree/shared/_cart_icon.html.erb
@@ -1,4 +1,6 @@
-<%= turbo_frame_tag "cart-icon-#{current_order&.item_count}", target: '_top' do %>
+<% item_count ||= current_order&.item_count %>
+
+<%= turbo_frame_tag "cart-icon-#{item_count}", target: '_top' do %>
<%= link_to spree.cart_path,
class: 'relative block w-6',
id: "cart-pane-link",
@@ -6,7 +8,7 @@
onclick: 'event.preventDefault()' do %>
<%= render 'spree/shared/icons/cart' %>
<span class='cart-counter w-full text-[8px] font-extrabold absolute top-[10px] text-center'>
- <%= current_order.item_count if current_order&.item_count&.positive? %>
+ <%= item_count if item_count&.positive? %>
</span>
<span class='sr-only'><%= Spree.t('storefront.cart.items_in_cart') %>, <%= Spree.t('storefront.cart.view_bag') %></span>
<% end %>
diff --git a/storefront/app/views/themes/default/spree/shared/_cart_pane.html.erb b/storefront/app/views/themes/default/spree/shared/_cart_pane.html.erb
index 3f1aecdaf3..e1cc7723a3 100644
--- a/storefront/app/views/themes/default/spree/shared/_cart_pane.html.erb
+++ b/storefront/app/views/themes/default/spree/shared/_cart_pane.html.erb
@@ -1,3 +1,6 @@
+<% order ||= current_order %>
+<% item_count ||= order&.item_count %>
+
<!-- begin sidebar/slideover -->
<div
id="cart-pane"
@@ -25,13 +28,13 @@
<span class='relative block w-6'>
<%= render 'spree/shared/icons/cart' %>
<span class='cart-counter w-full text-[8px] font-extrabold absolute top-[10px] text-center'>
- <%= current_order.item_count if current_order.present? && current_order.item_count.to_i > 0 %>
+ <%= item_count if item_count&.positive? %>
</span>
<span class='sr-only'><%= Spree.t('storefront.cart.items_in_cart') %>, <%= Spree.t('storefront.cart.view_bag') %></span>
</span>
</div>
<div class='flex-1 h-0 overflow-y-auto'>
- <%= turbo_frame_tag cart_id(current_order), class: 'cart-contents', src: spree.cart_path, data: { turbo_permanent: true } %>
+ <%= turbo_frame_tag cart_id(order), class: 'cart-contents', src: spree.cart_path, data: { turbo_permanent: true } %>
</div>
</div>
</div>