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
SD-1127 Fix dynamic delivery costs (#10721)
Przemek Kosakowski8a773e294e2c
diff --git a/frontend/app/assets/javascripts/spree/frontend/checkout/shipment.js b/frontend/app/assets/javascripts/spree/frontend/checkout/shipment.jsindex a989bc1500..bf3b063657 100644--- a/frontend/app/assets/javascripts/spree/frontend/checkout/shipment.js+++ b/frontend/app/assets/javascripts/spree/frontend/checkout/shipment.js@@ -3,6 +3,7 @@ function ShippingTotalManager (input1) {this.input = input1this.shippingMethods = this.input.shippingMethodsthis.shipmentTotal = this.input.shipmentTotal+ this.isFreeShipping = this.input.isFreeShippingthis.taxTotal = this.input.taxTotalthis.nonShipmentTax = $(this.taxTotal).data('non-shipment-tax')this.orderTotal = this.input.orderTotal@@ -35,8 +36,19 @@ ShippingTotalManager.prototype.parseCurrencyToFloat = function (input) {return accounting.unformat(input, this.formatOptions.decimal)}+ShippingTotalManager.prototype.totalShipmentAmount = function (newShipmentTotal, oldShipmentTotal) {+ var totalShipmentAmount = 0;+ if (!this.isFreeShipping.html()) {+ totalShipmentAmount = newShipmentTotal - oldShipmentTotal++ return totalShipmentAmount+ } else {+ return totalShipmentAmount+ }+}+ShippingTotalManager.prototype.readjustSummarySection = function (orderTotal, newShipmentTotal, oldShipmentTotal, newTaxTotal, oldTaxTotal) {- var newOrderTotal = orderTotal + (newShipmentTotal - oldShipmentTotal) + (newTaxTotal - oldTaxTotal)+ var newOrderTotal = orderTotal + this.totalShipmentAmount(newShipmentTotal, oldShipmentTotal) + (newTaxTotal - oldTaxTotal)this.taxTotal.html(accounting.formatMoney(newTaxTotal, this.formatOptions))this.shipmentTotal.html(accounting.formatMoney(newShipmentTotal, this.formatOptions))return this.orderTotal.html(accounting.formatMoney(accounting.toFixed(newOrderTotal, 10), this.formatOptions))@@ -53,7 +65,8 @@ Spree.ready(function ($) {orderTotal: $('#summary-order-total'),taxTotal: $('[data-hook="tax-total"]'),shipmentTotal: $('[data-hook="shipping-total"]'),- shippingMethods: $('input[data-behavior="shipping-method-selector"]')+ shippingMethods: $('input[data-behavior="shipping-method-selector"]'),+ isFreeShipping: $('[data-hook="is-free-shipping"]')}return new ShippingTotalManager(input).bindEvent()})diff --git a/frontend/app/views/spree/checkout/_summary.html.erb b/frontend/app/views/spree/checkout/_summary.html.erbindex 7a488bb1d1..7186d7b843 100644--- a/frontend/app/views/spree/checkout/_summary.html.erb+++ b/frontend/app/views/spree/checkout/_summary.html.erb@@ -28,7 +28,7 @@<% if order.passed_checkout_step?("address") && order.shipments.any? %><% if order.has_free_shipping? %><div class="d-table-cell"><%= Spree.t(:shipping) %>:</div>- <div class="d-table-cell text-right"><%= Spree.t(:free) %></div>+ <div class="d-table-cell text-right" data-hook="is-free-shipping"><%= Spree.t(:free) %></div><% else %><div class="d-table-cell"><%= Spree.t(:shipping) %>:</div><% shipping_total = Spree::Money.new(order.shipments.to_a.sum(&:cost), currency: order.currency) %>diff --git a/frontend/spec/features/delivery_spec.rb b/frontend/spec/features/delivery_spec.rbindex 23e010a26d..7b1c03a20c 100644--- a/frontend/spec/features/delivery_spec.rb+++ b/frontend/spec/features/delivery_spec.rb@@ -6,6 +6,7 @@ describe 'Delivery', type: :feature, inaccessible: true, js: true dolet(:country) { create(:country, name: 'United States of America', iso_name: 'UNITED STATES', iso: 'US') }let(:state) { create(:state, name: 'Alabama', abbr: 'AL', country: country) }let(:user) { create(:user) }+ let(:promotion) { create(:promotion, name: 'Free Shipping', starts_at: 1.day.ago, expires_at: 1.day.from_now) }let!(:shipping_method2) dosm = create(:shipping_method, name: 'Shipping Method2')sm.calculator.preferred_amount = 20@@ -36,14 +37,15 @@ describe 'Delivery', type: :feature, inaccessible: true, js: true doendit 'contains the shipping total' do- expect(page).to have_css(".checkout-content-summary-table", text: '$10.00')+ expect(page).to have_css('.checkout-content-summary-table', text: '$10.00')endcontext 'shipping method is changed' dobefore { find('label', text: shipping_method2.name).click }- it 'shipping total and order total both are updates' do- expect(page).to have_css(".checkout-content-summary-table", text: '$20.00')- end++ it 'shipping total and order total both are updates' do+ expect(page).to have_css('.checkout-content-summary-table', text: '$20.00')+ endendend@@ -53,7 +55,7 @@ describe 'Delivery', type: :feature, inaccessible: true, js: true doendit 'contains the correct address' do- expect(page).to have_text 'SHIPPING TO: ' + @street.upcase + ' - ' + @zipcode.upcase + ' - US'+ expect(page).to have_text "SHIPPING TO: #{@street.upcase} - #{@zipcode.upcase} - US"endend@@ -73,11 +75,27 @@ describe 'Delivery', type: :feature, inaccessible: true, js: true doendit 'calculates shipping total correctly with different currency marker' do- expect(page).to have_css(".checkout-content-summary-table", text: '$20,00')+ expect(page).to have_css('.checkout-content-summary-table', text: '$20,00')endit 'calculates order total correctly with different currency marker' do- expect(page).to have_css(".checkout-content-summary-table", text: '$39,99')+ expect(page).to have_css('.checkout-content-summary-table', text: '$39,99')+ end+ end++ context 'with free shipping promotion' do+ before do+ action = Spree::Promotion::Actions::FreeShipping.new+ action.promotion = promotion+ action.save+ promotion.reload++ add_mug_and_navigate_to_delivery_page+ find('label', text: shipping_method2.name).click+ end++ it 'total does not change after changing delivery option' do+ expect(page).to have_css('#summary-order-total', text: '$19.99')endend