浏览代码

添加会员plus

llp 5 天之前
父节点
当前提交
e15d7cc439

+ 53 - 0
packages/Longyi/Member/src/Services/MemberDiscountService.php

@@ -0,0 +1,53 @@
+<?php
+
+namespace Longyi\Member\Services;
+
+use Webkul\Checkout\Facades\Cart;
+
+class MemberDiscountService
+{
+    /**
+     * Member discount amount.
+     */
+    protected const MEMBER_DISCOUNT_AMOUNT = 19.9;
+
+    /**
+     * Apply member discount to cart.
+     */
+    public function apply(): void
+    {
+        $cart = Cart::getCart();
+
+        if (! $cart) {
+            throw new \Exception('Shopping cart not found.');
+        }
+
+        if ($cart->vip_plus_amount && $cart->vip_plus_amount > 0) {
+            throw new \Exception('Member discount has already been applied.');
+        }
+
+        $discountAmount = core()->convertPrice(self::MEMBER_DISCOUNT_AMOUNT);
+
+        $cart->vip_plus_amount = $discountAmount;
+        $cart->base_vip_plus_amount = core()->convertToBasePrice($discountAmount);
+        $cart->save();
+        Cart::collectTotals();
+    }
+
+    /**
+     * Remove member discount from cart.
+     */
+    public function remove(): void
+    {
+        $cart = Cart::getCart();
+
+        if (! $cart || ! $cart->vip_plus_amount || $cart->vip_plus_amount <= 0) {
+            return;
+        }
+
+        $cart->vip_plus_amount = null;
+        $cart->base_vip_plus_amount = null;
+        $cart->save();
+        Cart::collectTotals();
+    }
+}

+ 5 - 0
packages/Webkul/BagistoApi/src/Dto/SaveCheckoutCartInput.php

@@ -35,4 +35,9 @@ class SaveCheckoutCartInput
     #[ApiProperty(description: 'Gift Card Number. Pass "-1" to leave unchanged.')]
     #[SerializedName('giftCardNumber')]
     public ?string $giftCardNumber = null;
+
+    #[Groups(['mutation'])]
+    #[ApiProperty(description: 'Member discount toggle. Pass "-1" to leave unchanged, "1" to apply, empty string to remove.')]
+    #[SerializedName('memberDiscount')]
+    public ?string $memberDiscount = null;
 }

+ 3 - 1
packages/Webkul/BagistoApi/src/Providers/BagistoApiServiceProvider.php

@@ -40,6 +40,7 @@ use Webkul\BagistoApi\State\PaymentCallbackProcessor;
 use Webkul\BagistoApi\State\PaymentInitiateProcessor;
 use Webkul\BagistoApi\State\PaymentReplayProcessor;
 use Webkul\BagistoApi\State\SaveCheckoutCartProcessor;
+use Longyi\Member\Services\MemberDiscountService;
 use Webkul\BagistoApi\State\BookingSlotProvider;
 use Webkul\BagistoApi\State\PageProvider;
 use Webkul\BagistoApi\State\AttributeCollectionProvider;
@@ -254,7 +255,8 @@ class BagistoApiServiceProvider extends ServiceProvider
         $this->app->singleton(SaveCheckoutCartProcessor::class, function ($app) {
             return new SaveCheckoutCartProcessor(
                 $app->make('Webkul\CartRule\Repositories\CartRuleCouponRepository'),
-                $app->make(\Longyi\Gift\Services\GiftCardService::class)
+                $app->make(\Longyi\Gift\Services\GiftCardService::class),
+                $app->make(MemberDiscountService::class)
             );
         });
 

+ 37 - 3
packages/Webkul/BagistoApi/src/State/SaveCheckoutCartProcessor.php

@@ -5,6 +5,7 @@ namespace Webkul\BagistoApi\State;
 use ApiPlatform\Metadata\Operation;
 use ApiPlatform\State\ProcessorInterface;
 use Illuminate\Support\Facades\Request;
+use Longyi\Gift\Models\GiftCards;
 use Webkul\BagistoApi\Dto\CartData;
 use Webkul\BagistoApi\Dto\SaveCheckoutCartInput;
 use Webkul\BagistoApi\Exception\AuthenticationException;
@@ -16,6 +17,7 @@ use Webkul\CartRule\Repositories\CartRuleCouponRepository;
 use Webkul\Checkout\Facades\Cart;
 use Webkul\Shipping\Facades\Shipping;
 use Longyi\Gift\Services\GiftCardService;
+use Longyi\Member\Services\MemberDiscountService;
 
 /**
  * GraphQL processor for the SaveCheckoutCart mutation.
@@ -34,6 +36,7 @@ class SaveCheckoutCartProcessor implements ProcessorInterface
     public function __construct(
         protected CartRuleCouponRepository $cartRuleCouponRepository,
         protected GiftCardService $giftCardService,
+        protected MemberDiscountService $memberDiscountService,
     ) {}
 
     /**
@@ -77,6 +80,10 @@ class SaveCheckoutCartProcessor implements ProcessorInterface
                 $this->applyGiftCard((string) $data->giftCardNumber);
             }
 
+            if ($this->shouldUpdate($data->memberDiscount)) {
+                $this->applyMemberDiscount((string) $data->memberDiscount);
+            }
+
             Cart::collectTotals();
         } catch (OperationFailedException $e) {
             throw $e;
@@ -173,8 +180,6 @@ class SaveCheckoutCartProcessor implements ProcessorInterface
      */
     private function applyGiftCard(string $code): void
     {
-        // GiftController::activateGiftCard(); // destroyGiftCard
-
         $code = trim($code);
 
         if ($code === '') {
@@ -186,6 +191,35 @@ class SaveCheckoutCartProcessor implements ProcessorInterface
         if (Cart::getCart()?->giftcard_number === $code) {
             return;
         }
-        $this->giftCardService->activate($code);
+
+        $customerId = Cart::getCart()?->customer_id;
+
+        if (! $customerId) {
+            throw new OperationFailedException(__('bagistoapi::app.graphql.checkout.customer-not-found'));
+        }
+
+        $this->giftCardService->activate($code, $customerId);
+    }
+
+    /**
+     * Apply or remove member discount. "1" applies, empty string removes.
+     */
+    private function applyMemberDiscount(string $value): void
+    {
+        $value = trim($value);
+
+        if ($value === '') {
+            $this->memberDiscountService->remove();
+
+            return;
+        }
+
+        $customerId = Cart::getCart()?->customer_id;
+
+        if (! $customerId) {
+            throw new OperationFailedException(__('bagistoapi::app.graphql.checkout.customer-not-found'));
+        }
+
+        $this->memberDiscountService->apply();
     }
 }