| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Hooks\Handlers\CleanupHandlers; |
| 4 |
|
| 5 |
use FluentBooking\App\Models\Booking; |
| 6 |
use FluentBookingPro\App\Models\Transactions; |
| 7 |
use FluentBookingPro\App\Models\OrderItems; |
| 8 |
|
| 9 |
class OrderCleaner |
| 10 |
{ |
| 11 |
public function register() |
| 12 |
{ |
| 13 |
add_action('fluent_booking/before_delete_order', [$this, 'handleBeforeDelete'], 10, 2); |
| 14 |
} |
| 15 |
|
| 16 |
public function handleBeforeDelete($order, $booking) |
| 17 |
{ |
| 18 |
if (empty($order)) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
Transactions::query() |
| 23 |
->where('object_type', 'order') |
| 24 |
->where('object_id', $order->id) |
| 25 |
->delete(); |
| 26 |
|
| 27 |
OrderItems::query()->where('order_id', $order->id) |
| 28 |
->when($booking, function ($query, $booking) { |
| 29 |
$query->where('booking_id', $booking->id); |
| 30 |
})->delete(); |
| 31 |
|
| 32 |
} |
| 33 |
} |