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.

Support multiple relation types for related products on PDP (#10711)

Damian Legawiec8d50902e44a8
diff --git a/core/app/helpers/spree/products_helper.rb b/core/app/helpers/spree/products_helper.rb
index 208c7a4711..97aa97e4f1 100644
--- a/core/app/helpers/spree/products_helper.rb
+++ b/core/app/helpers/spree/products_helper.rb
@@ -115,19 +115,39 @@ module Spree
).call.to_json
end
+ def product_relation_types
+ @product_relation_types ||= @product.respond_to?(:relation_types) ? @product.relation_types : []
+ end
+
+ def product_relations_by_type(relation_type)
+ return [] if product_relation_types.none? || [email protected]_to?(:relations)
+
+ product_ids = @product.relations.where(relation_type: relation_type).pluck(:related_to_id).uniq
+
+ return [] if product_ids.empty?
+
+ Spree::Product.
+ available.not_discontinued.distinct.
+ where(id: product_ids).
+ includes(
+ :tax_category,
+ master: [
+ :prices,
+ { images: { attachment_attachment: :blob } },
+ ]
+ ).
+ limit(Spree::Config[:products_per_page])
+ end
+
def related_products
- return [] unless @product.respond_to?(:has_related_products?) && @product.has_related_products?(:related_products)
-
- @related_products ||= @product.
- related_products.
- includes(
- :tax_category,
- master: [
- :prices,
- images: { attachment_attachment: :blob },
- ]
- ).
- limit(Spree::Config[:products_per_page])
+ ActiveSupport::Deprecation.warn(<<-DEPRECATION, caller)
+ ProductsHelper#related_products is deprecated and will be removed in Spree 5.0.
+ Please use ProductsHelper#relations from now on.
+ DEPRECATION
+
+ return [] unless @product.respond_to?(:has_related_products?)
+
+ @related_products ||= relations_by_type('related_products')
end
def product_available_in_currency?
diff --git a/frontend/app/controllers/spree/products_controller.rb b/frontend/app/controllers/spree/products_controller.rb
index 302bc255af..473d61f544 100644
--- a/frontend/app/controllers/spree/products_controller.rb
+++ b/frontend/app/controllers/spree/products_controller.rb
@@ -33,9 +33,7 @@ module Spree
end
def related
- @related_products = related_products
-
- if @related_products.any?
+ if product_relation_types.any?
render template: 'spree/products/related', layout: false
else
head :no_content
diff --git a/frontend/app/views/spree/products/related.html.erb b/frontend/app/views/spree/products/related.html.erb
index 4887a62936..41eae0085e 100644
--- a/frontend/app/views/spree/products/related.html.erb
+++ b/frontend/app/views/spree/products/related.html.erb
@@ -1,8 +1,14 @@
-<% if related_products.any? %>
+<% if product_relation_types.any? %>
<div id="related-products">
- <div class="product-details-related">
- <p class="font-weight-bold text-center text-uppercase product-details-carousel-text"><%= Spree.t('pdp.you_may_also_like') %></p>
- <%= render 'spree/shared/carousel_4_products', id: 'related-products-carousel', products: @related_products %>
- </div>
+ <% product_relation_types.each do |relation_type| %>
+ <% products = product_relations_by_type(relation_type) %>
+
+ <% if products.any? %>
+ <div class="product-details-related" id="product-details-related-<%= relation_type.id %>">
+ <p class="font-weight-bold text-center text-uppercase product-details-carousel-text"><%= relation_type.name %></p>
+ <%= render 'spree/shared/carousel_4_products', id: "related-products-carousel-#{relation_type.id}", products: products %>
+ </div>
+ <% end %>
+ <% end %>
</div>
<% end %>
diff --git a/guides/src/content/developer/upgrades/four-dot-one-to-four-dot-two.md b/guides/src/content/developer/upgrades/four-dot-one-to-four-dot-two.md
index e58bd97a1f..5f3ba8e02a 100644
--- a/guides/src/content/developer/upgrades/four-dot-one-to-four-dot-two.md
+++ b/guides/src/content/developer/upgrades/four-dot-one-to-four-dot-two.md
@@ -26,7 +26,7 @@ gem 'spree_i18n', '~> 5.0', github: 'spree-contrib/spree_i18n', branch: 'master'
## Update gems
-```
+```bash
bundle update
```
@@ -57,6 +57,14 @@ rails active_storage:update
rails db:migrate
```
+## Upgrade all of your Spree extensions to the newest versions
+
+To avoid errors and compatibility issues, please update all of your Spree extension gems to the newest versions which usually includes fixes for the new Spree release, eg.
+
+```bash
+bundle update spree_related_products
+```
+
## Other things to remember
### Replace `fast_json` with `jsonapi-serializer`
diff --git a/guides/src/content/release_notes/4_2_0.md b/guides/src/content/release_notes/4_2_0.md
index 7739074622..6c387348f8 100644
--- a/guides/src/content/release_notes/4_2_0.md
+++ b/guides/src/content/release_notes/4_2_0.md
@@ -108,6 +108,8 @@ Please review each of the noteworthy changes to ensure your customizations or ex
* Add ship-to address on Checkout "Choose Delivery Method" page [Matthew Kennedy](https://github.com/spree/spree/pull/10239)
+* Added support for multiple Relation Types via [Related Products](https://github.com/spree-contrib/spree_related_products) gem on product page [Spark Solutions](https://github.com/spree/spree/pull/10711)
+
* Added `last_modified` and `etag` helper methods for `Spree::ProductsController` [Spark Solutions](https://github.com/spree/spree/pull/10515)
* Replace jquery.payment with cleave [Matthew Kennedy](https://github.com/spree/spree/pull/10363)