| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <?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();
- }
- }
|