From a45341900ad453c23dac7e3f4ad8a452f4c1b82f Mon Sep 17 00:00:00 2001 From: merdan Date: Mon, 7 Feb 2022 16:42:40 +0500 Subject: [PATCH] variants --- .../Sarga/API/Http/Controllers/Products.php | 10 +++---- .../Http/Resources/Catalog/ProductVariant.php | 28 ++++++++++++++----- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/packages/Sarga/API/Http/Controllers/Products.php b/packages/Sarga/API/Http/Controllers/Products.php index 9c2046c5c..f3e44850b 100644 --- a/packages/Sarga/API/Http/Controllers/Products.php +++ b/packages/Sarga/API/Http/Controllers/Products.php @@ -90,16 +90,14 @@ class Products extends ProductController if($product->super_attributes->count()>1 && $option) { - $last_attribute = $product->super_attributes->last(); - $products = $variants->where($attribute->code,$variant->{$attribute->code}) - ->map(function ($item,$key) use ($last_attribute){ - $option = $last_attribute->options->where('id',$item->{$last_attribute->code})->first(); + ->map(function ($item,$key) use ($product){ +// $option = $last_attribute->options->where('id',$item->{$last_attribute->code})->first(); - return ProductVariant::make($item,$option); + return ProductVariant::make($item,$product->super_attributes); }); - $item['variants']['attribute'] = SuperAttribute::make($last_attribute); + $item['variants']['attribute'] = SuperAttribute::make($product->super_attributes->last()); $item['variants']['products'] = $products->values(); } else diff --git a/packages/Sarga/API/Http/Resources/Catalog/ProductVariant.php b/packages/Sarga/API/Http/Resources/Catalog/ProductVariant.php index 50dffce4e..01d068184 100644 --- a/packages/Sarga/API/Http/Resources/Catalog/ProductVariant.php +++ b/packages/Sarga/API/Http/Resources/Catalog/ProductVariant.php @@ -12,13 +12,9 @@ class ProductVariant extends JsonResource * @return void */ - public function __construct($resource, $opion) + public function __construct($resource, $attributes) { -// $this->productReviewHelper = app('Webkul\Product\Helpers\Review'); - - $this->wishlistHelper = app('Webkul\Customer\Helpers\Wishlist'); - $this->option = $opion; - $this->wishlistHelper = app('Webkul\Customer\Helpers\Wishlist'); + $this->attributes = $attributes; parent::__construct($resource); } /** @@ -42,7 +38,7 @@ class ProductVariant extends JsonResource 'formatted_price' => core()->currency($productTypeInstance->getMinimalPrice()), 'short_description' => $product->short_description, 'description' => $product->description, - "option_value" => $this->option->admin_name, + "option_value" => $this->last_attribute_value(), /* product's checks */ 'in_stock' => $product->haveSufficientQuantity(1), 'is_wishlisted' => $this->wishlistHelper->getWishlistProduct($product) ? true : false, @@ -50,8 +46,26 @@ class ProductVariant extends JsonResource /* special price cases */ $this->merge($this->specialPriceInfo()), 'images' => ProductImage::collection($product->images), + 'attributes' => $this->super_attributes()->toArray(), ]; } + + private function super_attributes(){ + return $this->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 + ]; + }); + } + + private function last_attribute_value(){ + $last_attribute = $this->attributes->last(); + $option = $last_attribute->options->where('id',$this->{$last_attribute->code})->first(); + return $option->admin_name; + } /** * Get special price information. *