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
Added `LineItem#item_weight` and `Shipment#item_weight` (#12886)
Damian Legawiece1b6155afdf5
diff --git a/core/app/models/spree/line_item.rb b/core/app/models/spree/line_item.rbindex 8a9c6fcc8f..bd161655c6 100644--- a/core/app/models/spree/line_item.rb+++ b/core/app/models/spree/line_item.rb@@ -48,7 +48,7 @@ module Spreeafter_create :update_tax_charge- delegate :name, :description, :sku, :should_track_inventory?, :product, :options_text, :slug, :product_id, to: :variant+ delegate :name, :description, :sku, :should_track_inventory?, :product, :options_text, :slug, :product_id, :dimensions_unit, :weight_unit, to: :variantdelegate :brand, :category, to: :productdelegate :tax_zone, to: :orderdelegate :digital?, to: :variant@@ -118,6 +118,13 @@ module Spreeamount + adjustment_totalend+ # Returns the weight of the line item+ #+ # @return [BigDecimal]+ def item_weight+ variant.weight * quantity+ end+alias total final_amountalias money display_totaldiff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex e95a005fe8..3672849a6d 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -649,7 +649,7 @@ module Spree## @return [BigDecimal] the total weight of the inventory units in the orderdef total_weight- @total_weight ||= line_items.joins(:variant).includes(:variant).map { |li| li.variant.weight * li.quantity }.sum+ @total_weight ||= line_items.joins(:variant).includes(:variant).map(&:item_weight).sumend# Returns line items that have no shipping ratesdiff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rbindex fd67973e4b..d810580a03 100644--- a/core/app/models/spree/shipment.rb+++ b/core/app/models/spree/shipment.rb@@ -221,10 +221,27 @@ module Spreeinventory_units.where(line_item_id: line_item.id, variant_id: line_item.variant_id || variant.id)end+ # Returns the cost of the shipment+ #+ # @return [BigDecimal]def item_costmanifest.map { |m| (m.line_item.price + (m.line_item.adjustment_total / m.line_item.quantity)) * m.quantity }.sumend+ # Returns the weight of the shipment+ #+ # @return [BigDecimal]+ def item_weight+ manifest.map { |m| m.line_item.item_weight }.sum+ end++ # Returns the weight unit of the shipment+ #+ # @return [String]+ def weight_unit+ manifest.first.line_item.weight_unit+ end+def line_itemsinventory_units.includes(:line_item).map(&:line_item).uniqenddiff --git a/core/spec/models/spree/line_item_spec.rb b/core/spec/models/spree/line_item_spec.rbindex 89b75e56f0..33a378c661 100644--- a/core/spec/models/spree/line_item_spec.rb+++ b/core/spec/models/spree/line_item_spec.rb@@ -536,4 +536,31 @@ describe Spree::LineItem, type: :model doexpect(line_item.display_compare_at_amount.to_s).to eq('$30.00')endend++ describe '#item_weight' do+ let(:variant) { create(:variant, weight: 10) }+ let(:line_item) { build(:line_item, variant: variant, quantity: 2) }++ it 'returns the weight for the line item' do+ expect(line_item.item_weight).to eq(20)+ end+ end++ describe '#dimensions_unit' do+ let(:variant) { create(:variant, dimensions_unit: 'cm') }+ let(:line_item) { build(:line_item, variant: variant, quantity: 2) }++ it 'returns the dimension unit for the line item' do+ expect(line_item.dimensions_unit).to eq('cm')+ end+ end++ describe '#weight_unit' do+ let(:variant) { create(:variant, weight_unit: 'kg') }+ let(:line_item) { build(:line_item, variant: variant, quantity: 2) }++ it 'returns the weight unit for the line item' do+ expect(line_item.weight_unit).to eq('kg')+ end+ endenddiff --git a/core/spec/models/spree/shipment_spec.rb b/core/spec/models/spree/shipment_spec.rbindex 13748cb41f..80e1ffe621 100644--- a/core/spec/models/spree/shipment_spec.rb+++ b/core/spec/models/spree/shipment_spec.rb@@ -203,6 +203,26 @@ describe Spree::Shipment, type: :model doendend+ describe '#item_weight' do+ it 'equals line items weight' do+ order = create(:order)+ variant = create(:variant, weight: 10)+ line_item = create(:line_item, order: order, variant: variant, quantity: 2)+ shipment = create(:shipment, order: order)+ expect(shipment.item_weight).to eq(20.0)+ end+ end++ describe '#weight_unit' do+ it 'equals line items weight unit' do+ order = create(:order)+ variant = create(:variant, weight: 10, weight_unit: 'kg')+ line_item = create(:line_item, order: order, variant: variant, quantity: 2)+ shipment = create(:shipment, order: order)+ expect(shipment.weight_unit).to eq('kg')+ end+ end+it '#discounted_cost' doshipment = create(:shipment)shipment.cost = 10