From ab345dd15b9e0f3e5bc1c923dda62c202c10ba15 Mon Sep 17 00:00:00 2001 From: merdan Date: Tue, 11 Oct 2022 12:26:34 +0500 Subject: [PATCH] colorvariant size variant changes --- .../Sarga/API/Http/Controllers/Products.php | 24 +++ .../Http/Resources/Catalog/ColorVariant.php | 28 +++ .../API/Http/Resources/Catalog/Product.php | 168 +----------------- .../Http/Resources/Catalog/ProductDetail.php | 36 ++++ .../Http/Resources/Catalog/ProductVariant.php | 8 +- .../Http/Resources/Catalog/SizeVariant.php | 30 ++++ packages/Sarga/API/Http/routes.php | 1 + .../src/Providers/BrandServiceProvider.php | 1 + .../src/Repositories/ProductRepository.php | 4 +- 9 files changed, 132 insertions(+), 168 deletions(-) create mode 100644 packages/Sarga/API/Http/Resources/Catalog/ColorVariant.php create mode 100644 packages/Sarga/API/Http/Resources/Catalog/ProductDetail.php create mode 100644 packages/Sarga/API/Http/Resources/Catalog/SizeVariant.php diff --git a/packages/Sarga/API/Http/Controllers/Products.php b/packages/Sarga/API/Http/Controllers/Products.php index 171118c21..e6f9b937a 100644 --- a/packages/Sarga/API/Http/Controllers/Products.php +++ b/packages/Sarga/API/Http/Controllers/Products.php @@ -3,6 +3,7 @@ namespace Sarga\API\Http\Controllers; use Illuminate\Support\Facades\Log; +use Sarga\API\Http\Resources\Catalog\ProductDetail; use Sarga\API\Http\Resources\Catalog\ProductVariant; use Sarga\API\Http\Resources\Catalog\SuperAttribute; use Sarga\Shop\Repositories\ProductRepository; @@ -57,6 +58,29 @@ class Products extends ProductController response()->json(['error' => 'not found'],404); } + public function product($id){ + $product = $this->productRepository->select('id','attribute_family_id','type','brand_id') + ->with(['brand','related_products:id,type,attribute_family_id','variants'=>function($query) + { + $query->with(['product_flats' => function($qf) + { + $channel = core()->getRequestedChannelCode(); + + $locale = 'tm';//core()->getRequestedLocaleCode(); + + $qf->where('product_flat.channel', $channel) + ->where('product_flat.locale', $locale) +// ->whereNotNull('product_flat.url_key') + ->where('product_flat.status',1); + }]); + }])->find($id); + +// return $product; + + return ProductDetail::make($product); + + } + public function variants($id) { $product = $this->productRepository->with(['super_attributes:id,code','variants'=>function($query) diff --git a/packages/Sarga/API/Http/Resources/Catalog/ColorVariant.php b/packages/Sarga/API/Http/Resources/Catalog/ColorVariant.php new file mode 100644 index 000000000..056b1e340 --- /dev/null +++ b/packages/Sarga/API/Http/Resources/Catalog/ColorVariant.php @@ -0,0 +1,28 @@ +product ? $this->product : $this; + + return [ + 'id' => $product->id, + 'type' => $product->type, + 'name' => $product->name, + 'description' => $product->description, + 'images' => ProductImage::collection($product->images), + 'size_variants' => SizeVariant::collection($product->variants), + $this->merge($this->specialPriceInfo()), + + ]; + } +} \ No newline at end of file diff --git a/packages/Sarga/API/Http/Resources/Catalog/Product.php b/packages/Sarga/API/Http/Resources/Catalog/Product.php index b48bdfca6..5ac024546 100644 --- a/packages/Sarga/API/Http/Resources/Catalog/Product.php +++ b/packages/Sarga/API/Http/Resources/Catalog/Product.php @@ -7,19 +7,6 @@ use Illuminate\Support\Facades\Log; class Product extends JsonResource { - /** - * Create a new resource instance. - * - * @return void - */ - public function __construct($resource) - { -// $this->productReviewHelper = app('Webkul\Product\Helpers\Review'); - -// $this->wishlistHelper = app('Webkul\Customer\Helpers\Wishlist'); - - parent::__construct($resource); - } /** * Transform the resource into an array. @@ -31,57 +18,31 @@ class Product extends JsonResource { $product = $this->product ? $this->product : $this; - $productTypeInstance = $product->getTypeInstance(); - return [ /* product's information */ 'id' => $product->id, 'type' => $product->type, 'name' => $product->name, - 'description' => $product->description, - 'is_wishlisted' => $this->isWishlisted($product) , - 'is_item_in_cart' => \Cart::hasProduct($product), - 'shop_title' => $this->shop_title, +// 'description' => $product->description, +// 'is_wishlisted' => $this->isWishlisted($product), //todo transfer to mobile +// 'is_item_in_cart' => \Cart::hasProduct($product),//todo transfer to mobile +// 'shop_title' => $this->shop_title, 'brand' => $product->brand->name ?? '', - /* product's extra information */ - $this->merge($this->allProductExtraInfo()), + 'images' => ProductImage::collection($product->images), + 'color_count' => $this->related_products->count(), /* special price cases */ $this->merge($this->specialPriceInfo()), - /* super attributes */ - $this->mergeWhen($this->super_attributes, [ - 'super_attributes' => $this->super_attributes, - ]), ]; } - private function super_attributes(){ - if(is_countable($this->super_attributes)){ - return $this->super_attributes->map(function($item, $key){ - return [ - 'code' => $item->code, - 'value' => $this->{$item->code}, - 'name' => $item->name??$item->admin_name, - 'label' => $item->options->where('id',$this->{$item->code})->first()->admin_name - ]; - })->toArray(); - }else{ - $item = $this->super_attributes; - return [ - 'code' => $item->code, - 'value' => $this->{$item->code}, - 'name' => $item->name??$item->admin_name, - 'label' => $item->options->where('id',$this->{$item->code})->first()->admin_name - ]; - } - } /** * Get special price information. * * @return array */ - private function specialPriceInfo() + protected function specialPriceInfo() { if($this->type == 'configurable' && $variant = $this->product->getTypeInstance()->getMinPriceVariant()) { @@ -94,19 +55,11 @@ class Product extends JsonResource $typeInstance = $product->getTypeInstance(); return [ - 'images' => ProductImage::collection($product->images), + 'price' => (double) core()->convertPrice($typeInstance->getMinimalPrice()), 'formatted_price' => core()->currency($typeInstance->getMinimalPrice()), -//todo remove special price - 'special_price' => $this->when( - $typeInstance->haveSpecialPrice(), - (double) core()->convertPrice($typeInstance->getSpecialPrice()) - ), - 'formatted_special_price' => $this->when( - $typeInstance->haveSpecialPrice(), - core()->currency($typeInstance->getSpecialPrice()) - ), + 'regular_price' => $this->when( $typeInstance->haveSpecialPrice(), (double) core()->convertPrice($typeInstance->getMaximumPrice()) @@ -118,109 +71,6 @@ class Product extends JsonResource ]; } - /** - * Get all product's extra information. - * - * @return array - */ - private function allProductExtraInfo() - { - $product = $this->product ? $this->product : $this; - - $productTypeInstance = $product->getTypeInstance(); - - return [ - /* grouped product */ -// $this->mergeWhen( -// $productTypeInstance instanceof \Webkul\Product\Type\Grouped, -// $product->type == 'grouped' -// ? $this->getGroupedProductInfo($product) -// : null -// ), -// -// /* bundle product */ -// $this->mergeWhen( -// $productTypeInstance instanceof \Webkul\Product\Type\Bundle, -// $product->type == 'bundle' -// ? $this->getBundleProductInfo($product) -// : null -// ), - - /* configurable product */ - $this->mergeWhen( - $productTypeInstance instanceof \Webkul\Product\Type\Configurable, - $product->type == 'configurable' - ? $this->getConfigurableProductInfo($product) - : null - ), - - ]; - } - - /** - * Get grouped product's extra information. - * - * @param \Webkul\Product\Models\Product - * @return array - */ - private function getGroupedProductInfo($product) - { - return [ - 'grouped_products' => $product->grouped_products->map(function($groupedProduct) { - $associatedProduct = $groupedProduct->associated_product; - - $data = $associatedProduct->toArray(); - - return array_merge($data, [ - 'qty' => $groupedProduct->qty, - 'isSaleable' => $associatedProduct->getTypeInstance()->isSaleable(), - 'formated_price' => $associatedProduct->getTypeInstance()->getPriceHtml(), - 'show_quantity_changer' => $associatedProduct->getTypeInstance()->showQuantityBox(), - ]); - }) - ]; - } - - /** - * Get bundle product's extra information. - * - * @param \Webkul\Product\Models\Product - * @return array - */ - private function getBundleProductInfo($product) - { - return [ - 'currency_options' => core()->getAccountJsSymbols(), - 'bundle_options' => app('Webkul\Product\Helpers\BundleOption')->getBundleConfig($product) - ]; - } - - /** - * Get configurable product's extra information. - * - * @param \Webkul\Product\Models\Product - * @return array - */ - private function getConfigurableProductInfo($product) - { - $data = [ - 'variants_count' => $this->variants->count(), - 'color_count' => $this->variants->groupBy('color')->count(), - ]; - - $special_variant = $this->variants->sortBy('min_price')->first(); - - if($special_variant && $special_variant->min_price < $special_variant->max_price){ - $data = array_merge($data, [ - 'special_price' => core()->convertPrice($special_variant->min_price), - 'formatted_special_price' => core()->currency($special_variant->min_price), - 'regular_price' => core()->convertPrice($special_variant->price), - 'formatted_regular_price' => core()->currency($special_variant->price), - ]); - } - return $data; - } - private function isWishlisted($product):bool { $wishlist = false; diff --git a/packages/Sarga/API/Http/Resources/Catalog/ProductDetail.php b/packages/Sarga/API/Http/Resources/Catalog/ProductDetail.php new file mode 100644 index 000000000..357a5b90e --- /dev/null +++ b/packages/Sarga/API/Http/Resources/Catalog/ProductDetail.php @@ -0,0 +1,36 @@ +product ? $this->product : $this; + + return [ + /* product's information */ + 'id' => $product->id, + 'type' => $product->type, + 'name' => $product->name, + 'description' => $product->description, + 'brand' => $product->brand->name ?? '', + 'images' => ProductImage::collection($product->images), + 'color_variants' => ColorVariant::collection($product->related_products->where('status',1)), + 'size_variants' => SizeVariant::collection($product->variants->where('status',1)), + /* special price cases */ + $this->merge($this->specialPriceInfo()), + + ]; + } + + +} \ No newline at end of file diff --git a/packages/Sarga/API/Http/Resources/Catalog/ProductVariant.php b/packages/Sarga/API/Http/Resources/Catalog/ProductVariant.php index e946d446e..ef747c2fa 100644 --- a/packages/Sarga/API/Http/Resources/Catalog/ProductVariant.php +++ b/packages/Sarga/API/Http/Resources/Catalog/ProductVariant.php @@ -34,16 +34,10 @@ class ProductVariant extends JsonResource return [ 'id' => $this->id, 'name' => $product->name, -// 'url_key' => $product->url_key, 'price' => core()->convertPrice($productTypeInstance->getMinimalPrice()), 'formatted_price' => core()->currency($productTypeInstance->getMinimalPrice()), -// 'short_description' => $product->short_description, - 'description' => $product->description, "option_value" => $this->last_attribute_value(), - /* product's checks */ -// 'in_stock' => $product->haveSufficientQuantity(1), - 'is_item_in_cart' => \Cart::hasProduct($product), - 'brand' => $product->brand->name ?? '', + 'brand' => $product->brand->name ?? '', /* special price cases */ $this->merge($this->specialPriceInfo()), 'images' => ProductImage::collection($product->images), diff --git a/packages/Sarga/API/Http/Resources/Catalog/SizeVariant.php b/packages/Sarga/API/Http/Resources/Catalog/SizeVariant.php new file mode 100644 index 000000000..2e00e1706 --- /dev/null +++ b/packages/Sarga/API/Http/Resources/Catalog/SizeVariant.php @@ -0,0 +1,30 @@ +product ? $this->product : $this; + $flat = $this->product_flats->first(); + return [ + /* product's information */ + 'id' => $product->id, + 'type' => $product->type, + 'name' => $product->name, + 'size' => $flat->size ?? 0, + 'size_label' => $flat->size_label ?? null, + + /* special price cases */ + $this->merge($this->specialPriceInfo()), + + ]; + } +} \ No newline at end of file diff --git a/packages/Sarga/API/Http/routes.php b/packages/Sarga/API/Http/routes.php index d5260b003..a928a0606 100644 --- a/packages/Sarga/API/Http/routes.php +++ b/packages/Sarga/API/Http/routes.php @@ -60,6 +60,7 @@ Route::group(['prefix' => 'api'], function () { Route::get('products-search', [Products::class, 'searchProducts']); Route::get('suggestions', [\Sarga\API\Http\Controllers\SearchController::class, 'index']); Route::get('products/{id}', [Products::class, 'get']); + Route::get('product/{id}', [Products::class, 'product']); Route::get('products/{id}/variants', [Products::class, 'variants']); Route::get('states', [ResourceController::class, 'index'])->defaults('_config', [ diff --git a/packages/Sarga/Brand/src/Providers/BrandServiceProvider.php b/packages/Sarga/Brand/src/Providers/BrandServiceProvider.php index 2c014b901..a273f2dde 100644 --- a/packages/Sarga/Brand/src/Providers/BrandServiceProvider.php +++ b/packages/Sarga/Brand/src/Providers/BrandServiceProvider.php @@ -18,6 +18,7 @@ class BrandServiceProvider extends ServiceProvider $this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'brand'); // Log::info('brandd service provider'); $this->app->register(EventServiceProvider::class); + $this->app->register(ModuleServiceProvider::class); // CategoryProxy::observe(CategoryObserver::class); } diff --git a/packages/Sarga/Shop/src/Repositories/ProductRepository.php b/packages/Sarga/Shop/src/Repositories/ProductRepository.php index 235d5341c..7ed4c3ef1 100644 --- a/packages/Sarga/Shop/src/Repositories/ProductRepository.php +++ b/packages/Sarga/Shop/src/Repositories/ProductRepository.php @@ -83,13 +83,13 @@ class ProductRepository extends WProductRepository $page = Paginator::resolveCurrentPage('page'); $repository = app(ProductFlatRepository::class)->scopeQuery(function ($query) use ($params, $categoryId) { - $channel = core()->getRequestedChannelCode(); +// $channel = core()->getRequestedChannelCode(); $locale = 'tm';//core()->getRequestedLocaleCode(); $qb = $query->distinct() ->select('product_flat.*') - ->where('product_flat.channel', $channel) +// ->where('product_flat.channel', $channel) ->where('product_flat.locale', $locale); // ->whereNotNull('product_flat.url_key');