@@ -40,12 +40,15 @@ class Packlink_Shipping_Method extends \WC_Shipping_Method {
4040 * @var array
4141 */
4242 private static $ shipping_services = array ();
43+
44+
4345 /**
44- * Available shipping services loaded .
46+ * Last calculated parameters by cache key .
4547 *
46- * @var bool
48+ * @var array
4749 */
48- private static $ loaded = false ;
50+ private static $ last_shipping_params = array ();
51+
4952 /**
5053 * Pricing policy.
5154 *
@@ -372,24 +375,93 @@ private function load_shipping_costs( array $package, ShippingMethod $shipping_m
372375 $ id = $ shipping_method ->getId ();
373376 $ to_country = ! empty ( $ package ['destination ' ]['country ' ] ) ? $ package ['destination ' ]['country ' ] : $ warehouse ->country ;
374377 $ to_zip = ! empty ( $ package ['destination ' ]['postcode ' ] ) ? $ package ['destination ' ]['postcode ' ] : $ warehouse ->postalCode ;
375- if ( ! static ::$ loaded ) {
378+ $ parcels = $ this ->build_parcels ( $ package , $ default_parcel );
379+
380+ $ new_params = $ this ->get_shipping_calculation_params (
381+ $ package ,
382+ $ parcels ,
383+ $ warehouse ->country ,
384+ $ warehouse ->postalCode ,
385+ $ to_country ,
386+ $ to_zip ,
387+ $ cart_subtotal
388+ );
389+
390+
391+ if ( static ::$ last_shipping_params !== $ new_params ) {
376392 static ::$ shipping_services = ShippingCostCalculator::getShippingCosts (
377393 $ this ->shipping_method_service ->getAllMethods (),
378394 $ warehouse ->country ,
379395 $ warehouse ->postalCode ,
380396 $ to_country ,
381397 $ to_zip ,
382- $ this -> build_parcels ( $ package , $ default_parcel ) ,
398+ $ parcels ,
383399 $ cart_subtotal ,
384400 System_Info_Service::SYSTEM_ID
385401 );
386402
387- static ::$ loaded = true ;
403+ static ::$ last_shipping_params = $ new_params ;
388404 }
389405
390406 return array_key_exists ( $ id , static ::$ shipping_services ) || ( - 1 === $ id && ! empty ( static ::$ shipping_services ) );
391407 }
392408
409+ /**
410+ * Builds normalized shipping calculation parameters.
411+ *
412+ * @param array $package Package.
413+ * @param array $parcels Parcels.
414+ * @param string $from_country Origin country.
415+ * @param string $from_zip Origin postal code.
416+ * @param string $to_country Destination country.
417+ * @param string $to_zip Destination postal code.
418+ * @param float $cart_subtotal Cart subtotal.
419+ *
420+ * @return array
421+ */
422+ private function get_shipping_calculation_params (
423+ array $ package ,
424+ array $ parcels ,
425+ string $ from_country ,
426+ string $ from_zip ,
427+ string $ to_country ,
428+ string $ to_zip ,
429+ float $ cart_subtotal
430+ ) {
431+ $ contents = array ();
432+
433+ foreach ( $ package ['contents ' ] as $ item ) {
434+ $ contents [] = array (
435+ 'product_id ' => $ item ['product_id ' ] ?? null ,
436+ 'variation_id ' => $ item ['variation_id ' ] ?? null ,
437+ 'quantity ' => isset ( $ item ['quantity ' ] ) ? (int ) $ item ['quantity ' ] : 0 ,
438+ 'line_total ' => isset ( $ item ['line_total ' ] ) ? (float ) $ item ['line_total ' ] : 0.0 ,
439+ );
440+ }
441+
442+ $ normalized_parcels = array_map (
443+ static function ( $ parcel ) {
444+ return array (
445+ 'weight ' => isset ( $ parcel ->weight ) ? (float ) $ parcel ->weight : 0.0 ,
446+ 'width ' => isset ( $ parcel ->width ) ? (float ) $ parcel ->width : 0.0 ,
447+ 'height ' => isset ( $ parcel ->height ) ? (float ) $ parcel ->height : 0.0 ,
448+ 'length ' => isset ( $ parcel ->length ) ? (float ) $ parcel ->length : 0.0 ,
449+ );
450+ },
451+ $ parcels
452+ );
453+
454+ return array (
455+ 'from_country ' => $ from_country ,
456+ 'from_zip ' => $ from_zip ,
457+ 'to_country ' => $ to_country ,
458+ 'to_zip ' => $ to_zip ,
459+ 'cart_subtotal ' => $ cart_subtotal ,
460+ 'contents ' => $ contents ,
461+ 'parcels ' => $ normalized_parcels ,
462+ );
463+ }
464+
393465 /**
394466 * Retrieves the cart subtotal to be used for shipping cost calculation.
395467 *
0 commit comments