Skip to content

Commit 15e6075

Browse files
Merge branch 'bugfix/first-available-method' into release-week-22
2 parents 7776b3e + ed2a32a commit 15e6075

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/Mollie_HyvaCheckout/Observer/SalesQuoteCollectTotalsBefore/SetDefaultSelectedPaymentMethod.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Framework\Event\Observer;
1515
use Magento\Framework\Event\ObserverInterface;
1616
use Magento\Quote\Api\Data\PaymentInterfaceFactory;
17+
use Magento\Quote\Api\PaymentMethodManagementInterface;
1718
use Magento\Quote\Model\Quote;
1819
use Magento\Store\Model\ScopeInterface;
1920
use Magento\Store\Model\StoreManagerInterface;
@@ -26,19 +27,22 @@ class SetDefaultSelectedPaymentMethod implements ObserverInterface
2627
private StoreManagerInterface $storeManager;
2728
private ScopeConfigInterface $scopeConfig;
2829
private HyvaCheckoutConfig $hyvaCheckoutConfig;
30+
private PaymentMethodManagementInterface $paymentMethodManagement;
2931

3032
public function __construct(
3133
HyvaCheckoutConfig $hyvaCheckoutConfig,
3234
ScopeConfigInterface $scopeConfig,
3335
Config $config,
3436
PaymentInterfaceFactory $paymentFactory,
35-
StoreManagerInterface $storeManager
37+
StoreManagerInterface $storeManager,
38+
PaymentMethodManagementInterface $paymentMethodManagement,
3639
) {
3740
$this->paymentFactory = $paymentFactory;
3841
$this->config = $config;
3942
$this->storeManager = $storeManager;
4043
$this->scopeConfig = $scopeConfig;
4144
$this->hyvaCheckoutConfig = $hyvaCheckoutConfig;
45+
$this->paymentMethodManagement = $paymentMethodManagement;
4246
}
4347

4448
public function execute(Observer $observer): void
@@ -56,7 +60,11 @@ public function execute(Observer $observer): void
5660
return;
5761
}
5862

59-
if (!$this->isMethodActive($defaultMethod)) {
63+
if ($defaultMethod == 'first_mollie_method') {
64+
$defaultMethod = $this->getFirstAvailableMollieMethod($quote);
65+
}
66+
67+
if ($defaultMethod && !$this->isMethodActive($defaultMethod)) {
6068
return;
6169
}
6270

@@ -66,10 +74,11 @@ public function execute(Observer $observer): void
6674
}
6775

6876
/** @var \Magento\Quote\Api\Data\PaymentInterface $payment */
69-
$payment = $this->paymentFactory->create();
77+
$payment = $quote->getPayment() ?: $this->paymentFactory->create();
7078
$payment->setMethod($defaultMethod);
7179

7280
$quote->setPayment($payment);
81+
$this->paymentMethodManagement->set($quote->getId(), $payment);
7382
}
7483

7584
/**
@@ -93,4 +102,21 @@ private function quoteHasActivePaymentMethod(Quote $quote): bool
93102
{
94103
return $quote->getPayment()->getMethod() !== null;
95104
}
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+
}
96122
}

0 commit comments

Comments
 (0)