Skip to content

Commit

Permalink
Added the PaymentMethodSelected checkout event
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Nov 11, 2024
1 parent 4fa1512 commit b0734ce
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- `CouponAdded` (to checkout)
- `CouponRemoved` (from checkout)
- `CouponUtilized` (after a successful checkout converted to an order)
- `PaymentMethodSelected`
- Added a listener to update coupon and promo usage after a `CouponUtilized` event (Foundation)
- Added a promotion calculation listener (on CouponAdded and CouponRemoved events)
- Added the `PercentDiscount` adjuster class
Expand Down
1 change: 1 addition & 0 deletions src/Checkout/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- `CouponAdded` (to checkout)
- `CouponRemoved` (from checkout)
- `CouponUtilized` (after a successful checkout converted to an order)
- `PaymentMethodSelected`

## 4.1.0
##### 2024-07-11
Expand Down
6 changes: 6 additions & 0 deletions src/Checkout/Drivers/BaseCheckoutStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Vanilo\Checkout\Events\BillpayerChanged;
use Vanilo\Checkout\Events\CouponAdded;
use Vanilo\Checkout\Events\CouponRemoved;
use Vanilo\Checkout\Events\PaymentMethodSelected;
use Vanilo\Checkout\Events\ShippingMethodSelected;
use Vanilo\Checkout\Models\CheckoutStateProxy;
use Vanilo\Checkout\Traits\EmulatesFillAttributes;
Expand Down Expand Up @@ -154,7 +155,12 @@ public function getPaymentMethodId(): null|int|string

public function setPaymentMethodId(null|int|string $paymentMethodId): void
{
$old = $this->getPaymentMethodId();
$this->writeRawDataToStore('payment_method_id', $paymentMethodId);

if ($old !== $paymentMethodId) {
Event::dispatch(new PaymentMethodSelected($this, $old));
}
}

public function getNotes(): ?string
Expand Down
41 changes: 41 additions & 0 deletions src/Checkout/Events/PaymentMethodSelected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/**
* Contains the PaymentMethodSelected event class.
*
* @copyright Copyright (c) 2024 Vanilo UG
* @author Attila Fulop
* @license MIT
* @since 2024-11-11
*
*/

namespace Vanilo\Checkout\Events;

use Vanilo\Checkout\Contracts\Checkout;

class PaymentMethodSelected extends BaseCheckoutEvent
{
protected null|int|string $oldPaymentMethodId;

protected null|int|string $newPaymentMethodId;

public function __construct(Checkout $checkout, null|int|string $oldPaymentMethodId)
{
parent::__construct($checkout);
$this->oldPaymentMethodId = $oldPaymentMethodId;
$this->newPaymentMethodId = $checkout->getPaymentMethodId();
}

public function selectedPaymentMethodId(): null|int|string
{
return $this->newPaymentMethodId;
}

public function oldPaymentMethodId(): null|int|string
{
return $this->oldPaymentMethodId;
}
}

0 comments on commit b0734ce

Please sign in to comment.