colorvariant size variant changes

This commit is contained in:
merdan 2022-10-11 12:26:34 +05:00
parent bc4c69727f
commit ab345dd15b
9 changed files with 132 additions and 168 deletions

View File

@ -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)

View File

@ -0,0 +1,28 @@
<?php
namespace Sarga\API\Http\Resources\Catalog;
class ColorVariant extends Product
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
$product = $this->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()),
];
}
}

View File

@ -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;

View File

@ -0,0 +1,36 @@
<?php
namespace Sarga\API\Http\Resources\Catalog;
use Illuminate\Http\Resources\Json\JsonResource;
class ProductDetail extends Product
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
$product = $this->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()),
];
}
}

View File

@ -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),

View File

@ -0,0 +1,30 @@
<?php
namespace Sarga\API\Http\Resources\Catalog;
class SizeVariant extends ProductDetail
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
$product = $this->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()),
];
}
}

View File

@ -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', [

View File

@ -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);
}

View File

@ -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');