|
@@ -1,67 +0,0 @@
|
|
|
-<?php
|
|
|
|
|
-
|
|
|
|
|
-namespace Longyi\Gift\Listeners;
|
|
|
|
|
-
|
|
|
|
|
-use Webkul\Checkout\Facades\Cart;
|
|
|
|
|
-use Longyi\Gift\Models\GiftCards;
|
|
|
|
|
-
|
|
|
|
|
-class CartClearHandler
|
|
|
|
|
-{
|
|
|
|
|
- /**
|
|
|
|
|
- * Handle cart clear events.
|
|
|
|
|
- *
|
|
|
|
|
- * @param int $itemId
|
|
|
|
|
- * @return void
|
|
|
|
|
- */
|
|
|
|
|
- public function afterDeleteItem($itemId)
|
|
|
|
|
- {
|
|
|
|
|
- $cart = Cart::getCart();
|
|
|
|
|
-
|
|
|
|
|
- if (!$cart) {
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Check if cart has no more items after deletion
|
|
|
|
|
- // Refresh the cart to get latest items count
|
|
|
|
|
- $cart->refresh();
|
|
|
|
|
-
|
|
|
|
|
- // If cart is empty or has no active items, restore gift card
|
|
|
|
|
- if ($cart->items->count() === 0) {
|
|
|
|
|
- $this->restoreGiftCardBalance($cart);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Restore gift card balance when cart is cleared
|
|
|
|
|
- */
|
|
|
|
|
- protected function restoreGiftCardBalance($cart)
|
|
|
|
|
- {
|
|
|
|
|
- if (!$cart->giftcard_number) {
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- $giftCardNumber = $cart->giftcard_number;
|
|
|
|
|
- $giftCardAmount = $cart->giftcard_amount;
|
|
|
|
|
-
|
|
|
|
|
- // Clear cart gift card info
|
|
|
|
|
- $cart->giftcard_number = null;
|
|
|
|
|
- $cart->giftcard_amount = null;
|
|
|
|
|
- $cart->remaining_giftcard_amount = null;
|
|
|
|
|
- $cart->save();
|
|
|
|
|
-
|
|
|
|
|
- // Restore gift card balance
|
|
|
|
|
- if ($giftCardNumber && $giftCardAmount > 0) {
|
|
|
|
|
- $customerId = auth()->user()?->id ?? $cart->customer_id;
|
|
|
|
|
-
|
|
|
|
|
- $giftCard = GiftCards::where('giftcard_number', $giftCardNumber)
|
|
|
|
|
- ->where('customer_id', $customerId)
|
|
|
|
|
- ->first();
|
|
|
|
|
-
|
|
|
|
|
- if ($giftCard) {
|
|
|
|
|
- $giftCard->used_giftcard_amount -= $giftCardAmount;
|
|
|
|
|
- $giftCard->remaining_giftcard_amount += $giftCardAmount;
|
|
|
|
|
- $giftCard->save();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|