Browse Source

fix(core): Re-evaluate shipping when all OrderLines removed

Fixes #1441
Michael Bromley 3 years ago
parent
commit
19a554d39b

+ 46 - 0
packages/core/e2e/shop-order.e2e-spec.ts

@@ -2013,6 +2013,52 @@ describe('Shop orders', () => {
             const result = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
             expect(result.activeOrder?.shippingLines).toEqual([]);
         });
+
+        // https://github.com/vendure-ecommerce/vendure/issues/1441
+        it('shipping methods are re-evaluated when all OrderLines are removed', async () => {
+            const { createShippingMethod } = await adminClient.query<
+                CreateShippingMethod.Mutation,
+                CreateShippingMethod.Variables
+            >(CREATE_SHIPPING_METHOD, {
+                input: {
+                    code: `min-price-shipping`,
+                    translations: [
+                        { languageCode: LanguageCode.en, name: `min price shipping`, description: '' },
+                    ],
+                    fulfillmentHandler: manualFulfillmentHandler.code,
+                    checker: {
+                        code: defaultShippingEligibilityChecker.code,
+                        arguments: [{ name: 'orderMinimum', value: '100' }],
+                    },
+                    calculator: {
+                        code: defaultShippingCalculator.code,
+                        arguments: [
+                            { name: 'rate', value: '1000' },
+                            { name: 'taxRate', value: '0' },
+                            { name: 'includesTax', value: 'auto' },
+                        ],
+                    },
+                },
+            });
+            const minPriceShippingMethodId = createShippingMethod.id;
+
+            await shopClient.query<SetShippingMethod.Mutation, SetShippingMethod.Variables>(
+                SET_SHIPPING_METHOD,
+                {
+                    id: minPriceShippingMethodId,
+                },
+            );
+            const result1 = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
+            expect(result1.activeOrder?.shippingLines[0].shippingMethod.id).toBe(minPriceShippingMethodId);
+
+            const { removeAllOrderLines } = await shopClient.query<
+                RemoveAllOrderLines.Mutation,
+                RemoveAllOrderLines.Variables
+            >(REMOVE_ALL_ORDER_LINES);
+            orderResultGuard.assertSuccess(removeAllOrderLines);
+            expect(removeAllOrderLines.shippingLines.length).toBe(0);
+            expect(removeAllOrderLines.shippingWithTax).toBe(0);
+        });
     });
 });
 

+ 4 - 4
packages/core/src/service/helpers/order-calculator/order-calculator.ts

@@ -85,10 +85,10 @@ export class OrderCalculator {
                 // altered the unit prices, which in turn will alter the tax payable.
                 await this.applyTaxes(ctx, order, activeTaxZone);
             }
-            if (options?.recalculateShipping !== false) {
-                await this.applyShipping(ctx, order);
-                await this.applyShippingPromotions(ctx, order, promotions);
-            }
+        }
+        if (options?.recalculateShipping !== false) {
+            await this.applyShipping(ctx, order);
+            await this.applyShippingPromotions(ctx, order, promotions);
         }
         this.calculateOrderTotals(order);
         return taxZoneChanged ? order.getOrderItems() : Array.from(updatedOrderItems);