ShippingInsuranceService.php 769 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace Longyi\ShippingInsurance\Services;
  3. use Webkul\Checkout\Facades\Cart;
  4. class ShippingInsuranceService
  5. {
  6. /**
  7. * 启用运费险
  8. */
  9. public function activate(): void
  10. {
  11. $cart = Cart::getCart();
  12. if (! $cart) {
  13. throw new \Exception('Cart not found.');
  14. }
  15. $cart->update([
  16. 'use_shipping_insurance' => true,
  17. ]);
  18. Cart::collectTotals();
  19. }
  20. /**
  21. * 移除运费险
  22. */
  23. public function remove(): void
  24. {
  25. $cart = Cart::getCart();
  26. if (! $cart) {
  27. throw new \Exception('Cart not found.');
  28. }
  29. $cart->update([
  30. 'use_shipping_insurance' => false,
  31. ]);
  32. Cart::collectTotals();
  33. }
  34. }