| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace Webkul\Shop\Http\Resources;
- use Illuminate\Http\Resources\Json\JsonResource;
- use Webkul\Tax\Facades\Tax;
- use Carbon\Carbon;
- class CartResource extends JsonResource
- {
- /**
- * Transform the resource into an array.
- *
- * @param \Illuminate\Http\Request $request
- * @return array
- */
- public function toArray($request)
- {
- $taxes = collect(Tax::getTaxRatesWithAmount($this, true))->map(function ($rate) {
- return core()->currency($rate ?? 0);
- });
- return [
- 'id' => $this->id,
- 'is_guest' => $this->is_guest,
- 'customer_id' => $this->customer_id,
- 'items_count' => $this->items_count,
- 'items_qty' => $this->items_qty,
- 'applied_taxes' => $taxes,
- 'tax_total' => $this->tax_total,
- 'formatted_tax_total' => core()->formatPrice($this->tax_total),
- 'sub_total_incl_tax' => $this->sub_total_incl_tax,
- 'sub_total' => $this->sub_total,
- 'formatted_sub_total_incl_tax' => core()->formatPrice($this->sub_total_incl_tax),
- 'formatted_sub_total' => core()->formatPrice($this->sub_total),
- 'coupon_code' => $this->coupon_code,
- 'discount_amount' => $this->discount_amount,
- 'formatted_discount_amount' => core()->formatPrice($this->discount_amount),
- 'shipping_method' => $this->shipping_method,
- 'shipping_amount' => $this->shipping_amount,
- 'formatted_shipping_amount' => core()->formatPrice($this->shipping_amount),
- 'shipping_amount_incl_tax' => $this->shipping_amount_incl_tax,
- 'formatted_shipping_amount_incl_tax' => core()->formatPrice($this->shipping_amount_incl_tax),
- 'grand_total' => $this->grand_total,
- 'formatted_grand_total' => core()->formatPrice($this->grand_total),
- 'items' => CartItemResource::collection($this->items),
- 'billing_address' => new AddressResource($this->billing_address),
- 'shipping_address' => new AddressResource($this->shipping_address),
- 'have_stockable_items' => $this->haveStockableItems(),
- 'payment_method' => $this->payment?->method,
- 'payment_method_title' => core()->getConfigData('sales.payment_methods.'.$this->payment?->method.'.title'),
- 'vip_status' => $this->getVipStatus(),
- $this->mergeWhen($this->giftcard_number, [
- 'giftcard_number' => $this->giftcard_number,
- 'giftcard_amount' => $this->giftcard_amount,
- 'base_giftcard_amount' => $this->base_giftcard_amount,
- 'formatted_giftcard_amount' => core()->formatPrice($this->giftcard_amount),
- 'remaining_giftcard_amount' => $this->remaining_giftcard_amount,
- 'formatted_remaining_giftcard_amount' => core()->formatPrice($this->remaining_giftcard_amount),
- ]),
- $this->mergeWhen($this->vip_plus_amount && $this->vip_plus_amount > 0, [
- 'vip_plus_amount' => $this->vip_plus_amount,
- 'base_vip_plus_amount' => $this->base_vip_plus_amount,
- 'formatted_vip_plus_amount' => core()->formatPrice($this->vip_plus_amount),
- ]),
- $this->mergeWhen($this->vip_discount_amount && $this->vip_discount_amount > 0, [
- 'vip_discount_amount' => $this->vip_discount_amount,
- 'base_vip_discount_amount' => $this->base_vip_discount_amount,
- 'formatted_vip_discount_amount' => core()->formatPrice($this->vip_discount_amount),
- ]),
- ];
- }
- /**
- * Get VIP status for current customer.
- *
- * @return array
- */
- protected function getVipStatus(): array
- {
- if (!auth()->guard('customer')->check()) {
- return [
- 'is_vip' => false,
- 'vip_expire_date' => null,
- 'days_remaining' => 0,
- 'can_show_discount' => true,
- ];
- }
- $customer = auth()->guard('customer')->user();
- if (!$customer->vip_expire_date) {
- return [
- 'is_vip' => false,
- 'vip_expire_date' => null,
- 'days_remaining' => 0,
- 'can_show_discount' => true,
- ];
- }
- $expireDate = Carbon::parse($customer->vip_expire_date);
- $isExpired = $expireDate->isPast();
- $daysRemaining = $isExpired ? 0 : now()->diffInDays($expireDate, false);
- return [
- 'is_vip' => !$isExpired,
- 'vip_expire_date' => $customer->vip_expire_date,
- 'days_remaining' => $daysRemaining,
- 'can_show_discount' => $isExpired,
- ];
- }
- }
|