From af779ef7c619efa00d7633e73ad26ab8c01ab3a2 Mon Sep 17 00:00:00 2001 From: Rahul Date: Wed, 19 Feb 2025 01:14:20 +0530 Subject: [PATCH 1/2] Adding Conditions and GTIN to products and variant Summary This commit adds GTIN and Conditions to products to allow a more efficient integration of structured data and the related services such as Google Shopping or Facebook Marketplaces. Changes - Add Conditions and GTIN to products and variants - Added the fields to the Admin Interface - Add the values to API - Provides samples for GTIN (format EAN 8) and Condition (New) to all sample products. - Added Testing of the related fields - feat(i18n): Differentiate GTIN and Condition labels for product and variant forms. --- .../products/show/component.html.erb | 8 ++++- .../solidus_admin/products/show/component.rb | 6 ++++ api/lib/spree/api_configuration.rb | 2 +- api/openapi/solidus-api.oas.yml | 4 +++ .../views/spree/admin/products/_form.html.erb | 18 +++++++++++ .../views/spree/admin/variants/_form.html.erb | 17 ++++++++++ core/app/models/spree/product.rb | 2 ++ core/app/models/spree/variant.rb | 4 +++ core/config/locales/en.yml | 10 ++++++ ...add_gtin_and_condition_to_spree_variant.rb | 6 ++++ core/lib/spree/permitted_attributes.rb | 3 +- core/spec/models/spree/product_spec.rb | 15 +++++++++ core/spec/models/spree/variant_spec.rb | 32 +++++++++++++++++++ sample/db/samples/products.rb | 24 ++++++++++++++ 14 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 core/db/migrate/20250201172950_add_gtin_and_condition_to_spree_variant.rb diff --git a/admin/app/components/solidus_admin/products/show/component.html.erb b/admin/app/components/solidus_admin/products/show/component.html.erb index 3289bd88df3..56ee060d605 100644 --- a/admin/app/components/solidus_admin/products/show/component.html.erb +++ b/admin/app/components/solidus_admin/products/show/component.html.erb @@ -36,8 +36,14 @@ <%= render component("ui/forms/field").text_field(f, :meta_title) %> <%= render component("ui/forms/field").text_field(f, :meta_description) %> <%= render component("ui/forms/field").text_area(f, :meta_keywords) %> + <%= render component("ui/forms/field").text_field(f, :gtin) %> + <%= render component("ui/forms/field").select( + f, + :condition, + condition_options, + include_blank: t('spree.unset'), + ) %> <% end %> - <%= render component('ui/panel').new(title: "Media") do |panel| %> <% panel.with_action( name: t(".manage_images"), diff --git a/admin/app/components/solidus_admin/products/show/component.rb b/admin/app/components/solidus_admin/products/show/component.rb index 08d200d17d8..66ae89250fd 100644 --- a/admin/app/components/solidus_admin/products/show/component.rb +++ b/admin/app/components/solidus_admin/products/show/component.rb @@ -25,4 +25,10 @@ def option_type_options ["#{_1} (#{_2})", _3] end end + + def condition_options + @condition_options ||= Spree::Variant.conditions.map do |key, value| + [t("spree.condition.#{key}"), value] + end + end end diff --git a/api/lib/spree/api_configuration.rb b/api/lib/spree/api_configuration.rb index 8fd16fb747c..77b047f3258 100644 --- a/api/lib/spree/api_configuration.rb +++ b/api/lib/spree/api_configuration.rb @@ -13,7 +13,7 @@ class ApiConfiguration < Preferences::Configuration preference :product_property_attributes, :array, default: [:id, :product_id, :property_id, :value, :property_name] preference :variant_attributes, :array, default: [ - :id, :name, :sku, :weight, :height, :width, :depth, :is_master, + :id, :name, :sku, :gtin, :condition, :weight, :height, :width, :depth, :is_master, :slug, :description, :track_inventory ] diff --git a/api/openapi/solidus-api.oas.yml b/api/openapi/solidus-api.oas.yml index 8fbb507795a..2c54599a40e 100644 --- a/api/openapi/solidus-api.oas.yml +++ b/api/openapi/solidus-api.oas.yml @@ -74,6 +74,8 @@ paths: product: name: The Majestic Product price: '19.99' + gtin: 12345678 + condition: New shipping_category_id: 8 product_properties_attributes: - property_name: fabric @@ -86,6 +88,8 @@ paths: - price: 19.99 cost_price: 17 sku: SKU-3 + gtin: 12345678 + condition: New track_inventory: true options: - name: size diff --git a/backend/app/views/spree/admin/products/_form.html.erb b/backend/app/views/spree/admin/products/_form.html.erb index dec7f5f0c3c..eed4cfcf34b 100644 --- a/backend/app/views/spree/admin/products/_form.html.erb +++ b/backend/app/views/spree/admin/products/_form.html.erb @@ -179,6 +179,24 @@ <% else %> +
+ <%= f.field_container :gtin do %> + <%= f.label :gtin %> + <%= f.text_field :gtin %> + <% end %> +
+ +
+ <%= f.field_container :condition do %> + <%= f.label :condition %> + <%= f.select :condition, + Spree::Variant.conditions.map { |key, value| [t("spree.condition.#{key}"), value] }, + { include_blank: t('spree.unset') }, + class: 'custom-select' + %> + <% end %> +
+
<% [:height, :width, :depth, :weight].each_with_index do |field, index| %>
diff --git a/backend/app/views/spree/admin/variants/_form.html.erb b/backend/app/views/spree/admin/variants/_form.html.erb index 9a12b8c3b29..fcddd50950b 100644 --- a/backend/app/views/spree/admin/variants/_form.html.erb +++ b/backend/app/views/spree/admin/variants/_form.html.erb @@ -7,6 +7,23 @@ <%= f.text_field :sku, class: 'fullwidth' %>
+ +
+
+ <%= f.label :gtin %> + <%= f.text_field :gtin, class: 'fullwidth' %> +
+
+ +
+
+ <%= f.label :condition %> + <%= f.select :condition, + Spree::Variant.conditions.map { |key, value| [t("spree.condition.#{key}"), value] }, + { include_blank: t('spree.unset') }, + class: 'custom-select fullwidth' %> +
+