Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Added `Order#total_weight` method

Damian Legawiecb10c768992cf
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index 63012259b5..b802eaa29b 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -522,6 +522,14 @@ module Spree
self.shipments = Spree::Stock::Coordinator.new(self).shipments
end
+ # Returns the total weight of the inventory units in the order
+ # This is used to calculate the shipping rates for the order
+ #
+ # @return [BigDecimal] the total weight of the inventory units in the order
+ def total_weight
+ @total_weight ||= line_items.joins(:variant).includes(:variant).map { |li| li.variant.weight * li.quantity }.sum
+ end
+
def apply_free_shipping_promotions
Spree::PromotionHandler::FreeShipping.new(self).activate
shipments.each { |shipment| Spree::Adjustable::AdjustmentsUpdater.update(shipment) }
diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb
index 973046441b..ed2b0d4173 100644
--- a/core/spec/models/spree/order_spec.rb
+++ b/core/spec/models/spree/order_spec.rb
@@ -1726,4 +1726,20 @@ describe Spree::Order, type: :model do
it { expect(subject).to eq(false) }
end
end
+
+ describe '#total_weight' do
+ subject { order.total_weight }
+
+ let!(:line_items) { create_list(:line_item, 2, order: order, quantity: 2) }
+ let(:order) { create(:order) }
+
+ before do
+ line_items.each do |line_item|
+ line_item.variant.weight = 10
+ line_item.variant.save!
+ end
+ end
+
+ it { expect(subject).to eq(40) }
+ end
end