CartResource.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace Webkul\Admin\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use Webkul\Tax\Facades\Tax;
  5. class CartResource extends JsonResource
  6. {
  7. /**
  8. * Transform the resource into an array.
  9. *
  10. * @param \Illuminate\Http\Request $request
  11. * @return array
  12. */
  13. public function toArray($request)
  14. {
  15. $taxes = collect(Tax::getTaxRatesWithAmount($this, true))->map(function ($rate) {
  16. return core()->currency($rate ?? 0);
  17. });
  18. return [
  19. 'id' => $this->id,
  20. 'is_guest' => $this->is_guest,
  21. 'customer_id' => $this->customer_id,
  22. 'items_count' => $this->items_count,
  23. 'items_qty' => $this->items_qty,
  24. 'sub_total' => $this->base_sub_total,
  25. 'formatted_sub_total' => core()->formatPrice($this->base_sub_total),
  26. 'sub_total_incl_tax' => $this->base_sub_total_incl_tax,
  27. 'formatted_sub_total_incl_tax' => core()->formatPrice($this->base_sub_total_incl_tax),
  28. 'shipping_method' => $this->shipping_method,
  29. 'shipping_amount' => $this->base_shipping_amount,
  30. 'formatted_shipping_amount' => core()->formatPrice($this->base_shipping_amount),
  31. 'shipping_amount_incl_tax' => $this->base_shipping_amount_incl_tax,
  32. 'formatted_shipping_amount_incl_tax' => core()->formatPrice($this->base_shipping_amount_incl_tax),
  33. 'tax_total' => $this->base_tax_total,
  34. 'formatted_tax_total' => core()->formatPrice($this->tax_total),
  35. 'applied_taxes' => $taxes,
  36. 'coupon_code' => $this->coupon_code,
  37. 'discount_amount' => $this->base_discount_amount,
  38. 'formatted_discount_amount' => core()->formatPrice($this->base_discount_amount),
  39. 'grand_total' => $this->base_grand_total,
  40. 'formatted_grand_total' => core()->formatPrice($this->base_grand_total),
  41. 'items' => CartItemResource::collection($this->items),
  42. 'billing_address' => new AddressResource($this->billing_address),
  43. 'shipping_address' => new AddressResource($this->shipping_address),
  44. 'have_stockable_items' => $this->haveStockableItems(),
  45. 'payment_method' => $this->payment?->method,
  46. 'payment_method_title' => core()->getConfigData('sales.payment_methods.'.$this->payment?->method.'.title'),
  47. $this->mergeWhen($this->giftcard_number, [
  48. 'giftcard_number' => $this->giftcard_number,
  49. 'giftcard_amount' => $this->giftcard_amount,
  50. 'base_giftcard_amount' => $this->base_giftcard_amount,
  51. 'remaining_giftcard_amount' => $this->remaining_giftcard_amount,
  52. ]),
  53. ];
  54. }
  55. }