diff --git a/packages/Sarga/API/Http/Controllers/IntegrationController.php b/packages/Sarga/API/Http/Controllers/IntegrationController.php index 735d67810..6b25b8864 100644 --- a/packages/Sarga/API/Http/Controllers/IntegrationController.php +++ b/packages/Sarga/API/Http/Controllers/IntegrationController.php @@ -56,7 +56,7 @@ class IntegrationController extends Controller $validation = Validator::make($data, [ // 'category' => 'required', - 'sku' => ['required', 'unique:products,sku', new Slug], + 'product_code' => ['required', 'unique:products,sku', new Slug], 'images' => 'required', 'name' => 'required', 'url_key'=> 'required', @@ -67,8 +67,10 @@ class IntegrationController extends Controller return response()->json(['errors'=>$validation->getMessageBag()->all()],422); } + //todo return product id return $this->productRepository->create($data); + } } \ 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 fc605e15e..e4b900d9d 100644 --- a/packages/Sarga/API/Http/Resources/Catalog/Product.php +++ b/packages/Sarga/API/Http/Resources/Catalog/Product.php @@ -66,9 +66,9 @@ class Product extends JsonResource $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/ProductType/Scrapable.php b/packages/Sarga/API/ProductType/Scrapable.php new file mode 100644 index 000000000..a18e3b3ef --- /dev/null +++ b/packages/Sarga/API/ProductType/Scrapable.php @@ -0,0 +1,146 @@ +productRepository->getModel()->create($data); + + if($product['type'] != 'configurable'){ + Event::dispatch('catalog.product.create.after', $product); + return; + } + + if (isset($data['super_attributes'])) { + $super_attributes = []; + + foreach ($data['super_attributes'] as $attributeCode => $attributeOptions) { + $attribute = $this->attributeRepository->findOneByField('code', $attributeCode); + + $super_attributes[$attribute->id] = $attributeOptions; + + $product->super_attributes()->attach($attribute->id); + } + + foreach (array_permutation($super_attributes) as $permutation) { + $this->createVariant($product, $permutation); + } + } + + return $product; + } + + public function createVariant($product, $permutation, $data = []) + { + if (! count($data)) { + $data = [ + 'sku' => $product->sku . '-variant-' . implode('-', $permutation), + 'name' => '', + 'inventories' => [], + 'price' => 0, + 'weight' => 0, + 'status' => 1, + ]; + } + + $data = $this->fillRequiredFields($data); + + $typeOfVariants = 'simple'; + $productInstance = app(config('product_types.' . $product->type . '.class')); + + if (isset($productInstance->variantsType) && ! in_array($productInstance->variantsType , ['bundle', 'configurable', 'grouped'])) { + $typeOfVariants = $productInstance->variantsType; + } + + $variant = $this->productRepository->getModel()->create([ + 'parent_id' => $product->id, + 'type' => $typeOfVariants, + 'attribute_family_id' => $product->attribute_family_id, + 'sku' => $data['sku'], + ]); + + foreach ($this->fillableTypes as $attributeCode) { + if (! isset($data[$attributeCode])) { + continue; + } + + $attribute = $this->attributeRepository->findOneByField('code', $attributeCode); + + if ($attribute->value_per_channel) { + if ($attribute->value_per_locale) { + foreach (core()->getAllChannels() as $channel) { + foreach (core()->getAllLocales() as $locale) { + $this->attributeValueRepository->create([ + 'product_id' => $variant->id, + 'attribute_id' => $attribute->id, + 'channel' => $channel->code, + 'locale' => $locale->code, + 'value' => $data[$attributeCode], + ]); + } + } + } else { + foreach (core()->getAllChannels() as $channel) { + $this->attributeValueRepository->create([ + 'product_id' => $variant->id, + 'attribute_id' => $attribute->id, + 'channel' => $channel->code, + 'value' => $data[$attributeCode], + ]); + } + } + } else { + if ($attribute->value_per_locale) { + foreach (core()->getAllLocales() as $locale) { + $this->attributeValueRepository->create([ + 'product_id' => $variant->id, + 'attribute_id' => $attribute->id, + 'locale' => $locale->code, + 'value' => $data[$attributeCode], + ]); + } + } else { + $this->attributeValueRepository->create([ + 'product_id' => $variant->id, + 'attribute_id' => $attribute->id, + 'value' => $data[$attributeCode], + ]); + } + } + } + + foreach ($permutation as $attributeId => $optionId) { + $this->attributeValueRepository->create([ + 'product_id' => $variant->id, + 'attribute_id' => $attributeId, + 'value' => $optionId, + ]); + } + + $this->productInventoryRepository->saveInventories($data, $variant); + + return $variant; + } + + public function fillRequiredFields(array $data): array + { + /** + * Name field is not present when variant is created so adding sku. + */ + return array_merge($data, [ + 'url_key' => $data['sku'], + 'short_description' => $data['sku'], + 'description' => $data['sku'] + ]); + } +} \ No newline at end of file diff --git a/packages/Sarga/API/Repositories/ProductRepository.php b/packages/Sarga/API/Repositories/ProductRepository.php index 98a10cad6..c21278ac5 100644 --- a/packages/Sarga/API/Repositories/ProductRepository.php +++ b/packages/Sarga/API/Repositories/ProductRepository.php @@ -61,7 +61,10 @@ class ProductRepository extends WProductRepository $product['super_attributes']['color'] = $this->attributeValues($colors,'color'); } - $productCreated = $this->model()->create($product); + $productCreated = $this->getModel()->create($product); + //product flat + //attributes + //images if($product['type'] != 'configurable'){ Event::dispatch('catalog.product.create.after', $product);