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.

Storefront: open source wishlists (pt2)

Damian Legawiecb4ac5c30519d
diff --git a/storefront/app/controllers/concerns/spree/theme_concern.rb b/storefront/app/controllers/concerns/spree/theme_concern.rb
index 9465971ea0..caae6b8e09 100644
--- a/storefront/app/controllers/concerns/spree/theme_concern.rb
+++ b/storefront/app/controllers/concerns/spree/theme_concern.rb
@@ -5,6 +5,8 @@ module Spree
included do
helper 'spree/theme'
include Spree::ThemeHelper
+
+ prepend_before_action :set_theme_view_paths
end
def default_url_options
@@ -16,5 +18,12 @@ module Spree
super
end
end
+
+ def set_theme_view_paths
+ # add default Spree theme
+ prepend_view_path [Spree::Storefront::Engine.root, 'app/views/themes/default'].join('/')
+ # add current theme
+ prepend_view_path "app/views/themes/#{current_theme.class.name.demodulize.underscore}"
+ end
end
end
diff --git a/storefront/app/controllers/spree/account/base_controller.rb b/storefront/app/controllers/spree/account/base_controller.rb
index 68c6611773..a1b762bf9a 100644
--- a/storefront/app/controllers/spree/account/base_controller.rb
+++ b/storefront/app/controllers/spree/account/base_controller.rb
@@ -1,6 +1,11 @@
module Spree
module Account
class BaseController < Spree::UsersController
+ protected
+
+ def accurate_title
+ Spree.t(:my_account)
+ end
end
end
end
diff --git a/storefront/app/controllers/spree/store_controller.rb b/storefront/app/controllers/spree/store_controller.rb
index 9c4c007be6..760aca7947 100644
--- a/storefront/app/controllers/spree/store_controller.rb
+++ b/storefront/app/controllers/spree/store_controller.rb
@@ -30,7 +30,6 @@ module Spree
helper_method :turbo_frame_request?, :turbo_stream_request?
- prepend_before_action :set_theme_view_paths
before_action :redirect_to_default_locale
before_action :render_404_if_store_not_exists
rescue_from ActionController::InvalidAuthenticityToken, with: :invalid_authenticity_token
@@ -92,13 +91,6 @@ module Spree
protected
- def set_theme_view_paths
- # add default Spree theme
- prepend_view_path [Spree::Storefront::Engine.root, 'app/views/themes/default'].join('/')
- # add current theme
- prepend_view_path "app/views/themes/#{current_theme.class.name.demodulize.underscore}"
- end
-
def choose_layout
if turbo_frame_request? && !current_theme_preview.present?
'turbo_rails/frame'
diff --git a/storefront/app/controllers/spree/wishlists_controller.rb b/storefront/app/controllers/spree/wishlists_controller.rb
index c7c7e8698d..854b19d0f4 100644
--- a/storefront/app/controllers/spree/wishlists_controller.rb
+++ b/storefront/app/controllers/spree/wishlists_controller.rb
@@ -2,7 +2,7 @@ module Spree
class WishlistsController < StoreController
def show
if params[:id].present? && params[:token].present?
- @wishlist = Spree::Wishlist.find_by!(id: params[:id], token: params[:token])
+ @wishlist = current_store.wishlists.find_by!(id: params[:id], token: params[:token])
elsif try_spree_current_user
# https://github.com/spree/spree/blob/9475c6633b762669ee0c8f1f8a4d73e1c221a94e/core/app/models/concerns/spree/user_methods.rb#L71
@wishlist = try_spree_current_user.default_wishlist_for_store(current_store)
diff --git a/storefront/app/views/themes/default/spree/account/_account_nav.html.erb b/storefront/app/views/themes/default/spree/account/_account_nav.html.erb
index 394e4c8ff2..31993313d2 100644
--- a/storefront/app/views/themes/default/spree/account/_account_nav.html.erb
+++ b/storefront/app/views/themes/default/spree/account/_account_nav.html.erb
@@ -7,41 +7,40 @@
>
<%= link_to "#{Spree.t(:orders)} & #{Spree.t(:returns)}", spree.account_orders_path,
data: { active: current == "orders" },
- class: "shrink-0 block uppercase lg:px-4 lg:py-3 px-3 py-4 border-b lg:border-b-0 text-neutral-700 hover:border-accent-100 hover:bg-accent-100 hover:text-black #{current == "orders" ? "lg:bg-accent border-text lg:border-b-0 lg:border-l-[1.5px]" : "border-accrent lg:border-l"}"
+ class: "shrink-0 block lg:px-4 lg:py-3 px-3 py-4 border-b lg:border-b-0 text-neutral-700 hover:border-accent-100 hover:bg-accent-100 hover:text-black #{current == "orders" ? "lg:bg-accent border-text lg:border-b-0 lg:border-l-[1.5px]" : "border-accrent lg:border-l"}"
%>
<%= link_to Spree.t(:addresses), spree.account_addresses_path,
data: { active: current == "address" },
- class: "shrink-0 block uppercase lg:px-4 lg:py-3 px-3 py-4 border-b lg:border-b-0 text-neutral-700 hover:border-accent-100 hover:bg-accent-100 hover:text-black #{current == "address" ? "lg:bg-accent border-text lg:border-b-0 lg:border-l-[1.5px]" : "border-accrent lg:border-l"}"
+ class: "shrink-0 block lg:px-4 lg:py-3 px-3 py-4 border-b lg:border-b-0 text-neutral-700 hover:border-accent-100 hover:bg-accent-100 hover:text-black #{current == "address" ? "lg:bg-accent border-text lg:border-b-0 lg:border-l-[1.5px]" : "border-accrent lg:border-l"}"
%>
<%= link_to Spree.t(:personal_details), spree.edit_account_profile_path,
data: { active: current == "account" },
- class: "shrink-0 block uppercase lg:px-4 lg:py-3 px-3 py-4 border-b lg:border-b-0 text-neutral-700 hover:border-accent-100 hover:bg-accent-100 hover:text-black #{current == "account" ? "lg:bg-accent border-text lg:border-b-0 lg:border-l-[1.5px]" : "border-accrent lg:border-l"}"
+ class: "shrink-0 block lg:px-4 lg:py-3 px-3 py-4 border-b lg:border-b-0 text-neutral-700 hover:border-accent-100 hover:bg-accent-100 hover:text-black #{current == "account" ? "lg:bg-accent border-text lg:border-b-0 lg:border-l-[1.5px]" : "border-accrent lg:border-l"}"
%>
<%= link_to Spree.t(:wishlist), spree.account_wishlist_path,
data: { active: current == "wishlist" },
- class: "shrink-0 block uppercase lg:px-4 lg:py-3 px-3 py-4 border-b lg:border-b-0 text-neutral-700 hover:border-accent-100 hover:bg-accent-100 hover:text-black #{current == "wishlist" ? "lg:bg-accent border-text lg:border-b-0 lg:border-l-[1.5px]" : "border-accrent lg:border-l"}"
+ class: "shrink-0 block lg:px-4 lg:py-3 px-3 py-4 border-b lg:border-b-0 text-neutral-700 hover:border-accent-100 hover:bg-accent-100 hover:text-black #{current == "wishlist" ? "lg:bg-accent border-text lg:border-b-0 lg:border-l-[1.5px]" : "border-accrent lg:border-l"}"
%>
<% if spree.respond_to?(:account_gift_cards_path) %>
<%= link_to Spree.t(:gift_cards), spree.account_gift_cards_path,
data: { active: current == "gift_cards" },
- class: "shrink-0 block uppercase lg:px-4 lg:py-3 px-3 py-4 border-b lg:border-b-0 text-neutral-700 hover:border-accent-100 hover:bg-accent-100 hover:text-black #{current == "gift_cards" ? "lg:bg-accent border-text lg:border-b-0 lg:border-l-[1.5px]" : "border-accrent lg:border-l"}"
+ class: "shrink-0 block lg:px-4 lg:py-3 px-3 py-4 border-b lg:border-b-0 text-neutral-700 hover:border-accent-100 hover:bg-accent-100 hover:text-black #{current == "gift_cards" ? "lg:bg-accent border-text lg:border-b-0 lg:border-l-[1.5px]" : "border-accrent lg:border-l"}"
%>
<% end %>
-<%= link_to Spree.t(:store_credits), spree.account_store_credits_path,
+ <%= link_to Spree.t(:store_credits), spree.account_store_credits_path,
data: { active: current == "gift_cards" },
- class: "shrink-0 block uppercase lg:px-4 lg:py-3 px-3 py-4 border-b lg:border-b-0 text-neutral-700 hover:border-accent-100 hover:bg-accent-100 hover:text-black #{current == "store_credits" ? "lg:bg-accent border-text lg:border-b-0 lg:border-l-[1.5px]" : "border-accrent lg:border-l"}"
+ class: "shrink-0 block lg:px-4 lg:py-3 px-3 py-4 border-b lg:border-b-0 text-neutral-700 hover:border-accent-100 hover:bg-accent-100 hover:text-black #{current == "store_credits" ? "lg:bg-accent border-text lg:border-b-0 lg:border-l-[1.5px]" : "border-accrent lg:border-l"}"
%>
-<%= link_to Spree.t('storefront.account.newsletter_settings'), spree.edit_account_newsletter_path,
+ <%= link_to Spree.t('storefront.account.newsletter_settings'), spree.edit_account_newsletter_path,
data: { active: current == "newsletter" },
- class: "shrink-0 block uppercase lg:px-4 lg:py-3 px-3 py-4 border-b lg:border-b-0 text-neutral-700 hover:border-accent-100 hover:bg-accent-100 hover:text-black #{current == "newsletter" ? "lg:bg-accent border-text lg:border-b-0 lg:border-l-[1.5px]" : "border-accrent lg:border-l"}"
+ class: "shrink-0 block lg:px-4 lg:py-3 px-3 py-4 border-b lg:border-b-0 text-neutral-700 hover:border-accent-100 hover:bg-accent-100 hover:text-black #{current == "newsletter" ? "lg:bg-accent border-text lg:border-b-0 lg:border-l-[1.5px]" : "border-accrent lg:border-l"}"
%>
-<%= button_to Spree.t(:logout), spree_logout_path,
- class: "w-full text-left hidden shrink-0 md:block uppercase lg:px-4 lg:py-3 px-3 py-4 border-b lg:border-b-0 border-accrent lg:border-l text-neutral-700 hover:border-accent-100 hover:bg-accent-100 hover:text-black",
+ <%= button_to Spree.t(:logout), spree_logout_path,
+ class: "w-full text-left hidden shrink-0 md:block lg:px-4 lg:py-3 px-3 py-4 border-b lg:border-b-0 border-accrent lg:border-l text-neutral-700 hover:border-accent-100 hover:bg-accent-100 hover:text-black",
method: :delete,
- data: { turbo: false }
- %>
+ data: { turbo: false } if defined?(spree_logout_path) %>
</div>
diff --git a/storefront/app/views/themes/default/spree/account/addresses/index.html.erb b/storefront/app/views/themes/default/spree/account/addresses/index.html.erb
index afe9a06848..9a79cdf1b1 100644
--- a/storefront/app/views/themes/default/spree/account/addresses/index.html.erb
+++ b/storefront/app/views/themes/default/spree/account/addresses/index.html.erb
@@ -4,7 +4,7 @@
</div>
<div class="lg:col-span-8 lg:col-start-5">
<div class="mb-12 lg:mb-24">
- <div class="uppercase font-medium mb-4">
+ <div class="font-medium mb-4">
<%= Spree.t(:addresses) %>
</div>
<% if @addresses.any? %>
diff --git a/storefront/app/views/themes/default/spree/account/profile/edit.html.erb b/storefront/app/views/themes/default/spree/account/profile/edit.html.erb
index fc57102b62..fc84ba4378 100644
--- a/storefront/app/views/themes/default/spree/account/profile/edit.html.erb
+++ b/storefront/app/views/themes/default/spree/account/profile/edit.html.erb
@@ -2,7 +2,7 @@
<div class="lg:col-span-3">
<%= render 'spree/account/account_nav', current: 'account' %>
</div>
- <%= turbo_frame_tag :account_form, class: "lg:col-span-8 lg:col-start-5 pb-6" do %>
+ <div class="lg:col-span-8 lg:col-start-5 pb-6">
<h5 class="uppercase font-medium mb-4"><%= Spree.t('storefront.account.personal_details') %></h5>
<div class="flex justify-between">
<p class="mb-3 text-neutral-600">
@@ -16,11 +16,11 @@
<%= form_for @user, url: spree.account_profile_path, html: {class: "flex flex-col gap-4"}, method: :put do |f| %>
<div>
<%= f.label :first_name, Spree.t(:first_name), class: 'text-xs text-neutral-600 block mb-2' %>
- <%= f.text_field :first_name, class: 'text-input w-full', placeholder: Spree.t(:first_name) %>
+ <%= f.text_field :first_name, class: 'text-input w-full', placeholder: Spree.t(:first_name), required: true %>
</div>
<div>
<%= f.label :last_name, Spree.t(:last_name), class: 'text-xs text-neutral-600 block mb-2' %>
- <%= f.text_field :last_name, class: 'text-input w-full', placeholder: Spree.t(:last_name) %>
+ <%= f.text_field :last_name, class: 'text-input w-full', placeholder: Spree.t(:last_name), required: true %>
</div>
<div>
<%= f.label :phone, Spree.t(:phone), class: 'text-xs text-neutral-600 block mb-2' %>
@@ -28,9 +28,9 @@
</div>
<div>
<%= f.label :email, Spree.t(:email), class: 'text-xs text-neutral-600 block mb-2' %>
- <%= f.text_field :email, class: 'text-input w-full', placeholder: Spree.t(:email) %>
+ <%= f.text_field :email, class: 'text-input w-full', placeholder: Spree.t(:email), required: true %>
</div>
<%= f.submit Spree.t(:update), class: 'btn-primary w-full mb-6' %>
<% end %>
- <% end %>
+ </div>
</div>
diff --git a/storefront/app/views/themes/default/spree/products/_featured_image.html.erb b/storefront/app/views/themes/default/spree/products/_featured_image.html.erb
new file mode 100644
index 0000000000..bc0faea883
--- /dev/null
+++ b/storefront/app/views/themes/default/spree/products/_featured_image.html.erb
@@ -0,0 +1,37 @@
+<% with_cover_image ||= true %>
+<% object ||= product %>
+
+<% if object.default_image.present? && object.default_image.attached? %>
+ <% height ||= theme_setting('product_listing_image_height') %>
+ <% width ||= theme_setting('product_listing_image_width') %>
+
+ <figure class="group <%= 'aspect-1' if height == width %> card-product-featured-images-container bg-transparent h-full">
+ <% if with_cover_image %>
+ <% object_class = 'object-cover object-center' %>
+ <% else %>
+ <% object_class = 'object-contain object-top' %>
+ <% end %>
+
+ <% image_variant_options = spree_image_variant_options(resize_and_pad: [width * 2, height * 2, { alpha: true }]) %>
+
+ <% image_hover_class = object.secondary_image.present? && object.secondary_image.attached? ?
+ "w-full group-hover:opacity-0 transition-opacity duration-500 z-10 relative h-full bg-background product-card !max-h-full #{object_class}" :
+ "w-full h-full !max-h-full #{object_class}" %>
+
+ <%= image_tag main_app.cdn_image_url(object.default_image.variant(image_variant_options)),
+ loading: :lazy,
+ alt: object.default_image.alt,
+ class: image_hover_class %>
+
+ <% if object.secondary_image.present? && object.secondary_image.attached? %>
+ <% secondary_image_class = "w-full absolute top-0 left-0 opacity-100 h-full !max-h-full #{object_class}" %>
+
+ <%= image_tag main_app.cdn_image_url(object.secondary_image.variant(image_variant_options)),
+ loading: :lazy,
+ alt: object.secondary_image.alt,
+ class: secondary_image_class %>
+ <% end %>
+ </figure>
+<% else %>
+ <div class="w-full aspect-1 bg-accent"></div>
+<% end %>
diff --git a/storefront/app/views/themes/default/spree/products/_label.html.erb b/storefront/app/views/themes/default/spree/products/_label.html.erb
new file mode 100644
index 0000000000..56159d03c4
--- /dev/null
+++ b/storefront/app/views/themes/default/spree/products/_label.html.erb
@@ -0,0 +1,26 @@
+<% shared_class = "label rounded-full px-3 py-1 text-sm uppercase z-10 left-2 product-label" %>
+<% mobile_pdp ||= false %>
+
+<% if mobile_pdp %>
+ <% shared_class += ' bottom-2' %>
+<% else %>
+ <% shared_class += ' top-2' %>
+<% end %>
+
+<% if !product.available? || (@variant_from_options.present? && !@variant_from_options.in_stock?) %>
+ <div class='<%= shared_class %> bg-neutral-400 text-background product-label--sold-out'>
+ <%= Spree.t(:sold_out) %>
+ </div>
+<% elsif product.on_sale?(current_currency) && !@all_options_selected %>
+ <div class='<%= shared_class %> bg-danger text-background product-label--sale'>
+ <%= Spree.t(:sale) %>
+ </div>
+<% elsif @variant_from_options&.on_sale?(current_currency) && @all_options_selected %>
+ <div class='<%= shared_class %> bg-danger text-background product-label--sale'>
+ <%= Spree.t(:sale) %>
+ </div>
+<% elsif product.taggings.find { |tagging| tagging.context == 'labels' } %>
+ <div class='<%= shared_class %> bg-background border border-primary product-label--custom' data-title="<%= product.labels.first.name.strip.downcase.truncate(20) %>">
+ <%= product.labels.first.name.truncate(20) %>
+ </div>
+<% end %>
diff --git a/storefront/app/views/themes/default/spree/products/_price.html.erb b/storefront/app/views/themes/default/spree/products/_price.html.erb
new file mode 100644
index 0000000000..0f79211152
--- /dev/null
+++ b/storefront/app/views/themes/default/spree/products/_price.html.erb
@@ -0,0 +1,59 @@
+<% use_variant ||= false %>
+<% sold_out ||= !product.available? %>
+<% price_class ||= ''%>
+<% price_container_class ||= '' %>
+<% selected_variant ||= nil %>
+<div class="<%= price_class %>">
+ <div class="flex flex-col gap-3 <%= price_container_class %>">
+ <%
+ if use_variant
+ target = selected_variant
+ sold_out = selected_variant.nil?
+ if sold_out
+ target = @variant_from_options
+ sold_out = @variant_from_options.nil?
+ end
+ else
+ target = product
+ sold_out = !product.available?
+ end
+
+ target ||= product
+
+ price = target.price_in(current_currency)
+ money_price = price.display_amount
+
+ if target.is_a?(Spree::Product) && !use_variant && product.price_varies?(current_currency)
+ price = target.lowest_price(current_currency)
+ money_price = price.display_amount
+ money_price = "#{Spree.t(:from)}: #{money_price}"
+ end
+ %>
+ <div>
+ <% if price.compare_at_amount.present? && price.compare_at_amount > price.amount %>
+ <span class="hidden">
+ <%= Spree.t(:regular_price) %>
+ </span>
+ <p class="inline pr-2 opacity-50 line-through">
+ <%= price.display_compare_at_amount %>
+ </p>
+ <span class="hidden pr-2">
+ <%= Spree.t(:sale_price) %>
+ </span>
+ <p class="inline text-danger">
+ <%= money_price %>
+ </p>
+ <% else %>
+ <span class="hidden">
+ <%= Spree.t(:regular_price) %>
+ </span>
+
+ <% if price&.amount.nil? %>
+ <span class="uppercase"><%= Spree.t(:not_available) %></span>
+ <% else %>
+ <p><%= money_price %></p>
+ <% end %>
+ <% end %>
+ </div>
+ </div>
+</div>
diff --git a/storefront/config/routes.rb b/storefront/config/routes.rb
index ba9011dcd7..47e91d2faf 100644
--- a/storefront/config/routes.rb
+++ b/storefront/config/routes.rb
@@ -11,6 +11,9 @@ Spree::Core::Engine.add_routes do
resource :wishlist, only: [:show], controller: '/spree/wishlists' do
resources :wished_items, only: [:create, :destroy]
end
+ # Wishlists
+ resources :wishlists, only: [:show] # for sharing with ID and Token
+
end
root to: 'home#index'
diff --git a/storefront/spec/controllers/spree/wishlists_controller_spec.rb b/storefront/spec/controllers/spree/wishlists_controller_spec.rb
new file mode 100644
index 0000000000..c6ea432d32
--- /dev/null
+++ b/storefront/spec/controllers/spree/wishlists_controller_spec.rb
@@ -0,0 +1,54 @@
+require 'spec_helper'
+
+describe Spree::WishlistsController, type: :controller do
+ let(:store) { Spree::Store.default }
+ let(:user) { create(:user) }
+ let(:wishlist) { create(:wishlist, user: user, store: store, is_default: true) }
+ let(:variant) { create(:variant) }
+ let!(:wished_item) { create(:wished_item, variant: variant, wishlist: wishlist) }
+
+ before do
+ allow(controller).to receive(:current_store).and_return(store)
+ allow(controller).to receive(:spree_login_path).and_return('/login')
+ end
+
+ describe '#show' do
+ context 'when wishlist id and token are provided' do
+ subject { get :show, params: { id: wishlist.id, token: wishlist.token } }
+
+ it 'finds the wishlist by id and token' do
+ subject
+ expect(assigns(:wishlist)).to eq wishlist
+ expect(response).to have_http_status(:ok)
+ end
+ end
+
+ context 'when user is logged in' do
+ before do
+ allow(controller).to receive_messages try_spree_current_user: user
+ allow(controller).to receive_messages spree_current_user: user
+ end
+
+ subject { get :show }
+
+ it 'finds the default wishlist for current user' do
+ subject
+ expect(assigns(:wishlist)).to eq wishlist
+ expect(response).to have_http_status(:ok)
+ end
+
+ it 'loads wished items' do
+ subject
+ expect(assigns(:wished_items)).to include(wished_item)
+ end
+ end
+
+ context 'when user is not logged in' do
+ subject { get :show }
+
+ it 'redirects to login page' do
+ expect(subject).to have_http_status(302)
+ end
+ end
+ end
+end