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.

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.js
index 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 = input1
this.shippingMethods = this.input.shippingMethods
this.shipmentTotal = this.input.shipmentTotal
+ this.isFreeShipping = this.input.isFreeShipping
this.taxTotal = this.input.taxTotal
this.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.erb
index 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.rb
index 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 do
let(: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) do
sm = create(:shipping_method, name: 'Shipping Method2')
sm.calculator.preferred_amount = 20
@@ -36,14 +37,15 @@ describe 'Delivery', type: :feature, inaccessible: true, js: true do
end
it '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')
end
context 'shipping method is changed' do
before { 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')
+ end
end
end
@@ -53,7 +55,7 @@ describe 'Delivery', type: :feature, inaccessible: true, js: true do
end
it '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"
end
end
@@ -73,11 +75,27 @@ describe 'Delivery', type: :feature, inaccessible: true, js: true do
end
it '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')
end
it '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')
end
end