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 `Order#total_weight` method
Damian Legawiecb10c768992cf
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex 63012259b5..b802eaa29b 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -522,6 +522,14 @@ module Spreeself.shipments = Spree::Stock::Coordinator.new(self).shipmentsend+ # 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_promotionsSpree::PromotionHandler::FreeShipping.new(self).activateshipments.each { |shipment| Spree::Adjustable::AdjustmentsUpdater.update(shipment) }diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rbindex 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 doit { expect(subject).to eq(false) }endend++ 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) }+ endend