configurable options nonfinished. finish home

This commit is contained in:
merdan 2022-01-31 20:15:13 +05:00
parent 36ee06c789
commit 887d02d872
13 changed files with 197 additions and 71 deletions

View File

@ -2,6 +2,10 @@
namespace Sarga\API\Http\Controllers;
use Sarga\API\Http\Resources\Catalog\Attribute;
use Sarga\API\Http\Resources\Catalog\AttributeOption;
use Sarga\API\Http\Resources\Catalog\ProductVariant;
use Sarga\API\Http\Resources\Catalog\SuperAttribute;
use Sarga\API\Repositories\ProductRepository;
use Webkul\API\Http\Controllers\Shop\ProductController;
use Sarga\API\Http\Resources\Catalog\Product as ProductResource;
@ -57,35 +61,39 @@ class Products extends ProductController
->where('status',1);
}]);
}])->find($id);
// return $product;
// return $product->variants; //Attribute::make($product->super_attributes->first());
if(!empty($product) && $product->super_attributes->isNotEmpty() && $product->variants->isNotEmpty()){
$variants = $product->variants->makeHidden(['type','created_at','updated_at','parent_id','attribute_family_id',
'additional','new','featured','visible_individually','status','guest_checkout','meta_title','meta_keywords',
'product_flats','attribute_family','short_description','sku','brand']);
$data = array();
$attribute_main = $product->super_attributes->first();
$attribute = $product->super_attributes->first();
$data =[];
$distinctVariants = $variants->unique($attribute->code);//->only([$attribute_main->code]);
if($product->super_attributes->count() > 1){
$gr_data = array('attribute' => SuperAttribute::make($attribute),'options' =>[]);
$last_attribute = $product->super_attributes->last();
foreach($variants as $variant){
$option = $this->attributeOptionRepository->getOptionLabel($variant->{$attribute_main->code});
$last_option = $this->attributeOptionRepository->getOptionLabel($variant->{$last_attribute->code});
$data[$attribute_main->code][$option]['image'] = $variant->images;
$data[$attribute_main->code][$option][$last_attribute->code][$last_option] = ProductResource::make($variant);
// return $attribute->options->whereIn('id',$distinctVariants->pluck($attribute->code)->toArray());
foreach($distinctVariants as $variant){
$option = $attribute->options->where('id',$variant->{$attribute->code})->first();
$item = [
'option' => $option->admin_name,
'images' => $variant->images,
];
if($product->super_attributes->count()>1 && $option){
$last_attribute = $product->super_attributes->last();
$item['variants']['attribute'] = SuperAttribute::make($last_attribute);
$item['variants']['products'] = ProductResource::collection($variants->where($attribute->code,$variant->{$attribute->code}),$last_attribute);
}
}
else{
foreach($variants as $variant){
$option = $this->attributeOptionRepository->getOptionLabel($variant->{$attribute_main->code});
$data[$attribute_main->code][$option] = ProductResource::make($variant);
else{
$item['product'] = ProductVariant::make($variants);
}
$gr_data['options'][] = $item;
}
return response()->json($data);
return response()->json($gr_data);
}
else{
return response()->json(['message' => 'not found'],404);

View File

@ -6,6 +6,20 @@ use Illuminate\Http\Resources\Json\JsonResource;
class ProductVariant extends JsonResource
{
/**
* Create a new resource instance.
*
* @return void
*/
public function __construct($resource,$attribute)
{
// $this->productReviewHelper = app('Webkul\Product\Helpers\Review');
$this->wishlistHelper = app('Webkul\Customer\Helpers\Wishlist');
$this->attribute = $attribute;
parent::__construct($resource);
}
/**
* Transform the resource into an array.
*
@ -25,8 +39,8 @@ class ProductVariant extends JsonResource
'additional' => $this->additional,
'price' => $productTypeInstance->getMinimalPrice(),
'converted_price' => core()->currency($productTypeInstance->getMinimalPrice()),
"color" => $this->color,
"size" => $this->size,
"attribute" => $this->attribute,
// "size" => $this->size,
// "brand"=>$this->brand,
/* special price cases */

View File

@ -0,0 +1,23 @@
<?php
namespace Sarga\API\Http\Resources\Catalog;
use Illuminate\Http\Resources\Json\JsonResource;
class SuperAttribute extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request): array
{
return [
'id' => $this->id,
'code' => $this->code,
'name' => $this->name,
];
}
}

View File

@ -1,6 +1,6 @@
{
"name": "sarga/scrap",
"description": "Scrap for Sarga.",
"name": "sarga/importer",
"description": "Porduct importer for Sarga.",
"license": "MIT",
"authors": [
@ -13,14 +13,14 @@
"require": {},
"autoload": {
"psr-4": {
"Sarga\\Scrap\\": "src/"
"Sarga\\Importer\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Sarga\\Scrap\\Providers\\ScrapServiceProvider"
"ImporterServiceProvider"
],
"aliases": {}
}

View File

@ -0,0 +1,70 @@
<?php
namespace Sarga\Importer\Http\Controllers;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Sarga\API\Repositories\ProductRepository;
use Webkul\Marketplace\Repositories\SellerRepository;
class ProductController extends Controller
{
use ValidatesRequests;
protected $productRepository;
protected $sellerRepository;
public function __construct(ProductRepository $productRepository, SellerRepository $sellerRepository)
{
$this->sellerRepository = $sellerRepository;
$this->productRepository = $productRepository;
}
public function create()
{
try {
$data = json_decode(request()->getContent(),true);
}
catch (\Exception $e){
Log::error($e);
return response()->json(['errors'=>$e->getMessage()],400);
}
$validation = Validator::make($data, [
'categories' => 'required',
// 'sku' => ['required', 'unique:products,sku', new Slug],
'images' => 'required',
'name' => 'required',
'url_key'=> 'required',
'price' => 'required',
'vendor' => 'required'
]);
if ($validation->fails()) {
return response()->json(['errors'=>$validation->getMessageBag()->all()],422);
}
if($product = $this->productRepository->findOneByField('sku',$data['sku']))
{
return response()->json(['success'=>true,'product_id' => $product->id]);
}
elseif($product = $this->productRepository->create($data))
{
return response()->json([
'success'=>true,
'product_id' => $product->id
]);
}else
{
return response()->json(['success'=>false],400);
}
}
public function update(){
}
}

View File

@ -1,9 +1,9 @@
<?php
<?php namespace Sarga\Importer\Providers;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
class ScrapServiceProvider extends ServiceProvider
class ImporterServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.

View File

@ -0,0 +1,20 @@
<?php
use Illuminate\Support\Facades\Route;
use Sarga\Importer\Http\Controllers\ProductController;
use Sarga\Scrap\Http\Controllers\LCW;
use Sarga\Scrap\Http\Controllers\Trendyol;
Route::group(['prefix' => 'scrapi'], function ($router) {
Route::group(['middleware' => ['locale', 'currency']], function ($router) {
});
});
Route::group(['prefix' => 'scrap','middleware' =>['scrap']], function ($router){
//Trendyol routes
Route::get('trendyol',[Trendyol::class, 'index']);
//LCW
Route::get('lcw',[LCW::class, 'index']);
Route::put('create',[ProductController::class,'create']);
});

View File

@ -1,15 +0,0 @@
<?php
use Illuminate\Support\Facades\Route;
use Sarga\Scrap\Http\Controllers\LCW;
use Sarga\Scrap\Http\Controllers\Trendyol;
Route::group(['prefix' => 'scrapi'], function ($router) {
Route::group(['middleware' => ['locale', 'currency']], function ($router) {
//Trendyol routes
Route::get('trendyol',[Trendyol::class, 'index']);
//LCW
Route::get('lcw',[LCW::class, 'index']);
});
});

View File

@ -148,11 +148,13 @@ class AttributeGroupTableSeeder extends Seeder
'attribute_id' => '24',
'attribute_group_id' => '1',
'position' => '12',
], [
'attribute_id' => '25',
'attribute_group_id' => '1',
'position' => '13',
], [
],
// [
// 'attribute_id' => '25',
// 'attribute_group_id' => '1',
// 'position' => '13',
// ],
[
'attribute_id' => '26',
'attribute_group_id' => '1',
'position' => '9',

View File

@ -474,26 +474,28 @@ class AttributeTableSeeder extends Seeder
'created_at' => $now,
'updated_at' => $now,
'is_comparable' => '0',
], [
'id' => '25',
'code' => 'brand',
'admin_name' => 'Brand',
'type' => 'select',
'validation' => NULL,
'position' => '28',
'is_required' => '0',
'is_unique' => '0',
'value_per_locale' => '0',
'value_per_channel' => '0',
'is_filterable' => '1',
'is_configurable' => '0',
'is_user_defined' => '1',
'is_visible_on_front' => '1',
'use_in_flat' => '1',
'created_at' => $now,
'updated_at' => $now,
'is_comparable' => '0',
], [
],
// [
// 'id' => '25',
// 'code' => 'brand',
// 'admin_name' => 'Brand',
// 'type' => 'select',
// 'validation' => NULL,
// 'position' => '28',
// 'is_required' => '0',
// 'is_unique' => '0',
// 'value_per_locale' => '0',
// 'value_per_channel' => '0',
// 'is_filterable' => '1',
// 'is_configurable' => '0',
// 'is_user_defined' => '1',
// 'is_visible_on_front' => '1',
// 'use_in_flat' => '1',
// 'created_at' => $now,
// 'updated_at' => $now,
// 'is_comparable' => '0',
// ],
[
'id' => '26',
'code' => 'guest_checkout',
'admin_name' => 'Guest Checkout',
@ -655,12 +657,14 @@ class AttributeTableSeeder extends Seeder
'locale' => 'en',
'name' => 'Size',
'attribute_id' => '24',
], [
'id' => '25',
'locale' => 'en',
'name' => 'Brand',
'attribute_id' => '25',
], [
],
// [
// 'id' => '25',
// 'locale' => 'en',
// 'name' => 'Brand',
// 'attribute_id' => '25',
// ],
[
'id' => '26',
'locale' => 'en',
'name' => 'Allow Guest Checkout',