2
2
3
3
namespace DoubleThreeDigital \SimpleCommerce \UpdateScripts \v6_0 ;
4
4
5
+ use DoubleThreeDigital \SimpleCommerce \Contracts \Order as OrderContract ;
5
6
use DoubleThreeDigital \SimpleCommerce \Facades \Order ;
7
+ use Illuminate \Support \Arr ;
8
+ use Illuminate \Support \Collection ;
9
+ use Illuminate \Support \Str ;
6
10
use Statamic \UpdateScripts \UpdateScript ;
7
11
8
12
class UpdateClassReferences extends UpdateScript
@@ -14,24 +18,54 @@ public function shouldUpdate($newVersion, $oldVersion)
14
18
15
19
public function update ()
16
20
{
17
- Order::query ()
18
- ->where ('gateway ' , '!= ' , null )
19
- ->chunk (50 , function ($ orders ) {
20
- $ orders ->each (function ($ order ) {
21
- // When the gateway reference is still a class, change it to the handle.
22
- if ($ order ->gateway () && class_exists ($ order ->gateway ()['use ' ])) {
23
- $ order ->gateway (array_merge ($ order ->gateway (), [
24
- 'use ' => $ order ->gateway ()['use ' ]::handle (),
25
- ]));
26
-
27
- $ order ->save ();
21
+ $ this
22
+ ->updateReferencesToGateways ()
23
+ ->updateReferencesToShippingMethods ();
24
+ }
25
+
26
+ protected function updateReferencesToGateways (): self
27
+ {
28
+ Order::query ()->whereNotNull ('gateway ' )->chunk (100 , function (Collection $ orders ) {
29
+ $ orders
30
+ ->filter (fn (OrderContract $ order ) => str_contains (Arr::get ($ order ->gateway , 'use ' ), '\\' ))
31
+ ->each (function (OrderContract $ order ) {
32
+ $ class = Arr::get ($ order ->gateway , 'use ' );
33
+
34
+ // Adjust the class name before new'ing it up since the namespace has changed.
35
+ if (Str::startsWith ($ class , 'DoubleThreeDigital ' )) {
36
+ // $class = str_replace('DoubleThreeDigital', 'DuncanMcClean', $class);
28
37
}
29
38
30
- // When the shipping method reference is still a class, change it to the handle.
31
- if ($ order ->has ('shipping_method ' ) && class_exists ($ order ->get ('shipping_method ' ))) {
32
- $ order ->set ('shipping_method ' , $ order ->get ('shipping_method ' )::handle ())->saveQuietly ();
39
+ $ handle = $ class ::handle ();
40
+
41
+ $ order ->gatewayData (gateway: $ handle );
42
+ $ order ->save ();
43
+ });
44
+ });
45
+
46
+ return $ this ;
47
+ }
48
+
49
+ protected function updateReferencesToShippingMethods (): self
50
+ {
51
+ Order::query ()->whereNotNull ('shipping_method ' )->chunk (100 , function (Collection $ orders ) {
52
+ $ orders
53
+ ->filter (fn (OrderContract $ order ) => str_contains ($ order ->get ('shipping_method ' ), '\\' ))
54
+ ->each (function (OrderContract $ order ) {
55
+ $ class = $ order ->get ('shipping_method ' );
56
+
57
+ // Adjust the class name before new'ing it up since the namespace has changed.
58
+ if (Str::startsWith ($ class , 'DoubleThreeDigital ' )) {
59
+ // $class = str_replace('DoubleThreeDigital', 'DuncanMcClean', $class);
33
60
}
61
+
62
+ $ handle = $ class ::handle ();
63
+
64
+ $ order ->set ('shipping_method ' , $ handle );
65
+ $ order ->save ();
34
66
});
35
- });
67
+ });
68
+
69
+ return $ this ;
36
70
}
37
71
}
0 commit comments