product resource
This commit is contained in:
parent
607aed02a5
commit
4eb0456e7c
|
|
@ -1,22 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace Sarga\API\Http\Controllers;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Webkul\API\Http\Controllers\Shop\Controller;
|
||||
use Webkul\Attribute\Repositories\AttributeFamilyRepository;
|
||||
use Webkul\Core\Contracts\Validations\Slug;
|
||||
use Webkul\Product\Repositories\ProductRepository;
|
||||
use Sarga\API\Repositories\ProductRepository;
|
||||
|
||||
class IntegrationController extends Controller
|
||||
{
|
||||
protected $productRepository;
|
||||
protected $attributeFamilyRepository;
|
||||
|
||||
public function __construct(ProductRepository $productRepository, AttributeFamilyRepository $attributeFamilyRepository)
|
||||
public function __construct(ProductRepository $productRepository)
|
||||
{
|
||||
$this->productRepository = $productRepository;
|
||||
$this->attributeFamilyRepository = $attributeFamilyRepository;
|
||||
}
|
||||
|
||||
public function store(){
|
||||
|
|
@ -49,11 +47,15 @@ class IntegrationController extends Controller
|
|||
|
||||
public function create(){
|
||||
|
||||
$data = json_decode(request()->getContent(),true);
|
||||
try {
|
||||
$data = json_decode(request()->getContent(),true);
|
||||
}
|
||||
catch (\Exception $e){
|
||||
return response()->json(['errors'=>$e->getMessage()],400);
|
||||
}
|
||||
|
||||
$validation = Validator::make($data, [
|
||||
'type' => 'required',
|
||||
'category' => 'required',
|
||||
// 'category' => 'required',
|
||||
'sku' => ['required', 'unique:products,sku', new Slug],
|
||||
'images' => 'required',
|
||||
'name' => 'required',
|
||||
|
|
@ -62,28 +64,11 @@ class IntegrationController extends Controller
|
|||
]);
|
||||
|
||||
if ($validation->fails()) {
|
||||
return response()->json($validation->getMessageBag()->all());
|
||||
return response()->json(['errors'=>$validation->getMessageBag()->all()],422);
|
||||
}
|
||||
|
||||
$product['sku'] = $data['sku'];
|
||||
|
||||
//todo test here add some attributes,families
|
||||
$product['attribute_family_id'] = $this->getAttributeFamily(array_keys($data['attributes']));
|
||||
|
||||
$product['super_attributes']= [];
|
||||
|
||||
$product['type'] = ($data->color_variants != null || $data->size_variants != null) ? 'configurable':'simple';
|
||||
|
||||
//$this->productRepository->create($product);
|
||||
return $this->productRepository->create($data);
|
||||
|
||||
}
|
||||
|
||||
//find attribute family
|
||||
private function getAttributeFamily($attrubetCodes){
|
||||
if($attrubetCodes)
|
||||
return $this->attributeFamilyRepository->whereHas('custom_attributes',function ($query) use ($attrubetCodes){
|
||||
$query->whereIn('attributes.code', $attrubetCodes);
|
||||
},'=',count($attrubetCodes))->first()->id ?? 1;
|
||||
return 1; //default attribute family
|
||||
}
|
||||
}
|
||||
|
|
@ -18,7 +18,7 @@ class Scrap
|
|||
public function handle($request, Closure $next)
|
||||
{
|
||||
if(!$request->hasHeader('Authorization') || $request->header('Authorization') != '0a358dd1-2b07-4cdf-9d9a-a68dac6bb5fc') {
|
||||
return response()->json(['message'=>"Unauthorized request"],401);
|
||||
return response()->json(['errors'=>"Unauthorized request"],401);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
|
|
|
|||
|
|
@ -53,24 +53,24 @@ class Product extends JsonResource
|
|||
|
||||
'is_wishlisted' => $this->wishlistHelper->getWishlistProduct($product) ? true : false,
|
||||
'is_item_in_cart' => \Cart::hasProduct($product),
|
||||
'show_quantity_changer' => $this->when(
|
||||
$product->type !== 'grouped',
|
||||
$product->getTypeInstance()->showQuantityBox()
|
||||
),
|
||||
// 'show_quantity_changer' => $this->when(
|
||||
// $product->type !== 'grouped',
|
||||
// $product->getTypeInstance()->showQuantityBox()
|
||||
// ),
|
||||
/*
|
||||
* attributes
|
||||
*/
|
||||
'specifications' => app('Webkul\Product\Helpers\View')->getAdditionalData($product),
|
||||
// 'specifications' => app('Webkul\Product\Helpers\View')->getAdditionalData($product),
|
||||
/* product's extra information */
|
||||
$this->merge($this->allProductExtraInfo()),
|
||||
// $this->merge($this->allProductExtraInfo()),
|
||||
|
||||
/* special price cases */
|
||||
$this->merge($this->specialPriceInfo()),
|
||||
|
||||
/* super attributes */
|
||||
$this->mergeWhen($productTypeInstance->isComposite(), [
|
||||
'super_attributes' => Attribute::collection($product->super_attributes),
|
||||
]),
|
||||
// $this->mergeWhen($productTypeInstance->isComposite(), [
|
||||
// 'super_attributes' => Attribute::collection($product->super_attributes),
|
||||
// ]),
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,16 +27,16 @@ class ProductVariant extends JsonResource
|
|||
'converted_price' => core()->convertPrice($productTypeInstance->getMinimalPrice()),
|
||||
"color" => $this->color,
|
||||
"size"=>$this->size,
|
||||
"brand"=>$this->brand,
|
||||
// "brand"=>$this->brand,
|
||||
|
||||
"special_price"=> $this->special_price,
|
||||
"special_price_from"=>$this->special_price_from,
|
||||
"special_price_to"=>$this->special_price_to,
|
||||
"length"=>$this->length,
|
||||
"width"=>$this->width,
|
||||
"height"=>$this->height,
|
||||
"weight"=>$this->weight,
|
||||
"quantity" => $this->inventories->sum('qty')
|
||||
"special_price_to"=>$this->special_price_to,
|
||||
// "length"=>$this->length,
|
||||
// "width"=>$this->width,
|
||||
// "height"=>$this->height,
|
||||
// "weight"=>$this->weight,
|
||||
// "quantity" => $this->inventories->sum('qty')
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,126 @@
|
|||
<?php
|
||||
|
||||
namespace Sarga\API\Repositories;
|
||||
|
||||
use Illuminate\Container\Container as App;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Webkul\Attribute\Repositories\AttributeGroupRepository;
|
||||
use Webkul\Attribute\Repositories\AttributeOptionRepository;
|
||||
use Webkul\Attribute\Repositories\AttributeRepository;
|
||||
use Webkul\Product\Repositories\ProductRepository as WProductRepository;
|
||||
|
||||
class ProductRepository extends WProductRepository
|
||||
{
|
||||
protected $attributeGroupRepo;
|
||||
protected $optionRepository;
|
||||
|
||||
public function __construct(AttributeRepository $attributeRepository,
|
||||
App $app,
|
||||
AttributeGroupRepository $attributeGroupRepo,
|
||||
AttributeOptionRepository $optionRepository)
|
||||
{
|
||||
$this->attributeGroupRepo = $attributeGroupRepo;
|
||||
$this->optionRepository = $optionRepository;
|
||||
parent::__construct($attributeRepository, $app);
|
||||
}
|
||||
|
||||
public function create($data){
|
||||
$product['sku'] = $data['sku'];
|
||||
$product['status'] = true;
|
||||
|
||||
$product['type'] = (!empty($data['color_variants']) || !empty($data['size_variants'])) ? 'configurable':'simple';
|
||||
//todo test here add some attributes,families
|
||||
|
||||
if(array_key_exists('attributes',$data)){
|
||||
$grp = $this->getAttributeFamily(array_keys(Arr::collapse($data['attributes'])));
|
||||
$product['attribute_family_id'] = $grp ? $grp->attribute_family_id :
|
||||
(($product['type'] == 'configurable') ? 2:1);//default_configurable_product: default_simple_prodcut
|
||||
}
|
||||
else
|
||||
$product['attribute_family_id'] = $product['type'] == 'configurable' ? 2:1;
|
||||
|
||||
if(!empty($data['size_variants'])){
|
||||
$sizes = Arr::pluck($data['size_variants'],'size');
|
||||
|
||||
if(!empty($data['color_variants'])){
|
||||
|
||||
$variant_size = Arr::pluck(Arr::collapse(Arr::pluck($data['color_variants'],'size_variants')),'size');
|
||||
$sizes = array_unique(array_merge($sizes, $variant_size));
|
||||
}
|
||||
|
||||
if($sizes){
|
||||
$product['super_attributes']['size'] = $this->attributeValues($sizes,'size');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(!empty($data['color_variants'])){
|
||||
$colors = Arr::pluck($data['color_variants'],'color');
|
||||
$colors[] = $data['color'];
|
||||
$product['super_attributes']['color'] = $this->attributeValues($colors,'color');
|
||||
}
|
||||
|
||||
$productCreated = $this->model()->create($product);
|
||||
|
||||
if($product['type'] != 'configurable'){
|
||||
Event::dispatch('catalog.product.create.after', $product);
|
||||
}else{
|
||||
|
||||
}
|
||||
|
||||
// if($productCreated && !empty($data['attributes'])){
|
||||
// $this->updateAttributes($productCreated,$data['attributes']);
|
||||
// }
|
||||
|
||||
return $productCreated;
|
||||
}
|
||||
|
||||
private function attributeValues($values,$attributeCode){
|
||||
|
||||
$attribute = $this->attributeRepository->getAttributeByCode($attributeCode);
|
||||
|
||||
$all_options = $attribute->options()
|
||||
->orderBy('sort_order','asc')
|
||||
->get();
|
||||
|
||||
$options = $all_options->whereIn('admin_name',$values)->pluck('admin_name')->toArray();
|
||||
//create new options if doesn exist
|
||||
if(count($values) != count($options)
|
||||
&& $new_options = array_diff($values,$options)){
|
||||
|
||||
$order = $all_options->last()->sort_order ?? 0;
|
||||
|
||||
foreach($new_options as $new_option){
|
||||
$order++;
|
||||
$this->optionRepository->create([
|
||||
'admin_name' => $new_option,
|
||||
'sort_order' => $order,
|
||||
'attribute_id' => $attribute->id
|
||||
]);
|
||||
}
|
||||
$options = array_merge($options,$new_options);
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
//find attribute family
|
||||
private function getAttributeFamily($attrubetCodes){
|
||||
$count = count($attrubetCodes);
|
||||
$str = "'" . implode("','", $attrubetCodes) . "'";
|
||||
|
||||
$grups = $this->attributeGroupRepo->leftJoin('attribute_group_mappings','attribute_groups.id','=','attribute_group_mappings.attribute_group_id')
|
||||
->leftJoin('attributes',function($join) use ($attrubetCodes) {
|
||||
$join->on('attributes.id','=','attribute_group_mappings.attribute_id')
|
||||
->whereIn('code',$attrubetCodes);
|
||||
})
|
||||
->groupBy('attribute_groups.id')
|
||||
->havingRaw("SUM(IF(attributes.code IN($str),1,0)) = $count")
|
||||
->select('attribute_groups.attribute_family_id')
|
||||
->first();
|
||||
|
||||
return $grups;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -184,7 +184,6 @@ class ProductController extends Controller
|
|||
'sku' => ['required', 'unique:products,sku', new Slug],
|
||||
]);
|
||||
|
||||
Log::info(request());
|
||||
$product = $this->productRepository->create(request()->all());
|
||||
|
||||
session()->flash('success', trans('admin::app.response.create-success', ['name' => 'Product']));
|
||||
|
|
|
|||
Loading…
Reference in New Issue