Skip to content

Commit

Permalink
Added the UpdateCouponUsage listener
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Aug 24, 2024
1 parent d955bc9 commit 7958a02
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
- `CouponAdded` (to checkout)
- `CouponRemoved` (from checkout)
- `CouponUtilized` (after a successful checkout converted to an order)

- Added a listener to update coupon and promo usage after a `CouponUtilized` event (Foundation)
- Added a promotion calculation listener (on CouponAdded and CouponRemoved events)

## 4.1.0
##### 2024-07-11
Expand Down
23 changes: 23 additions & 0 deletions src/Foundation/Listeners/UpdateCouponUsage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Vanilo\Foundation\Listeners;

use Illuminate\Support\Facades\DB;
use Vanilo\Checkout\Contracts\CouponEvent;
use Vanilo\Promotion\Models\CouponProxy;

class UpdateCouponUsage
{
public function handle(CouponEvent $event): void
{
if (null !== $coupon = CouponProxy::findByCode($event->getCouponCode())) {
DB::transaction(function () use ($coupon) {
$coupon->usage_count += 1;
$promotion = $coupon->getPromotion();
$promotion->usage_count += 1;
$coupon->save();
$promotion->save();
});
}
}
}
5 changes: 5 additions & 0 deletions src/Foundation/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
use Vanilo\Checkout\Events\BillpayerChanged;
use Vanilo\Checkout\Events\CouponAdded;
use Vanilo\Checkout\Events\CouponRemoved;
use Vanilo\Checkout\Events\CouponUtilized;
use Vanilo\Checkout\Events\ShippingAddressChanged;
use Vanilo\Checkout\Events\ShippingMethodSelected;
use Vanilo\Foundation\Listeners\CalculatePromotions;
use Vanilo\Foundation\Listeners\CalculateShippingFees;
use Vanilo\Foundation\Listeners\CalculateTaxes;
use Vanilo\Foundation\Listeners\DeleteCartAdjustments;
use Vanilo\Foundation\Listeners\UpdateCouponUsage;
use Vanilo\Foundation\Listeners\UpdateSalesFigures;
use Vanilo\Order\Events\OrderWasCreated;

Expand Down Expand Up @@ -57,6 +59,9 @@ class EventServiceProvider extends ServiceProvider
],
CouponRemoved::class => [
CalculatePromotions::class,
],
CouponUtilized::class => [
UpdateCouponUsage::class,
]
];
}

0 comments on commit 7958a02

Please sign in to comment.