@@ -27,10 +27,27 @@ class CheckoutHelper
2727 ];
2828
2929 /** @noinspection GlobalVariableUsageInspection */
30+
31+ public static function passOfficeToCartPackages (array $ packages ): array
32+ {
33+ return array_map (
34+ static function (array $ package ) {
35+ $ office = CheckoutHelper::getCurrentValue ('office_code ' );
36+
37+ if (!empty ($ office )) {
38+ $ package ['destination ' ][MetaKeys::OFFICE_CODE ] = $ office ;
39+ }
40+
41+ return $ package ;
42+ },
43+ $ packages ,
44+ );
45+ }
46+
3047 public static function getCurrentValue (string $ valueName , string $ defaultValue = null ): ?string
3148 {
3249 try {
33- $ cdekValue = WC ()->session ->get (Config::DELIVERY_NAME . "_ $ valueName " );
50+ $ cdekValue = WC ()->session ->get (Config::DELIVERY_NAME . "_ $ valueName " );
3451 if (!empty ($ cdekValue )) {
3552 return $ cdekValue ;
3653 }
@@ -59,7 +76,7 @@ public static function getCurrentValue(string $valueName, string $defaultValue =
5976 }
6077
6178 try {
62- $ cdekValue = WC ()->customer ->get_meta (Config::DELIVERY_NAME . "_ $ valueName " );
79+ $ cdekValue = WC ()->customer ->get_meta (Config::DELIVERY_NAME . "_ $ valueName " );
6380
6481 if (!empty ($ cdekValue )) {
6582 return $ cdekValue ;
@@ -71,19 +88,36 @@ public static function getCurrentValue(string $valueName, string $defaultValue =
7188 return $ checkout ->get_value ($ valueName ) ?: $ defaultValue ;
7289 }
7390
74- public static function passOfficeToCartPackages (array $ packages ): array {
75- return array_map (
76- static function (array $ package ) {
77- $ office = CheckoutHelper::getCurrentValue ('office_code ' );
91+ public static function restoreFields (array $ fields ): array
92+ {
93+ if (self ::getSelectedShippingRate () === null ) {
94+ return $ fields ;
95+ }
7896
79- if (!empty ($ office )) {
80- $ package ['destination ' ][MetaKeys::OFFICE_CODE ] = $ office ;
97+ $ originalFields = WC ()->checkout ()->get_checkout_fields ('billing ' );
98+
99+ foreach (self ::AVAILABLE_FIELDSETS as $ fieldset ) {
100+ $ fieldsetInstance = new $ fieldset ;
101+
102+ assert ($ fieldsetInstance instanceof FieldsetContract);
103+
104+ if (!$ fieldsetInstance ->isApplicable ()) {
105+ continue ;
106+ }
107+
108+ foreach ($ fieldsetInstance ->getFieldsNames () as $ field ) {
109+ if (empty ($ fields ['billing ' ][$ field ])) {
110+ $ fields ['billing ' ][$ field ] = empty ($ originalFields [$ field ]) ?
111+ $ fieldsetInstance ->getFieldDefinition ($ field ) : $ originalFields [$ field ];
81112 }
82113
83- return $ package ;
84- },
85- $ packages ,
86- );
114+ if ($ fieldsetInstance ->isRequiredField ($ field )) {
115+ $ fields ['billing ' ][$ field ]['required ' ] = true ;
116+ }
117+ }
118+ }
119+
120+ return $ fields ;
87121 }
88122
89123 public static function getSelectedShippingRate (?WC_Cart $ cart = null ): ?WC_Shipping_Rate
@@ -96,13 +130,17 @@ public static function getSelectedShippingRate(?WC_Cart $cart = null): ?WC_Shipp
96130 return null ;
97131 }
98132
99- $ methods = $ cart ->get_shipping_methods ();
133+ if (method_exists ($ cart , 'get_shipping_methods ' )) {
134+ $ methods = $ cart ->get_shipping_methods ();
135+ } else {
136+ $ methods = [];
137+ }
100138
101139 if (empty ($ methods )) {
102140 $ methods = $ cart ->calculate_shipping ();
103141 }
104142
105- if (is_null ($ methods )){
143+ if (is_null ($ methods )) {
106144 return null ;
107145 }
108146
@@ -120,37 +158,5 @@ public static function isShippingRateSuitable(WC_Shipping_Rate $rate): bool
120158 {
121159 return $ rate ->get_method_id () === Config::DELIVERY_NAME ;
122160 }
123-
124- public static function restoreFields (array $ fields ): array
125- {
126- if (self ::getSelectedShippingRate () === null ) {
127- return $ fields ;
128- }
129-
130- $ originalFields = WC ()->checkout ()->get_checkout_fields ('billing ' );
131-
132- foreach (self ::AVAILABLE_FIELDSETS as $ fieldset ) {
133- $ fieldsetInstance = new $ fieldset ;
134-
135- assert ($ fieldsetInstance instanceof FieldsetContract);
136-
137- if (!$ fieldsetInstance ->isApplicable ()) {
138- continue ;
139- }
140-
141- foreach ($ fieldsetInstance ->getFieldsNames () as $ field ) {
142- if (empty ($ fields ['billing ' ][$ field ])) {
143- $ fields ['billing ' ][$ field ] = empty ($ originalFields [$ field ]) ?
144- $ fieldsetInstance ->getFieldDefinition ($ field ) : $ originalFields [$ field ];
145- }
146-
147- if ($ fieldsetInstance ->isRequiredField ($ field )) {
148- $ fields ['billing ' ][$ field ]['required ' ] = true ;
149- }
150- }
151- }
152-
153- return $ fields ;
154- }
155161 }
156162}
0 commit comments