Skip to content

Commit 5b81cb7

Browse files
committed
Added mall.cart.extendAvailablePaymentMethods event
1 parent 70974d9 commit 5b81cb7

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

components/PaymentMethodSelector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ protected function setData()
257257

258258
$method = PaymentMethod::where('id', $this->order->payment_method_id ?? $this->cart->payment_method_id)->first();
259259

260-
$this->setVar('methods', PaymentMethod::orderBy('sort_order', 'ASC')->get());
260+
$this->setVar('paymentMethods', PaymentMethod::getAvailableByCart($this->cart));
261261
$this->setVar('customerMethods', $this->getCustomerMethods());
262262
$this->setVar('activeMethod', $method);
263263
$this->setVar('shippingSelectionBeforePayment', GeneralSettings::get('shipping_selection_before_payment', false)); // Needed by themes

components/QuickCheckout.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,9 @@ protected function setData()
505505
$paymentMethod = PaymentMethod::getDefault();
506506
$cart->setPaymentMethod($paymentMethod);
507507
}
508-
$this->setVar('paymentMethods', PaymentMethod::orderBy('sort_order', 'ASC')->get());
508+
509+
$this->setVar('paymentMethods', PaymentMethod::getAvailableByCart($cart));
510+
509511
$this->setVar('customerPaymentMethods', $this->getCustomerMethods());
510512

511513
// $this->setVar('dataLayer', $this->handleDataLayer());

docs/.vitepress/config.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default defineConfig({
1818
},
1919
siteTitle: false,
2020
editLink: {
21-
pattern: 'https://github.com/OFFLINE-GmbH/oc-mall-plugin/edit/develop/src/docs/:path',
21+
pattern: 'https://github.com/OFFLINE-GmbH/oc-mall-plugin/tree/next/docs/:path',
2222
text: 'Edit this page on GitHub'
2323
},
2424
lastUpdated: {

docs/development/core/events.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ This event is emitted when the quantity of a cart product has changed. It receiv
8181
* `oldQuantity` the old quantity value
8282
* `newQuantity` the new quantity value
8383

84+
### `mall.cart.extendAvailablePaymentMethods`
85+
86+
This event allows to customize the payment methods based on the current cart.
87+
8488
## Checkout
8589

8690
### `mall.checkout.succeeded`

models/PaymentMethod.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use October\Rain\Database\Traits\Sortable;
1616
use October\Rain\Database\Traits\Validation;
1717
use October\Rain\Parse\Twig;
18+
use October\Rain\Support\Facades\Event;
1819
use OFFLINE\Mall\Classes\Database\IsStates;
1920
use OFFLINE\Mall\Classes\Payments\PaymentGateway;
2021
use OFFLINE\Mall\Classes\Totals\PaymentTotal;
@@ -206,6 +207,21 @@ public static function getDefault(): ?self
206207
return $default;
207208
}
208209

210+
/**
211+
* Get all available payment methods.
212+
* @param Cart $cart
213+
* @return mixed
214+
*/
215+
public static function getAvailableByCart(Cart $cart)
216+
{
217+
$results = array_filter(Event::fire('mall.cart.extendAvailablePaymentMethods', [$cart]) ?? []);
218+
if (count($results) > 0) {
219+
return $results[0];
220+
}
221+
222+
return PaymentMethod::orderBy('sort_order', 'ASC')->get();
223+
}
224+
209225
/**
210226
* Include all payment methods, even disabled ones.
211227
* @param Builder $query

0 commit comments

Comments
 (0)