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 6, 2025
1 parent b367f99 commit 53b229a
Show file tree
Hide file tree
Showing 10 changed files with 104 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
15 changes: 15 additions & 0 deletions core/spec/models/spree/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ class Extension < Spree::Base
expect(product.reload.master.default_price.price).to eq 12
end
end

context "when master variant attributes change" do
before do
product.master.gtin = "1234567890123"
product.master.condition = "new"
end

it_behaves_like "a change occurred"

it "saves the master with updated gtin and condition" do
product.save!
expect(product.reload.master.gtin).to eq "1234567890123"
expect(product.reload.master.condition).to eq "new"
end
end
end

context "product has no variants" do
Expand Down
32 changes: 32 additions & 0 deletions core/spec/models/spree/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,38 @@
end
end

describe 'enums' do
it 'has the correct enum values' do
expect(described_class.conditions).to eq({
"damaged" => "Damaged",
"new" => "New",
"refurbished" => "Refurbished",
"used" => "Used"
})
end

it 'correctly assigns and reads condition values' do
variant = create(:variant, condition: 'new')
expect(variant.reload.condition).to eq('new')
end

it 'does not accept invalid enum values' do
expect { create(:variant, condition: 'invalid_value') }.to raise_error(ArgumentError)
end
end

describe 'gtin and condition attributes' do
let(:variant) { create(:variant, gtin: '1234567890123', condition: 'new') }

it 'has a GTIN' do
expect(variant.gtin).to eq('1234567890123')
end

it 'has a condition' do
expect(variant.condition).to eq('new')
end
end

context "validations" do
it "should validate price is greater than 0" do
variant = build(:variant, price: -1)
Expand Down

0 comments on commit 53b229a

Please sign in to comment.