Skip to content

Commit

Permalink
Added GTIN and Condition to variant for structured data use
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulsingh321 committed Feb 3, 2025
1 parent ca1ae4d commit 0365af4
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion api/lib/spree/api_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
]

Expand Down
18 changes: 18 additions & 0 deletions backend/app/views/spree/admin/products/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@
</div>
</div>
<% else %>
<div data-hook="admin_product_form_gtin">
<%= f.field_container :gtin do %>
<%= f.label :gtin, t('spree.gtin') %>
<%= f.text_field :gtin %>
<% end %>
</div>

<div data-hook="admin_product_form_condition">
<%= 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.select_condition') },
class: 'custom-select'
%>
<% end %>
</div>

<div id="shipping_specs" class="row">
<% [:height, :width, :depth, :weight].each_with_index do |field, index| %>
<div id="shipping_specs_<%= field %>_field" class="col-6">
Expand Down
17 changes: 17 additions & 0 deletions backend/app/views/spree/admin/variants/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@
<%= f.text_field :sku, class: 'fullwidth' %>
</div>
</div>

<div class="col-3">
<div class="field" data-hook="gtin">
<%= f.label :gtin, t('spree.gtin') %>
<%= f.text_field :gtin, class: 'fullwidth' %>
</div>
</div>

<div class="col-3">
<div class="field" data-hook="condition">
<%= f.label :condition %>
<%= f.select :condition,
Spree::Variant.conditions.map { |key, value| [t("spree.condition.#{key}"), value] },
{ include_blank: t('spree.select_condition') },
class: 'custom-select fullwidth' %>
</div>
</div>
<div class="col-3">
<div class="field checkbox" data-hook="track_inventory">
<label>
Expand Down
2 changes: 2 additions & 0 deletions core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions core/app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class Variant < Spree::Base
attr_writer :rebuild_vat_prices
include Spree::DefaultPrice

# Consider that not all platforms digest structured data in the same way,
# you might have to modify the output on the frontend or in feeds accordingly.
enum condition: { damaged: "damaged", new: "new", refurbished: "refurbished", used: "used" }, _prefix: true

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
Expand Down
7 changes: 7 additions & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,11 @@ en:
company: Company
complete: complete
complete_order: Complete Order
condition:
damaged: Damaged
new: New
refurbished: Refurbished
used: Used
configuration: Configuration
configurations: Configurations
confirm: Confirm
Expand Down Expand Up @@ -1580,6 +1585,7 @@ en:
general: General
google_analytics: Google Analytics
google_analytics_id: Analytics ID
gtin: GTIN
guest_checkout: Guest Checkout
guest_user_account: Checkout as a Guest
has_no_shipped_units: has no shipped units
Expand Down Expand Up @@ -2106,6 +2112,7 @@ en:
select: Select
select_a_reason: Select a reason
select_a_stock_location: Select a stock location
select_condition: Select condition
selected_quantity_not_available: selected of %{item} is not available.
send_copy_of_all_mails_to: Send Copy of All Mails To
send_mailer: Send Shipment Email
Expand Down
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion core/lib/spree/permitted_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ module PermittedAttributes
:meta_keywords, :price, :sku, :deleted_at,
:option_values_hash, :weight, :height, :width, :depth,
:shipping_category_id, :tax_category_id,
:taxon_ids, :option_type_ids, :cost_currency, :cost_price
:taxon_ids, :option_type_ids, :cost_currency, :cost_price,
:gtin, :condition
]

@@property_attributes = [:name, :presentation]
Expand Down

0 comments on commit 0365af4

Please sign in to comment.