ProductDetailCollection.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Http\Resources\V2;
  3. use Illuminate\Http\Resources\Json\ResourceCollection;
  4. use App\Models\Review;
  5. use App\Models\Attribute;
  6. class ProductDetailCollection extends ResourceCollection
  7. {
  8. public function toArray($request)
  9. {
  10. return [
  11. 'data' => $this->collection->map(function ($data) {
  12. $precision = 2;
  13. $calculable_price = home_discounted_base_price($data, false);
  14. $calculable_price = number_format($calculable_price, $precision, '.', '');
  15. $calculable_price = floatval($calculable_price);
  16. // $calculable_price = round($calculable_price, 2);
  17. $photo_paths = get_images_path($data->photos);
  18. $photos = [];
  19. if (!empty($photo_paths)) {
  20. for ($i = 0; $i < count($photo_paths); $i++) {
  21. if ($photo_paths[$i] != "" ) {
  22. $item = array();
  23. $item['variant'] = "";
  24. $item['path'] = $photo_paths[$i];
  25. $photos[]= $item;
  26. }
  27. }
  28. }
  29. foreach ($data->stocks as $stockItem){
  30. if($stockItem->image != null && $stockItem->image != ""){
  31. $item = array();
  32. $item['variant'] = $stockItem->variant;
  33. $item['path'] = uploaded_asset($stockItem->image) ;
  34. $photos[]= $item;
  35. }
  36. }
  37. $brand = [
  38. 'id'=> 0,
  39. 'name'=> "",
  40. 'logo'=> "",
  41. ];
  42. if($data->brand != null) {
  43. $brand = [
  44. 'id'=> $data->brand->id,
  45. 'name'=> $data->brand->getTranslation('name'),
  46. 'logo'=> uploaded_asset($data->brand->logo),
  47. ];
  48. }
  49. return [
  50. 'id' => (integer)$data->id,
  51. 'name' => $data->getTranslation('name'),
  52. 'added_by' => $data->added_by,
  53. 'seller_id' => $data->user->id,
  54. 'shop_id' => $data->added_by == 'admin' ? 0 : $data->user->shop->id,
  55. 'shop_name' => $data->added_by == 'admin' ? translate('In House Product') : $data->user->shop->name,
  56. 'shop_logo' => $data->added_by == 'admin' ? uploaded_asset(get_setting('header_logo')) : uploaded_asset($data->user->shop->logo)??"",
  57. 'photos' => $photos,
  58. 'thumbnail_image' => uploaded_asset($data->thumbnail_img),
  59. 'tags' => explode(',', $data->tags),
  60. 'price_high_low' => (double)explode('-', home_discounted_base_price($data, false))[0] == (double)explode('-', home_discounted_price($data, false))[1] ? format_price((double)explode('-', home_discounted_price($data, false))[0]) : "From " . format_price((double)explode('-', home_discounted_price($data, false))[0]) . " to " . format_price((double)explode('-', home_discounted_price($data, false))[1]),
  61. 'choice_options' => $this->convertToChoiceOptions(json_decode($data->choice_options)),
  62. 'colors' => json_decode($data->colors),
  63. 'has_discount' => home_base_price($data, false) != home_discounted_base_price($data, false),
  64. 'stroked_price' => home_base_price($data),
  65. 'main_price' => home_discounted_base_price($data),
  66. 'calculable_price' => $calculable_price,
  67. 'currency_symbol' => currency_symbol(),
  68. 'current_stock' => (integer)$data->stocks->first()->qty,
  69. 'unit' => $data->unit,
  70. 'rating' => (double)$data->rating,
  71. 'rating_count' => (integer)Review::where(['product_id' => $data->id])->count(),
  72. 'earn_point' => (double)$data->earn_point,
  73. 'description' => $data->getTranslation('description'),
  74. 'video_link' => $data->video_link != null ? $data->video_link : "",
  75. 'brand' => $brand,
  76. 'link' => route('product', $data->slug)
  77. ];
  78. })
  79. ];
  80. }
  81. public function with($request)
  82. {
  83. return [
  84. 'success' => true,
  85. 'status' => 200
  86. ];
  87. }
  88. protected function convertToChoiceOptions($data)
  89. {
  90. $result = array();
  91. // if($data) {
  92. foreach ($data as $key => $choice) {
  93. $item['name'] = $choice->attribute_id;
  94. $item['title'] = Attribute::find($choice->attribute_id)->getTranslation('name');
  95. $item['options'] = $choice->values;
  96. array_push($result, $item);
  97. }
  98. // }
  99. return $result;
  100. }
  101. protected function convertPhotos($data)
  102. {
  103. $result = array();
  104. foreach ($data as $key => $item) {
  105. array_push($result, uploaded_asset($item));
  106. }
  107. return $result;
  108. }
  109. }