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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Open source storefront wishlists
Damian Legawiece2935978aabf
diff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex 6156da1a07..3564c33556 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -184,6 +184,8 @@ en:spree/zone:description: Descriptionname: Name+ spree/wished_item:+ variant: ""models:spree/address:one: Address@@ -400,6 +402,10 @@ en:attributes:base:cannot_destroy_if_used_in_store_credit: Cannot delete store credit categories once they are used in store credit.+ spree/wished_item:+ attributes:+ variant:+ taken: "You already added this item to your wishlist"spree/variant:attributes:base:diff --git a/storefront/app/controllers/spree/wishlists_controller.rb b/storefront/app/controllers/spree/wishlists_controller.rbnew file mode 100644index 0000000000..c7c7e8698d--- /dev/null+++ b/storefront/app/controllers/spree/wishlists_controller.rb@@ -0,0 +1,19 @@+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])+ 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)+ else+ return require_user+ end++ @wished_items = @wishlist.wished_items.includes(+ product: [:prices_including_master, :variants, :master, :taggings],+ variant: [:images, :product, { option_values: :option_type }]+ )+ end+ end+enddiff --git a/storefront/app/views/themes/default/spree/shared/_wishlist_icon.html.erb b/storefront/app/views/themes/default/spree/shared/_wishlist_icon.html.erbnew file mode 100644index 0000000000..967f8f1d26--- /dev/null+++ b/storefront/app/views/themes/default/spree/shared/_wishlist_icon.html.erb@@ -0,0 +1,13 @@+<% background_color ||= '#fff' %>+<div class="relative">+ <%= render 'spree/shared/icons/heart' %>+ <% if current_wishlist && current_wishlist.wished_items_count > 0 %>+ <span+ style="background-color: <%= background_color %>"+ class="absolute text-[0.5rem] font-extrabold leading-[0.5rem] right-[0.10rem] bottom-1 px-[0.0625rem] text-center"+ id="wishlist-icon-count"+ >+ <%= current_wishlist.wished_items_count %>+ </span>+ <% end %>+</div>diff --git a/storefront/app/views/themes/default/spree/shared/icons/_heart.html.erb b/storefront/app/views/themes/default/spree/shared/icons/_heart.html.erbnew file mode 100644index 0000000000..5a6489eab0--- /dev/null+++ b/storefront/app/views/themes/default/spree/shared/icons/_heart.html.erb@@ -0,0 +1,12 @@+<% active ||= false %>+<% size ||= 24 %>++<% if active %>+ <svg xmlns="http://www.w3.org/2000/svg" width="<%= size %>" height="<%= size %>" viewBox="0 0 24 24" fill="none">+ <path d="M13.7601 3.46195L13.7602 3.46182C14.2788 2.91653 14.8925 2.48607 15.5652 2.19312C16.2378 1.90021 16.9574 1.75 17.6831 1.75C18.4087 1.75 19.1283 1.90021 19.8009 2.19312C20.4736 2.48607 21.0873 2.91653 21.6059 3.46182L21.6062 3.46208C22.1251 4.00718 22.5386 4.6564 22.8213 5.37354C23.104 6.09072 23.25 6.86062 23.25 7.63895C23.25 8.41727 23.104 9.18717 22.8213 9.90436C22.5386 10.6215 22.1251 11.2707 21.6062 11.8158L21.606 11.8159L20.389 13.095L11.9997 21.912L3.6104 13.095L2.39335 11.8159C1.34505 10.7142 0.75 9.21232 0.75 7.63895C0.75 6.06557 1.34505 4.56369 2.39335 3.46195C3.44045 2.36148 4.85242 1.75065 6.31633 1.75065C7.78024 1.75065 9.19221 2.36148 10.2393 3.46195L11.4564 4.74103L11.9997 5.31207L12.543 4.74103L13.7601 3.46195Z" fill="currentColor" stroke="currentColor" stroke-width="1.5"/>+ </svg>+<% else %>+ <svg xmlns="http://www.w3.org/2000/svg" width="<%= size %>" height="<%= size %>" viewBox="0 0 24 24" fill="none">+ <path d="M13.7601 3.46195L13.7602 3.46182C14.2788 2.91653 14.8925 2.48607 15.5652 2.19312C16.2378 1.90021 16.9574 1.75 17.6831 1.75C18.4087 1.75 19.1283 1.90021 19.8009 2.19312C20.4736 2.48607 21.0873 2.91653 21.6059 3.46182L21.6062 3.46208C22.1251 4.00718 22.5386 4.6564 22.8213 5.37354C23.104 6.09072 23.25 6.86062 23.25 7.63895C23.25 8.41727 23.104 9.18717 22.8213 9.90436C22.5386 10.6215 22.1251 11.2707 21.6062 11.8158L21.606 11.8159L20.389 13.095L11.9997 21.912L3.6104 13.095L2.39335 11.8159C1.34505 10.7142 0.75 9.21232 0.75 7.63895C0.75 6.06557 1.34505 4.56369 2.39335 3.46195C3.44045 2.36148 4.85242 1.75065 6.31633 1.75065C7.78024 1.75065 9.19221 2.36148 10.2393 3.46195L11.4564 4.74103L11.9997 5.31207L12.543 4.74103L13.7601 3.46195Z" stroke="currentColor" stroke-width="1.5"/>+ </svg>+<% end %>diff --git a/storefront/app/views/themes/default/spree/wishlists/_no_wished_items.html.erb b/storefront/app/views/themes/default/spree/wishlists/_no_wished_items.html.erbnew file mode 100644index 0000000000..a86056924d--- /dev/null+++ b/storefront/app/views/themes/default/spree/wishlists/_no_wished_items.html.erb@@ -0,0 +1,10 @@+<div class="text-center py-6">+ <p class="mb-2 font-medium uppercase"><%= Spree.t('storefront.wished_items.no_wished_items.title') %></p>+ <p><%= Spree.t('storefront.wished_items.no_wished_items.description') %></p>+ <div class="flex justify-center mt-8">+ <%= link_to Spree.t(:shop_all), spree.products_path, class: 'btn-primary flex' %>+ </div>+</div>+<script>+ Turbo.cache.exemptPageFromPreview()+</script>diff --git a/storefront/app/views/themes/default/spree/wishlists/_wished_item.html.erb b/storefront/app/views/themes/default/spree/wishlists/_wished_item.html.erbnew file mode 100644index 0000000000..cd9f1d8548--- /dev/null+++ b/storefront/app/views/themes/default/spree/wishlists/_wished_item.html.erb@@ -0,0 +1,38 @@+<div class='w-full flex justify-between flex-col' id="<%= dom_id(wished_item) %>">+ <div class="relative">+ <%= render 'spree/products/featured_image', object: wished_item.variant %>+ <%= render 'spree/products/label', product: wished_item.product %>+ <div class="absolute right-2 top-2 z-10">+ <%= button_to spree.account_wishlist_wished_item_path(wished_item.variant_id), method: :delete, data: { turbo_confirm: 'Are you sure?' }, class: 'btn-icon', 'aria-label': Spree.t('storefront.wished_items.remove') do %>+ <%= render 'spree/shared/icons/delete' %>+ <span class="sr-only"><%= Spree.t('storefront.wished_items.remove') %></span>+ <% end %>+ </div>+ </div>+ <div>+ <div class='mt-4'>+ <%= link_to wished_item.variant.name, spree_storefront_resource_url(wished_item.product, relative: true), class: 'uppercase font-medium', data: { 'turbo-frame': '_top' } %>+ </div>+ <%= render 'spree/products/price', product: wished_item.variant, use_variant: false, price_class: 'mt-1' %>+ <div class='my-4'>+ <% wished_item.variant.option_values.each do |option| %>+ <div class='border-default border-t py-2 flex gap-1 uppercase tracking-widest text-sm !leading-4'>+ <div class='min-w-[88px]'><%= option.option_type.presentation %>:</div>+ <div><%= option.presentation %></div>+ </div>+ <% end %>+ </div>+ <% if wished_item.variant.available? && wished_item.variant.purchasable? && wished_item.variant.price_in(current_currency).amount&.positive? %>+ <%= form_with url: spree.line_items_path do |f| %>+ <%= f.hidden_field :variant_id, value: wished_item.variant_id %>+ <%= f.hidden_field :quantity, value: 1 %>+ <%= f.button class: 'btn-primary btn-icon w-full' do %>+ <%= Spree.t(:add_to_cart) %>+ <%= render 'spree/shared/icons/cart', color: 'white' %>+ <% end %>+ <% end %>+ <% else %>+ <div class="h-12 w-full font-medium bg-neutral-100 uppercase flex justify-center items-center rounded-button"><%= Spree.t('storefront.wished_items.unavailable') %></div>+ <% end %>+ </div>+</div>diff --git a/storefront/app/views/themes/default/spree/wishlists/show.html.erb b/storefront/app/views/themes/default/spree/wishlists/show.html.erbnew file mode 100644index 0000000000..01e06631ec--- /dev/null+++ b/storefront/app/views/themes/default/spree/wishlists/show.html.erb@@ -0,0 +1,17 @@+<div class="page-container grid grid-cols-1 lg:grid-cols-12 lg:gap-6 lg:mt-6">+ <div class="lg:col-span-3">+ <%= render 'spree/account/account_nav', current: 'wishlist' %>+ </div>+ <div class="lg:col-span-8 lg:col-start-5 mb-12 lg:mb-0" id="wishlist">+ <h1 class="uppercase font-medium mb-4">+ <%= Spree.t(:wishlist) %>+ </h1>+ <% if @wished_items.any? %>+ <div class="grid lg:grid-cols-3 gap-x-6 gap-y-10 lg:mb-24">+ <%= render collection: @wished_items, partial: 'spree/wishlists/wished_item', cached: ->(wished_item) { [*spree_base_cache_scope.call(wished_item), wished_item.variant, current_currency] } %>+ </div>+ <% else %>+ <%= render 'no_wished_items' %>+ <% end %>+ </div>+</div>diff --git a/storefront/config/locales/en.yml b/storefront/config/locales/en.ymlindex ce4d630f50..3deb6fde46 100644--- a/storefront/config/locales/en.yml+++ b/storefront/config/locales/en.yml@@ -1,4 +1,13 @@en:spree:storefront:+ add_to_wishlist: Add to wishlist+ wished_items:+ select_variant: Select variant to add to wishlist+ remove_error: 'You already removed this item from your wishlist'+ remove: Remove from wishlist+ unavailable: Unavailable+ no_wished_items:+ title: You have no Saved Items+ description: You have no saved items yet.\ No newline at end of filediff --git a/storefront/config/routes.rb b/storefront/config/routes.rbindex 9eab218dde..cd857fbe77 100644--- a/storefront/config/routes.rb+++ b/storefront/config/routes.rb@@ -1,5 +1,11 @@Spree::Core::Engine.add_routes doscope '(:locale)', locale: /#{Spree.available_locales.join('|')}/, defaults: { locale: nil } do+ namespace :account do+ resource :wishlist, only: [:show], controller: '/spree/wishlists' do+ resources :wished_items, only: [:create, :destroy]+ end+ end+root to: 'home#index'endenddiff --git a/storefront/spec/controllers/spree/account/wished_items_controller_spec.rb b/storefront/spec/controllers/spree/account/wished_items_controller_spec.rbnew file mode 100644index 0000000000..7d4e4e7cf2--- /dev/null+++ b/storefront/spec/controllers/spree/account/wished_items_controller_spec.rb@@ -0,0 +1,94 @@+require 'spec_helper'++describe Spree::Account::WishedItemsController, type: :controller do+ let(:store) { Spree::Store.default }+ let(:variant) { create(:variant, price: 19.99) }+ let(:format) { :html }++ before do+ allow(controller).to receive(:current_store).and_return(store)+ allow(controller).to receive(:spree_login_path).and_return('/login')+ end++ describe '#create' do+ subject { post :create, params: { wished_item: { variant_id: variant.id } }, format: format }++ context 'when an item is already added to a wishlist' do+ let(:wishlist) { create(:wishlist, user: user, store: store) }+ let(:user) { create(:user) }++ 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_messages try_spree_current_user: user+ allow(controller).to receive_messages spree_current_user: user+ allow(controller).to receive(:current_wishlist).and_return(wishlist)+ end++ it 'responds with an error' do+ expect { subject }.to_not change(Spree::WishedItem, :count)+ expect(flash[:error]).to eq('You already added this item to your wishlist')+ end+ end+ end++ describe '#destroy' do+ let(:user) { create(:user) }+ let(:wishlist) { create(:wishlist, user: user, store: store) }+ let!(:wished_item) { create(:wished_item, variant: variant, wishlist: wishlist) }++ subject { delete :destroy, params: { id: variant.id }, format: format }++ context 'when user is logged in' do+ before do+ allow(controller).to receive(:current_store).and_return(store)+ allow(controller).to receive_messages try_spree_current_user: user+ allow(controller).to receive_messages spree_current_user: user+ allow(controller).to receive(:current_wishlist).and_return(wishlist)+ end++ it 'removes wished item' do+ expect { subject }.to change { wishlist.wished_items.count }.by(-1)++ expect(response).to have_http_status(302)+ end++ context 'with turbo_stream format' do+ render_views+ let(:format) { :turbo_stream }++ it 'responds successfully' do+ subject++ expect(response).to have_http_status(200)+ end+ end+ end++ context 'when user is not logged in' do+ it 'redirects to login page' do+ expect(subject).to have_http_status(302)+ end+ end++ context 'when an item is already removed from the wishlist' do+ let(:wishlist) { create(:wishlist, user: user, store: store) }+ let(:user) { create(:user) }++ before do+ wished_item.destroy!++ allow(controller).to receive(:current_store).and_return(store)+ allow(controller).to receive_messages try_spree_current_user: user+ allow(controller).to receive_messages spree_current_user: user+ allow(controller).to receive(:current_wishlist).and_return(wishlist)+ end++ it 'responds with an error' do+ expect { subject }.to_not change(Spree::WishedItem, :count)+ expect(flash[:error]).to eq('You already removed this item from your wishlist')+ end+ end+ end+end