88// distributed under the License is distributed on an "AS IS" BASIS,
99// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010
11+ import 'dart:developer' ;
12+
1113import 'package:flutter/material.dart' ;
1214import 'package:label_storemax/app_state_options.dart' ;
1315import 'package:label_storemax/helpers/tools.dart' ;
@@ -65,23 +67,92 @@ class _CheckoutShippingTypePageState extends State<CheckoutShippingTypePage> {
6567 .firstWhere ((c) => c['name' ] == state, orElse: () => null );
6668
6769 for (final shipping in wsShipping) {
70+ if (shipping.locations == null ) {
71+ continue ;
72+ }
6873 Locations location = shipping.locations.firstWhere (
69- (ws) => (ws.type == "state" &&
70- stateMap["code" ] != null &&
71- ws.code == "$countryCode :" + stateMap["code" ] ||
72- ws.code == postalCode ||
73- ws.code == countryCode),
74- orElse: () => null );
74+ (ws) => (ws.type == "state" &&
75+ stateMap != null &&
76+ stateMap["code" ] != null &&
77+ ws.code == "$countryCode :" + stateMap["code" ] ||
78+ ws.code == postalCode ||
79+ ws.code == countryCode),
80+ orElse: () => null ,
81+ );
7582
7683 if (location != null ) {
7784 _shipping = shipping;
7885 break ;
7986 }
8087 }
8188
82- if (_shipping != null && _shipping.methods != null ) {
83- if (_shipping.methods.flatRate != null ) {
84- _shipping.methods.flatRate
89+ _handleShippingZones (_shipping);
90+
91+ if (_shipping == null ) {
92+ WSShipping noZones = wsShipping
93+ .firstWhere ((element) => element.parentId == 0 , orElse: () => null );
94+ _handleShippingZones (noZones);
95+ }
96+ if (_wsShippingOptions.length == 0 ) {
97+ _isShippingSupported = false ;
98+ }
99+
100+ setState (() {
101+ _isLoading = false ;
102+ });
103+ }
104+
105+ Future <String > _getShippingPrice (int index) async {
106+ double total = 0 ;
107+ List <CartLineItem > cartLineItem = await Cart .getInstance.getCart ();
108+
109+ total +=
110+ await workoutShippingCostWC (sum: _wsShippingOptions[index]['cost' ]);
111+
112+ switch (_wsShippingOptions[index]['method_id' ]) {
113+ case "flat_rate" :
114+ FlatRate flatRate = (_wsShippingOptions[index]['object' ] as FlatRate );
115+
116+ if (cartLineItem.firstWhere (
117+ (t) => t.shippingClassId == null || t.shippingClassId == "0" ,
118+ orElse: () => null ) !=
119+ null ) {
120+ total += await workoutShippingClassCostWC (
121+ sum: flatRate.classCost,
122+ cartLineItem: cartLineItem
123+ .where ((t) =>
124+ t.shippingClassId == null || t.shippingClassId == "0" )
125+ .toList ());
126+ }
127+
128+ List <CartLineItem > cItemsWithShippingClasses = cartLineItem
129+ .where ((t) => t.shippingClassId != null && t.shippingClassId != "0" )
130+ .toList ();
131+ for (int i = 0 ; i < cItemsWithShippingClasses.length; i++ ) {
132+ ShippingClasses shippingClasses = flatRate.shippingClasses.firstWhere (
133+ (d) => d.id == cItemsWithShippingClasses[i].shippingClassId,
134+ orElse: () => null );
135+ if (shippingClasses != null ) {
136+ double classTotal = await workoutShippingClassCostWC (
137+ sum: shippingClasses.cost,
138+ cartLineItem: cartLineItem
139+ .where ((g) => g.shippingClassId == shippingClasses.id)
140+ .toList ());
141+ total += classTotal;
142+ }
143+ }
144+
145+ break ;
146+ default :
147+ break ;
148+ }
149+ return (total).toString ();
150+ }
151+
152+ _handleShippingZones (WSShipping shipping) async {
153+ if (shipping != null && shipping.methods != null ) {
154+ if (shipping.methods.flatRate != null ) {
155+ shipping.methods.flatRate
85156 .where ((t) => t != null )
86157 .toList ()
87158 .forEach ((flatRate) {
@@ -97,8 +168,8 @@ class _CheckoutShippingTypePageState extends State<CheckoutShippingTypePage> {
97168 });
98169 }
99170
100- if (_shipping .methods.localPickup != null ) {
101- _shipping .methods.localPickup
171+ if (shipping .methods.localPickup != null ) {
172+ shipping .methods.localPickup
102173 .where ((t) => t != null )
103174 .toList ()
104175 .forEach ((localPickup) {
@@ -114,9 +185,9 @@ class _CheckoutShippingTypePageState extends State<CheckoutShippingTypePage> {
114185 });
115186 }
116187
117- if (_shipping .methods.freeShipping != null ) {
188+ if (shipping .methods.freeShipping != null ) {
118189 List <FreeShipping > freeShipping =
119- _shipping .methods.freeShipping.where ((t) => t != null ).toList ();
190+ shipping .methods.freeShipping.where ((t) => t != null ).toList ();
120191
121192 for (int i = 0 ; i < freeShipping.length; i++ ) {
122193 if (isNumeric (freeShipping[i].cost) ||
@@ -148,61 +219,6 @@ class _CheckoutShippingTypePageState extends State<CheckoutShippingTypePage> {
148219 }
149220 }
150221 }
151-
152- if (_wsShippingOptions.length == 0 ) {
153- _isShippingSupported = false ;
154- }
155-
156- setState (() {
157- _isLoading = false ;
158- });
159- }
160-
161- Future <String > _getShippingPrice (int index) async {
162- double total = 0 ;
163- List <CartLineItem > cartLineItem = await Cart .getInstance.getCart ();
164-
165- total +=
166- await workoutShippingCostWC (sum: _wsShippingOptions[index]['cost' ]);
167-
168- switch (_wsShippingOptions[index]['method_id' ]) {
169- case "flat_rate" :
170- FlatRate flatRate = (_wsShippingOptions[index]['object' ] as FlatRate );
171-
172- if (cartLineItem.firstWhere (
173- (t) => t.shippingClassId == null || t.shippingClassId == "0" ,
174- orElse: () => null ) !=
175- null ) {
176- total += await workoutShippingClassCostWC (
177- sum: flatRate.classCost,
178- cartLineItem: cartLineItem
179- .where ((t) =>
180- t.shippingClassId == null || t.shippingClassId == "0" )
181- .toList ());
182- }
183-
184- List <CartLineItem > cItemsWithShippingClasses = cartLineItem
185- .where ((t) => t.shippingClassId != null && t.shippingClassId != "0" )
186- .toList ();
187- for (int i = 0 ; i < cItemsWithShippingClasses.length; i++ ) {
188- ShippingClasses shippingClasses = flatRate.shippingClasses.firstWhere (
189- (d) => d.id == cItemsWithShippingClasses[i].shippingClassId,
190- orElse: () => null );
191- if (shippingClasses != null ) {
192- double classTotal = await workoutShippingClassCostWC (
193- sum: shippingClasses.cost,
194- cartLineItem: cartLineItem
195- .where ((g) => g.shippingClassId == shippingClasses.id)
196- .toList ());
197- total += classTotal;
198- }
199- }
200-
201- break ;
202- default :
203- break ;
204- }
205- return (total).toString ();
206222 }
207223
208224 @override
0 commit comments