Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Allow importing CSV with dimensions and weight units for products (#12365)
Łukasz Adamczyk700526f1e17b
diff --git a/admin/app/helpers/spree/admin/store_helper.rb b/admin/app/helpers/spree/admin/store_helper.rbindex fb95fcfff0..75f9b1661e 100644--- a/admin/app/helpers/spree/admin/store_helper.rb+++ b/admin/app/helpers/spree/admin/store_helper.rb@@ -5,6 +5,7 @@ module Spreedef weight_units(store = nil)store ||= current_store+if store.metric_unit_system?[[Spree.t('weight_units.kilogram'), 'kg'],@@ -18,18 +19,18 @@ module Spreeendend- def dimensions_units(store = nil)+ def dimension_units(store = nil)store ||= current_storeif store.metric_unit_system?[- [Spree.t('dimensions_units.centimeter'), 'cm'],- [Spree.t('dimensions_units.millimeter'), 'mm']+ [Spree.t('dimension_units.centimeter'), 'cm'],+ [Spree.t('dimension_units.millimeter'), 'mm']]else[- [Spree.t('dimensions_units.inch'), 'in'],- [Spree.t('dimensions_units.foot'), 'ft']+ [Spree.t('dimension_units.inch'), 'in'],+ [Spree.t('dimension_units.foot'), 'ft']]endenddiff --git a/admin/app/views/spree/admin/variants/form/_dimensions.html.erb b/admin/app/views/spree/admin/variants/form/_dimensions.html.erbindex 332f192c46..f36610a2b4 100644--- a/admin/app/views/spree/admin/variants/form/_dimensions.html.erb+++ b/admin/app/views/spree/admin/variants/form/_dimensions.html.erb@@ -1,34 +1,33 @@<div class="row"><div class="col-lg-6">- <div class="form-group">+ <div class="form-group mb-3"><%= f.label :width %>- <div class="form-control d-flex align-items-center py-0 focus-border-blue focus-shadow-blue mb-3">- <%= f.text_field :width, value: number_with_precision(f.object.width, precision: 2), class: 'form-control-plaintext pl-0' %>- <%= f.select :dimensions_unit, options_for_select(dimensions_units(current_store), f.object.dimensions_unit), { include_blank: false }, { class: 'form-control-plaintext w-auto border-left' } %>- </div>+ <%= f.text_field :width, value: number_with_precision(f.object.width, precision: 2), class: 'form-control' %></div>- <div class="form-group">++ <div class="form-group mb-3"><%= f.label :height %>- <div class="form-control d-flex align-items-center py-0 focus-border-blue focus-shadow-blue mb-3">- <%= f.text_field :height, value: number_with_precision(f.object.height, precision: 2), class: 'form-control-plaintext pl-0' %>- <%= f.select :dimensions_unit, options_for_select(dimensions_units(current_store), f.object.dimensions_unit), { include_blank: false }, { class: 'form-control-plaintext w-auto border-left' } %>- </div>+ <%= f.text_field :height, value: number_with_precision(f.object.height, precision: 2), class: 'form-control' %></div></div>+<div class="col-lg-6">- <div class="form-group">+ <div class="form-group mb-3"><%= f.label :depth %>- <div class="form-control d-flex align-items-center py-0 focus-border-blue focus-shadow-blue mb-3">- <%= f.text_field :depth, value: number_with_precision(f.object.depth, precision: 2), class: 'form-control-plaintext pl-0' %>- <%= f.select :dimensions_unit, options_for_select(dimensions_units(current_store), f.object.dimensions_unit), { include_blank: false }, { class: 'form-control-plaintext w-auto border-left' } %>- </div>+ <%= f.text_field :depth, value: number_with_precision(f.object.depth, precision: 2), class: 'form-control' %></div>- <div class="form-group">- <%= f.label :weight %>- <div class="form-control d-flex align-items-center py-0 focus-border-blue focus-shadow-blue mb-3">- <%= f.text_field :weight, value: number_with_precision(f.object.weight, precision: 2), class: 'form-control-plaintext pl-0' %>- <%= f.select :weight_unit, options_for_select(weight_units(current_store), f.object.weight_unit), { include_blank: false }, { class: 'form-control-plaintext w-auto border-left' } %>- </div>++ <div class="form-group mb-3">+ <%= f.label :dimensions_unit, Spree.t(:unit) %>+ <%= f.select :dimensions_unit, options_for_select(dimension_units(current_store), f.object.dimensions_unit), { include_blank: false }, { class: 'form-control' } %></div></div>-</div>\ No newline at end of file+</div>++<div class="form-group">+ <%= f.label :weight %>+ <div class="form-control d-flex align-items-center py-0 focus-border-blue focus-shadow-blue">+ <%= f.text_field :weight, value: number_with_precision(f.object.weight, precision: 2), class: 'form-control-plaintext pl-0' %>+ <%= f.select :weight_unit, options_for_select(weight_units(current_store), f.object.weight_unit), { include_blank: false }, { class: 'form-control-plaintext w-auto border-left' } %>+ </div>+</div>diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex 5fa7ac5537..f1a0162370 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -9,6 +9,9 @@ module SpreeMEMOIZED_METHODS = %w(purchasable in_stock on_sale backorderable tax_category options_text compare_at_price)+ DIMENSION_UNITS = %w[mm cm in ft]+ WEIGHT_UNITS = %w[g kg lb oz]+belongs_to :product, -> { with_deleted }, touch: true, class_name: 'Spree::Product', inverse_of: :variantsbelongs_to :tax_category, class_name: 'Spree::TaxCategory', optional: true@@ -64,6 +67,9 @@ module Spreevalidates :sku, uniqueness: { conditions: -> { where(deleted_at: nil) }, case_sensitive: false, scope: spree_base_uniqueness_scope },allow_blank: true, unless: :disable_sku_validation?+ validates :dimensions_unit, inclusion: { in: DIMENSION_UNITS }, allow_blank: true+ validates :weight_unit, inclusion: { in: WEIGHT_UNITS }, allow_blank: true+after_create :create_stock_itemsafter_create :set_master_out_of_stock, unless: :is_master?after_commit :clear_line_items_cache, on: :updatediff --git a/core/app/presenters/spree/csv/product_variant_presenter.rb b/core/app/presenters/spree/csv/product_variant_presenter.rbindex 356b523d8f..2b7c4ed127 100644--- a/core/app/presenters/spree/csv/product_variant_presenter.rb+++ b/core/app/presenters/spree/csv/product_variant_presenter.rb@@ -22,7 +22,9 @@ module Spree'width','height','depth',+ 'dimensions_unit','weight',+ 'weight_unit','available_on','discontinue_on','inventory_count',@@ -77,7 +79,9 @@ module Spreevariant.width,variant.height,variant.depth,+ variant.dimensions_unit,variant.weight,+ variant.weight_unit,variant.available_on&.strftime('%Y-%m-%d %H:%M:%S'),variant.discontinue_on&.strftime('%Y-%m-%d %H:%M:%S'),variant.total_on_hand == BigDecimal::INFINITY ? '∞' : variant.total_on_hand,diff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex fe27ada85d..427254a02e 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -797,6 +797,11 @@ en:delete_from_taxon: Delete From Taxondigital:digital_delivery: Digital Delivery+ dimension_units:+ millimeter: Millimeter (mm)+ centimeter: Centimeter (cm)+ inch: Inch (in)+ foot: Foot (ft)discontinued_variants_present: Some line items in this order have products that are no longer available.delivery: Deliverydelivery_information: Delivery Information@@ -1847,6 +1852,11 @@ en:unable_to_connect_to_gateway: Unable to connect to gateway.unable_to_create_reimbursements: Unable to create reimbursements because there are items pending manual intervention.under_price: Under %{price}+ unit_system: Unit system+ unit_systems:+ metric_system: Metric system+ imperial_system: Imperial system+ metric: metricunlock: Unlockunrecognized_card_type: Unrecognized card typeunshippable_items: Unshippable Items@@ -1885,6 +1895,11 @@ en:we_are_busy_updating_page: "We're busy updating this page."please_check_back_soon: Please check back soon.weight: Weight+ weight_units:+ kilogram: Kilogram (kg)+ gram: Gram (g)+ pound: Pound (lb)+ ounce: Ounce (oz)what_is_a_cvv: What is a (CVV) Credit Card Code?what_is_this: What's This?width: Widthdiff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rbindex b11ad4a5d1..35bf7f40e8 100644--- a/core/spec/models/spree/variant_spec.rb+++ b/core/spec/models/spree/variant_spec.rb@@ -72,6 +72,30 @@ describe Spree::Variant, type: :model doendendend++ it 'validates the dimensions unit' do+ expect(build(:variant, dimensions_unit: nil)).to be_valid++ expect(build(:variant, dimensions_unit: 'mm')).to be_valid+ expect(build(:variant, dimensions_unit: 'cm')).to be_valid+ expect(build(:variant, dimensions_unit: 'in')).to be_valid+ expect(build(:variant, dimensions_unit: 'ft')).to be_valid++ expect(build(:variant, dimensions_unit: 'oz')).to be_invalid+ expect(build(:variant, dimensions_unit: 'lb')).to be_invalid+ end++ it 'validates the weight unit' do+ expect(build(:variant, weight_unit: nil)).to be_valid++ expect(build(:variant, weight_unit: 'g')).to be_valid+ expect(build(:variant, weight_unit: 'kg')).to be_valid+ expect(build(:variant, weight_unit: 'lb')).to be_valid+ expect(build(:variant, weight_unit: 'oz')).to be_valid++ expect(build(:variant, weight_unit: 'mm')).to be_invalid+ expect(build(:variant, weight_unit: 'ft')).to be_invalid+ endendcontext 'after create' dodiff --git a/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb b/core/spec/presenters/spree/csv/product_variant_presenter_spec.rbindex 4082e21359..6bdc3d9e93 100644--- a/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb+++ b/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb@@ -2,7 +2,7 @@ require 'spec_helper'RSpec.describe Spree::CSV::ProductVariantPresenter dolet(:store) { Spree::Store.default }- let(:product) { create(:product, stores: [store]) }+ let(:product) { create(:product, stores: [store], width: 10, height: 15, depth: 20, dimensions_unit: 'in', weight_unit: 'lb') }let(:variant) { product.master }let(:properties) { [] }let(:taxons) { [] }@@ -32,16 +32,18 @@ RSpec.describe Spree::CSV::ProductVariantPresenter doexpect(subject[17]).to eq variant.widthexpect(subject[18]).to eq variant.heightexpect(subject[19]).to eq variant.depth- expect(subject[20]).to eq variant.weight- expect(subject[21]).to eq variant.available_on&.strftime('%Y-%m-%d %H:%M:%S')- expect(subject[22]).to eq variant.discontinue_on&.strftime('%Y-%m-%d %H:%M:%S')- expect(subject[23]).to eq(variant.total_on_hand == BigDecimal::INFINITY ? '∞' : variant.total_on_hand)- expect(subject[24]).to eq variant.backorderable?- expect(subject[25]).to eq variant.tax_category&.name- expect(subject[26]).to eq variant.digital?- expect(subject[27]).to eq variant.images[0]&.attachment&.url- expect(subject[28]).to eq variant.images[1]&.attachment&.url- expect(subject[29]).to eq variant.images[2]&.attachment&.url+ expect(subject[20]).to eq 'in'+ expect(subject[21]).to eq variant.weight+ expect(subject[22]).to eq 'lb'+ expect(subject[23]).to eq variant.available_on&.strftime('%Y-%m-%d %H:%M:%S')+ expect(subject[24]).to eq variant.discontinue_on&.strftime('%Y-%m-%d %H:%M:%S')+ expect(subject[25]).to eq(variant.total_on_hand == BigDecimal::INFINITY ? '∞' : variant.total_on_hand)+ expect(subject[26]).to eq variant.backorderable?+ expect(subject[27]).to eq variant.tax_category&.name+ expect(subject[28]).to eq variant.digital?+ expect(subject[29]).to eq variant.images[0]&.attachment&.url+ expect(subject[30]).to eq variant.images[1]&.attachment&.url+ expect(subject[31]).to eq variant.images[2]&.attachment&.urlendcontext 'when index is not zero' do