Skip to content

Commit

Permalink
Merge pull request #1169 from verenaroe/extend-payment-method
Browse files Browse the repository at this point in the history
Added mall.cart.extendAvailablePaymentMethods event
  • Loading branch information
tobias-kuendig authored Sep 13, 2024
2 parents 70974d9 + 5b81cb7 commit 41e6317
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion components/PaymentMethodSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ protected function setData()

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

$this->setVar('methods', PaymentMethod::orderBy('sort_order', 'ASC')->get());
$this->setVar('paymentMethods', PaymentMethod::getAvailableByCart($this->cart));
$this->setVar('customerMethods', $this->getCustomerMethods());
$this->setVar('activeMethod', $method);
$this->setVar('shippingSelectionBeforePayment', GeneralSettings::get('shipping_selection_before_payment', false)); // Needed by themes
Expand Down
4 changes: 3 additions & 1 deletion components/QuickCheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,9 @@ protected function setData()
$paymentMethod = PaymentMethod::getDefault();
$cart->setPaymentMethod($paymentMethod);
}
$this->setVar('paymentMethods', PaymentMethod::orderBy('sort_order', 'ASC')->get());

$this->setVar('paymentMethods', PaymentMethod::getAvailableByCart($cart));

$this->setVar('customerPaymentMethods', $this->getCustomerMethods());

// $this->setVar('dataLayer', $this->handleDataLayer());
Expand Down
2 changes: 1 addition & 1 deletion docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default defineConfig({
},
siteTitle: false,
editLink: {
pattern: 'https://github.com/OFFLINE-GmbH/oc-mall-plugin/edit/develop/src/docs/:path',
pattern: 'https://github.com/OFFLINE-GmbH/oc-mall-plugin/tree/next/docs/:path',
text: 'Edit this page on GitHub'
},
lastUpdated: {
Expand Down
4 changes: 4 additions & 0 deletions docs/development/core/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ This event is emitted when the quantity of a cart product has changed. It receiv
* `oldQuantity` the old quantity value
* `newQuantity` the new quantity value

### `mall.cart.extendAvailablePaymentMethods`

This event allows to customize the payment methods based on the current cart.

## Checkout

### `mall.checkout.succeeded`
Expand Down
16 changes: 16 additions & 0 deletions models/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use October\Rain\Database\Traits\Sortable;
use October\Rain\Database\Traits\Validation;
use October\Rain\Parse\Twig;
use October\Rain\Support\Facades\Event;
use OFFLINE\Mall\Classes\Database\IsStates;
use OFFLINE\Mall\Classes\Payments\PaymentGateway;
use OFFLINE\Mall\Classes\Totals\PaymentTotal;
Expand Down Expand Up @@ -206,6 +207,21 @@ public static function getDefault(): ?self
return $default;
}

/**
* Get all available payment methods.
* @param Cart $cart
* @return mixed
*/
public static function getAvailableByCart(Cart $cart)
{
$results = array_filter(Event::fire('mall.cart.extendAvailablePaymentMethods', [$cart]) ?? []);
if (count($results) > 0) {
return $results[0];
}

return PaymentMethod::orderBy('sort_order', 'ASC')->get();
}

/**
* Include all payment methods, even disabled ones.
* @param Builder $query
Expand Down

0 comments on commit 41e6317

Please sign in to comment.