14
14
use Magento \Framework \Event \Observer ;
15
15
use Magento \Framework \Event \ObserverInterface ;
16
16
use Magento \Quote \Api \Data \PaymentInterfaceFactory ;
17
+ use Magento \Quote \Api \PaymentMethodManagementInterface ;
17
18
use Magento \Quote \Model \Quote ;
18
19
use Magento \Store \Model \ScopeInterface ;
19
20
use Magento \Store \Model \StoreManagerInterface ;
@@ -26,19 +27,22 @@ class SetDefaultSelectedPaymentMethod implements ObserverInterface
26
27
private StoreManagerInterface $ storeManager ;
27
28
private ScopeConfigInterface $ scopeConfig ;
28
29
private HyvaCheckoutConfig $ hyvaCheckoutConfig ;
30
+ private PaymentMethodManagementInterface $ paymentMethodManagement ;
29
31
30
32
public function __construct (
31
33
HyvaCheckoutConfig $ hyvaCheckoutConfig ,
32
34
ScopeConfigInterface $ scopeConfig ,
33
35
Config $ config ,
34
36
PaymentInterfaceFactory $ paymentFactory ,
35
- StoreManagerInterface $ storeManager
37
+ StoreManagerInterface $ storeManager ,
38
+ PaymentMethodManagementInterface $ paymentMethodManagement ,
36
39
) {
37
40
$ this ->paymentFactory = $ paymentFactory ;
38
41
$ this ->config = $ config ;
39
42
$ this ->storeManager = $ storeManager ;
40
43
$ this ->scopeConfig = $ scopeConfig ;
41
44
$ this ->hyvaCheckoutConfig = $ hyvaCheckoutConfig ;
45
+ $ this ->paymentMethodManagement = $ paymentMethodManagement ;
42
46
}
43
47
44
48
public function execute (Observer $ observer ): void
@@ -56,7 +60,11 @@ public function execute(Observer $observer): void
56
60
return ;
57
61
}
58
62
59
- if (!$ this ->isMethodActive ($ defaultMethod )) {
63
+ if ($ defaultMethod == 'first_mollie_method ' ) {
64
+ $ defaultMethod = $ this ->getFirstAvailableMollieMethod ($ quote );
65
+ }
66
+
67
+ if ($ defaultMethod && !$ this ->isMethodActive ($ defaultMethod )) {
60
68
return ;
61
69
}
62
70
@@ -66,10 +74,11 @@ public function execute(Observer $observer): void
66
74
}
67
75
68
76
/** @var \Magento\Quote\Api\Data\PaymentInterface $payment */
69
- $ payment = $ this ->paymentFactory ->create ();
77
+ $ payment = $ quote -> getPayment () ?: $ this ->paymentFactory ->create ();
70
78
$ payment ->setMethod ($ defaultMethod );
71
79
72
80
$ quote ->setPayment ($ payment );
81
+ $ this ->paymentMethodManagement ->set ($ quote ->getId (), $ payment );
73
82
}
74
83
75
84
/**
@@ -93,4 +102,21 @@ private function quoteHasActivePaymentMethod(Quote $quote): bool
93
102
{
94
103
return $ quote ->getPayment ()->getMethod () !== null ;
95
104
}
105
+
106
+ private function getFirstAvailableMollieMethod (Quote $ quote ): ?string
107
+ {
108
+ $ methods = $ this ->paymentMethodManagement ->getList ($ quote ->getId ());
109
+
110
+ foreach ($ methods as $ method ) {
111
+ $ methodCode = $ method ->getCode ();
112
+ if (strpos ($ methodCode , 'mollie_ ' ) === 0 &&
113
+ $ methodCode != 'mollie_methods_applepay ' &&
114
+ $ this ->isMethodActive ($ methodCode )
115
+ ) {
116
+ return $ methodCode ;
117
+ }
118
+ }
119
+
120
+ return null ;
121
+ }
96
122
}
0 commit comments