cart product adn super attributes

This commit is contained in:
merdan 2022-02-28 17:06:42 +05:00
parent 8e4ff32772
commit 2822c21060
2 changed files with 53 additions and 1 deletions

View File

@ -0,0 +1,52 @@
<?php
namespace Sarga\API\Http\Resources\Checkout;
use Illuminate\Http\Resources\Json\JsonResource;
use Sarga\API\Http\Resources\Catalog\ProductImage;
class CartItemProduct extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
/* assign product */
$product = $this->product ? $this->product : $this;
return [
'id' => $this->id,
'name' => $product->name,
'images' => ProductImage::collection($product->images),
/* super attributes */
$this->mergeWhen($product->parent, [
'super_attributes' => $this->super_attributes($product->parent->super_attributes),
]),
];
}
private function super_attributes($attributes){
if(is_countable($attributes)){
return $attributes->map(function($item, $key){
return [
'code' => $item->code,
'value' => $this->{$item->code},
'name' => $item->name,
'label' => $item->options->where('id',$this->{$item->code})->first()->admin_name
];
})->toArray();
}else{
$item = $attributes;
return [
'code' => $item->code,
'value' => $this->{$item->code},
'name' => $item->name,
'label' => $item->options->where('id',$this->{$item->code})->first()->admin_name
];
}
}
}

View File

@ -45,7 +45,7 @@ class CartItemResource extends JsonResource
? $this->resource->additional
: json_decode($this->resource->additional, true),
'child' => new self($this->child),
'product' => $this->when($this->product_id, new ProductResource($this->product)),
'product' => $this->when($this->product_id, new CartItemProduct($this->product)),
];
}
}