From 32495fbe0e0f0eb1ee7babd4fd8481c4471550b6 Mon Sep 17 00:00:00 2001 From: Rahul Date: Sun, 2 Feb 2025 00:20:14 +0530 Subject: [PATCH] Added GTIN and Condition to variant for structured data use Issue: #6060 --- .../app/views/spree/admin/products/_form.html.erb | 11 ++++++++++- .../app/views/spree/admin/variants/_form.html.erb | 12 +++++++++++- core/app/models/spree/product.rb | 2 ++ core/app/models/spree/variant.rb | 3 +++ ...172950_add_gtin_and_condition_to_spree_variant.rb | 6 ++++++ 5 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 core/db/migrate/20250201172950_add_gtin_and_condition_to_spree_variant.rb diff --git a/backend/app/views/spree/admin/products/_form.html.erb b/backend/app/views/spree/admin/products/_form.html.erb index a8b2ec4bbd1..4843cad47dd 100644 --- a/backend/app/views/spree/admin/products/_form.html.erb +++ b/backend/app/views/spree/admin/products/_form.html.erb @@ -173,7 +173,7 @@ <% else %>
- <% [:height, :width, :depth, :weight].each_with_index do |field, index| %> + <% [:height, :width, :depth, :weight, :gtin].each_with_index do |field, index| %>
<%= f.label field %> @@ -190,6 +190,15 @@ <% end %>
+
+ <%= f.field_container :condition do %> + <%= f.label :condition %> + <%= f.select :condition, + Spree::Variant::CONDITIONS.map { |c| [c, c] }, + { include_blank: t(:select_condition) }, + { class: 'custom-select' } %> + <% end %> +
<% end %>
diff --git a/backend/app/views/spree/admin/variants/_form.html.erb b/backend/app/views/spree/admin/variants/_form.html.erb index 9a12b8c3b29..5fa93feb1de 100644 --- a/backend/app/views/spree/admin/variants/_form.html.erb +++ b/backend/app/views/spree/admin/variants/_form.html.erb @@ -45,7 +45,7 @@
<%= t(".properties") %>
- <% [:weight, :height, :width, :depth].each_with_index do |field, index| %> + <% [:weight, :height, :width, :depth, :gtin].each_with_index do |field, index| %>
<%= f.label field %> @@ -55,6 +55,16 @@
<% end %> + +
+
+ <%= f.label :condition %> + <%= f.select :condition, + Spree::Variant::CONDITIONS.map { |c| [c, c] }, + { include_blank: t(:select_condition) }, + { class: 'custom-select fullwidth' } %> +
+
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb index 03193148ec0..71f4194ada2 100644 --- a/core/app/models/spree/product.rb +++ b/core/app/models/spree/product.rb @@ -84,6 +84,8 @@ def find_or_build_master :track_inventory, :weight, :width, + :gtin, + :condition ] MASTER_ATTRIBUTES.each do |attr| delegate :"#{attr}", :"#{attr}=", to: :find_or_build_master diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb index 606d1a5e31c..cb1468df740 100644 --- a/core/app/models/spree/variant.rb +++ b/core/app/models/spree/variant.rb @@ -28,6 +28,8 @@ class Variant < Spree::Base attr_writer :rebuild_vat_prices include Spree::DefaultPrice + CONDITIONS = %w[New Refurbished Used Damaged].freeze + belongs_to :product, -> { with_discarded }, touch: true, class_name: 'Spree::Product', inverse_of: :variants_including_master, optional: false belongs_to :tax_category, class_name: 'Spree::TaxCategory', optional: true belongs_to :shipping_category, class_name: "Spree::ShippingCategory", optional: true @@ -70,6 +72,7 @@ class Variant < Spree::Base validates :cost_price, numericality: { greater_than_or_equal_to: 0, allow_nil: true } validates :price, numericality: { greater_than_or_equal_to: 0, allow_nil: true } + validates :condition, inclusion: { in: CONDITIONS }, allow_nil: true validates_uniqueness_of :sku, allow_blank: true, case_sensitive: true, conditions: -> { where(deleted_at: nil) }, if: :enforce_unique_sku? after_create :create_stock_items diff --git a/core/db/migrate/20250201172950_add_gtin_and_condition_to_spree_variant.rb b/core/db/migrate/20250201172950_add_gtin_and_condition_to_spree_variant.rb new file mode 100644 index 00000000000..ba012f09a67 --- /dev/null +++ b/core/db/migrate/20250201172950_add_gtin_and_condition_to_spree_variant.rb @@ -0,0 +1,6 @@ +class AddGtinAndConditionToSpreeVariant < ActiveRecord::Migration[7.0] + def change + add_column :spree_variants, :gtin, :string + add_column :spree_variants, :condition, :string + end +end