Skip to content

Commit 0365af4

Browse files
committed
Added GTIN and Condition to variant for structured data use
1 parent ca1ae4d commit 0365af4

File tree

8 files changed

+57
-2
lines changed

8 files changed

+57
-2
lines changed

api/lib/spree/api_configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ApiConfiguration < Preferences::Configuration
1313
preference :product_property_attributes, :array, default: [:id, :product_id, :property_id, :value, :property_name]
1414

1515
preference :variant_attributes, :array, default: [
16-
:id, :name, :sku, :weight, :height, :width, :depth, :is_master,
16+
:id, :name, :sku, :gtin, :condition, :weight, :height, :width, :depth, :is_master,
1717
:slug, :description, :track_inventory
1818
]
1919

backend/app/views/spree/admin/products/_form.html.erb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,24 @@
172172
</div>
173173
</div>
174174
<% else %>
175+
<div data-hook="admin_product_form_gtin">
176+
<%= f.field_container :gtin do %>
177+
<%= f.label :gtin, t('spree.gtin') %>
178+
<%= f.text_field :gtin %>
179+
<% end %>
180+
</div>
181+
182+
<div data-hook="admin_product_form_condition">
183+
<%= f.field_container :condition do %>
184+
<%= f.label :condition %>
185+
<%= f.select :condition,
186+
Spree::Variant.conditions.map { |key, value| [t("spree.condition.#{key}"), value] },
187+
{ include_blank: t('spree.select_condition') },
188+
class: 'custom-select'
189+
%>
190+
<% end %>
191+
</div>
192+
175193
<div id="shipping_specs" class="row">
176194
<% [:height, :width, :depth, :weight].each_with_index do |field, index| %>
177195
<div id="shipping_specs_<%= field %>_field" class="col-6">

backend/app/views/spree/admin/variants/_form.html.erb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
77
<%= f.text_field :sku, class: 'fullwidth' %>
88
</div>
99
</div>
10+
11+
<div class="col-3">
12+
<div class="field" data-hook="gtin">
13+
<%= f.label :gtin, t('spree.gtin') %>
14+
<%= f.text_field :gtin, class: 'fullwidth' %>
15+
</div>
16+
</div>
17+
18+
<div class="col-3">
19+
<div class="field" data-hook="condition">
20+
<%= f.label :condition %>
21+
<%= f.select :condition,
22+
Spree::Variant.conditions.map { |key, value| [t("spree.condition.#{key}"), value] },
23+
{ include_blank: t('spree.select_condition') },
24+
class: 'custom-select fullwidth' %>
25+
</div>
26+
</div>
1027
<div class="col-3">
1128
<div class="field checkbox" data-hook="track_inventory">
1229
<label>

core/app/models/spree/product.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ def find_or_build_master
8484
:track_inventory,
8585
:weight,
8686
:width,
87+
:gtin,
88+
:condition
8789
]
8890
MASTER_ATTRIBUTES.each do |attr|
8991
delegate :"#{attr}", :"#{attr}=", to: :find_or_build_master

core/app/models/spree/variant.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class Variant < Spree::Base
2828
attr_writer :rebuild_vat_prices
2929
include Spree::DefaultPrice
3030

31+
# Consider that not all platforms digest structured data in the same way,
32+
# you might have to modify the output on the frontend or in feeds accordingly.
33+
enum condition: { damaged: "damaged", new: "new", refurbished: "refurbished", used: "used" }, _prefix: true
34+
3135
belongs_to :product, -> { with_discarded }, touch: true, class_name: 'Spree::Product', inverse_of: :variants_including_master, optional: false
3236
belongs_to :tax_category, class_name: 'Spree::TaxCategory', optional: true
3337
belongs_to :shipping_category, class_name: "Spree::ShippingCategory", optional: true

core/config/locales/en.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,11 @@ en:
11321132
company: Company
11331133
complete: complete
11341134
complete_order: Complete Order
1135+
condition:
1136+
damaged: Damaged
1137+
new: New
1138+
refurbished: Refurbished
1139+
used: Used
11351140
configuration: Configuration
11361141
configurations: Configurations
11371142
confirm: Confirm
@@ -1580,6 +1585,7 @@ en:
15801585
general: General
15811586
google_analytics: Google Analytics
15821587
google_analytics_id: Analytics ID
1588+
gtin: GTIN
15831589
guest_checkout: Guest Checkout
15841590
guest_user_account: Checkout as a Guest
15851591
has_no_shipped_units: has no shipped units
@@ -2106,6 +2112,7 @@ en:
21062112
select: Select
21072113
select_a_reason: Select a reason
21082114
select_a_stock_location: Select a stock location
2115+
select_condition: Select condition
21092116
selected_quantity_not_available: selected of %{item} is not available.
21102117
send_copy_of_all_mails_to: Send Copy of All Mails To
21112118
send_mailer: Send Shipment Email
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AddGtinAndConditionToSpreeVariant < ActiveRecord::Migration[7.0]
2+
def change
3+
add_column :spree_variants, :gtin, :string
4+
add_column :spree_variants, :condition, :string
5+
end
6+
end

core/lib/spree/permitted_attributes.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ module PermittedAttributes
7878
:meta_keywords, :price, :sku, :deleted_at,
7979
:option_values_hash, :weight, :height, :width, :depth,
8080
:shipping_category_id, :tax_category_id,
81-
:taxon_ids, :option_type_ids, :cost_currency, :cost_price
81+
:taxon_ids, :option_type_ids, :cost_currency, :cost_price,
82+
:gtin, :condition
8283
]
8384

8485
@@property_attributes = [:name, :presentation]

0 commit comments

Comments
 (0)