diff --git a/packages/Sarga/API/Http/Controllers/IntegrationController.php b/packages/Sarga/API/Http/Controllers/IntegrationController.php index 3206c347a..735d67810 100644 --- a/packages/Sarga/API/Http/Controllers/IntegrationController.php +++ b/packages/Sarga/API/Http/Controllers/IntegrationController.php @@ -1,22 +1,20 @@ 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 - } } \ No newline at end of file diff --git a/packages/Sarga/API/Http/Middleware/Scrap.php b/packages/Sarga/API/Http/Middleware/Scrap.php index 8c83322d2..537fde8c4 100644 --- a/packages/Sarga/API/Http/Middleware/Scrap.php +++ b/packages/Sarga/API/Http/Middleware/Scrap.php @@ -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); diff --git a/packages/Sarga/API/Http/Resources/Catalog/Product.php b/packages/Sarga/API/Http/Resources/Catalog/Product.php index d1cb85606..3e9f3c106 100644 --- a/packages/Sarga/API/Http/Resources/Catalog/Product.php +++ b/packages/Sarga/API/Http/Resources/Catalog/Product.php @@ -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), +// ]), ]; } diff --git a/packages/Sarga/API/Http/Resources/Catalog/ProductVariant.php b/packages/Sarga/API/Http/Resources/Catalog/ProductVariant.php index 1a7cb190b..ee6d1e5cb 100644 --- a/packages/Sarga/API/Http/Resources/Catalog/ProductVariant.php +++ b/packages/Sarga/API/Http/Resources/Catalog/ProductVariant.php @@ -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') ]; } diff --git a/packages/Sarga/API/Repositories/ProductRepository.php b/packages/Sarga/API/Repositories/ProductRepository.php new file mode 100644 index 000000000..98a10cad6 --- /dev/null +++ b/packages/Sarga/API/Repositories/ProductRepository.php @@ -0,0 +1,126 @@ +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; + + } +} \ No newline at end of file diff --git a/packages/Webkul/Product/src/Http/Controllers/ProductController.php b/packages/Webkul/Product/src/Http/Controllers/ProductController.php index 0c05db7d6..9a03d06f6 100755 --- a/packages/Webkul/Product/src/Http/Controllers/ProductController.php +++ b/packages/Webkul/Product/src/Http/Controllers/ProductController.php @@ -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']));