From 8e19a8716580c6fb15f7df5a5d43927f1ef1da22 Mon Sep 17 00:00:00 2001 From: merdan Date: Fri, 11 Feb 2022 20:13:21 +0500 Subject: [PATCH] variant fix --- packages/Sarga/API/Http/Controllers/Products.php | 3 ++- .../API/Http/Resources/Catalog/ProductVariant.php | 14 +++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/Sarga/API/Http/Controllers/Products.php b/packages/Sarga/API/Http/Controllers/Products.php index f3e44850b..6b8ede2dd 100644 --- a/packages/Sarga/API/Http/Controllers/Products.php +++ b/packages/Sarga/API/Http/Controllers/Products.php @@ -2,6 +2,7 @@ namespace Sarga\API\Http\Controllers; +use Illuminate\Support\Facades\Log; use Sarga\API\Http\Resources\Catalog\ProductVariant; use Sarga\API\Http\Resources\Catalog\SuperAttribute; use Sarga\Shop\Repositories\ProductRepository; @@ -93,7 +94,7 @@ class Products extends ProductController $products = $variants->where($attribute->code,$variant->{$attribute->code}) ->map(function ($item,$key) use ($product){ // $option = $last_attribute->options->where('id',$item->{$last_attribute->code})->first(); - + Log::info($product->super_attributes); return ProductVariant::make($item,$product->super_attributes); }); diff --git a/packages/Sarga/API/Http/Resources/Catalog/ProductVariant.php b/packages/Sarga/API/Http/Resources/Catalog/ProductVariant.php index 714d12ade..ac51ed143 100644 --- a/packages/Sarga/API/Http/Resources/Catalog/ProductVariant.php +++ b/packages/Sarga/API/Http/Resources/Catalog/ProductVariant.php @@ -3,6 +3,7 @@ namespace Sarga\API\Http\Resources\Catalog; use Illuminate\Http\Resources\Json\JsonResource; +use Illuminate\Support\Facades\Log; class ProductVariant extends JsonResource { @@ -61,9 +62,16 @@ class ProductVariant extends JsonResource } private function last_attribute_value(){ - $last_attribute = is_countable($this->attributes)?$this->attributes->last():$this->attributes; - $option = $last_attribute->options->where('id',$this->{$last_attribute->code})->first(); - return $option->admin_name; + + $last_attribute = is_countable($this->attributes) ? $this->attributes->last() : $this->attributes; + + if(!empty($last_attribute->options)) + { + $option = $last_attribute->options->where('id',$this->{$last_attribute->code})->first(); + return $option->admin_name; + } + + return null; } /** * Get special price information.