CartResource.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Webkul\Shop\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use Webkul\Tax\Facades\Tax;
  5. use Carbon\Carbon;
  6. class CartResource extends JsonResource
  7. {
  8. /**
  9. * Transform the resource into an array.
  10. *
  11. * @param \Illuminate\Http\Request $request
  12. * @return array
  13. */
  14. public function toArray($request)
  15. {
  16. $taxes = collect(Tax::getTaxRatesWithAmount($this, true))->map(function ($rate) {
  17. return core()->currency($rate ?? 0);
  18. });
  19. return [
  20. 'id' => $this->id,
  21. 'is_guest' => $this->is_guest,
  22. 'customer_id' => $this->customer_id,
  23. 'items_count' => $this->items_count,
  24. 'items_qty' => $this->items_qty,
  25. 'applied_taxes' => $taxes,
  26. 'tax_total' => $this->tax_total,
  27. 'formatted_tax_total' => core()->formatPrice($this->tax_total),
  28. 'sub_total_incl_tax' => $this->sub_total_incl_tax,
  29. 'sub_total' => $this->sub_total,
  30. 'formatted_sub_total_incl_tax' => core()->formatPrice($this->sub_total_incl_tax),
  31. 'formatted_sub_total' => core()->formatPrice($this->sub_total),
  32. 'coupon_code' => $this->coupon_code,
  33. 'discount_amount' => $this->discount_amount,
  34. 'formatted_discount_amount' => core()->formatPrice($this->discount_amount),
  35. 'shipping_method' => $this->shipping_method,
  36. 'shipping_amount' => $this->shipping_amount,
  37. 'formatted_shipping_amount' => core()->formatPrice($this->shipping_amount),
  38. 'shipping_amount_incl_tax' => $this->shipping_amount_incl_tax,
  39. 'formatted_shipping_amount_incl_tax' => core()->formatPrice($this->shipping_amount_incl_tax),
  40. 'grand_total' => $this->grand_total,
  41. 'formatted_grand_total' => core()->formatPrice($this->grand_total),
  42. 'items' => CartItemResource::collection($this->items),
  43. 'billing_address' => new AddressResource($this->billing_address),
  44. 'shipping_address' => new AddressResource($this->shipping_address),
  45. 'have_stockable_items' => $this->haveStockableItems(),
  46. 'payment_method' => $this->payment?->method,
  47. 'payment_method_title' => core()->getConfigData('sales.payment_methods.'.$this->payment?->method.'.title'),
  48. 'vip_status' => $this->getVipStatus(),
  49. $this->mergeWhen($this->giftcard_number, [
  50. 'giftcard_number' => $this->giftcard_number,
  51. 'giftcard_amount' => $this->giftcard_amount,
  52. 'base_giftcard_amount' => $this->base_giftcard_amount,
  53. 'formatted_giftcard_amount' => core()->formatPrice($this->giftcard_amount),
  54. 'remaining_giftcard_amount' => $this->remaining_giftcard_amount,
  55. 'formatted_remaining_giftcard_amount' => core()->formatPrice($this->remaining_giftcard_amount),
  56. ]),
  57. $this->mergeWhen($this->vip_plus_amount && $this->vip_plus_amount > 0, [
  58. 'vip_plus_amount' => $this->vip_plus_amount,
  59. 'base_vip_plus_amount' => $this->base_vip_plus_amount,
  60. 'formatted_vip_plus_amount' => core()->formatPrice($this->vip_plus_amount),
  61. ]),
  62. $this->mergeWhen($this->vip_discount_amount && $this->vip_discount_amount > 0, [
  63. 'vip_discount_amount' => $this->vip_discount_amount,
  64. 'base_vip_discount_amount' => $this->base_vip_discount_amount,
  65. 'formatted_vip_discount_amount' => core()->formatPrice($this->vip_discount_amount),
  66. ]),
  67. ];
  68. }
  69. /**
  70. * Get VIP status for current customer.
  71. *
  72. * @return array
  73. */
  74. protected function getVipStatus(): array
  75. {
  76. if (!auth()->guard('customer')->check()) {
  77. return [
  78. 'is_vip' => false,
  79. 'vip_expire_date' => null,
  80. 'days_remaining' => 0,
  81. 'can_show_discount' => true,
  82. ];
  83. }
  84. $customer = auth()->guard('customer')->user();
  85. if (!$customer->vip_expire_date) {
  86. return [
  87. 'is_vip' => false,
  88. 'vip_expire_date' => null,
  89. 'days_remaining' => 0,
  90. 'can_show_discount' => true,
  91. ];
  92. }
  93. $expireDate = Carbon::parse($customer->vip_expire_date);
  94. $isExpired = $expireDate->isPast();
  95. $daysRemaining = $isExpired ? 0 : now()->diffInDays($expireDate, false);
  96. return [
  97. 'is_vip' => !$isExpired,
  98. 'vip_expire_date' => $customer->vip_expire_date,
  99. 'days_remaining' => $daysRemaining,
  100. 'can_show_discount' => $isExpired,
  101. ];
  102. }
  103. }