active brands
This commit is contained in:
parent
c662823711
commit
adc7001963
|
|
@ -63,7 +63,7 @@ class IntegrationController extends Controller
|
|||
return response()->json(['errors'=>$validation->getMessageBag()->all()],422);
|
||||
}
|
||||
|
||||
if($product = $this->productRepository->findOneByField('sku',$data['sku'])){
|
||||
if($product = $this->productRepository->findOneByField('product_number',$data['product_number'])){//product_group_id
|
||||
return response()->json(['success'=>true,'product_id' => $product->id]);
|
||||
}
|
||||
elseif($product = $this->productRepository->create($data)){
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ class ProductController extends Controller
|
|||
return response()->json(['errors'=>$validation->getMessageBag()->all()],422);
|
||||
}
|
||||
|
||||
if($product = $this->productRepository->findOneByField('sku',$data['sku']))
|
||||
if($product = $this->productRepository->findOneByField('sku',$data['product_group_id']))//product_group_id
|
||||
{
|
||||
return response()->json(['success'=>true,'product_id' => $product->id]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,187 @@
|
|||
<?php
|
||||
|
||||
namespace Sarga\Importer\src;
|
||||
|
||||
use Illuminate\Container\Container as Application;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
use Sarga\Brand\Repositories\BrandRepository;
|
||||
use Sarga\Shop\Repositories\AttributeValueRepository;
|
||||
use Sarga\Shop\Repositories\VendorRepository;
|
||||
use Webkul\Attribute\Repositories\AttributeGroupRepository;
|
||||
use Webkul\Attribute\Repositories\AttributeRepository;
|
||||
|
||||
class ImportRepository extends \Webkul\Core\Eloquent\Repository
|
||||
{
|
||||
|
||||
public function __construct(protected AttributeRepository $attributeRepository,
|
||||
protected AttributeValueRepository $attributeValueRepository,
|
||||
protected AttributeGroupRepository $attributeGroupRepo,
|
||||
protected BrandRepository $brandRepository,
|
||||
protected VendorRepository $vendorRepository,
|
||||
Application $app)
|
||||
{
|
||||
parent::__construct($app);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function model()
|
||||
{
|
||||
return \Webkul\Product\Contracts\Product::class;
|
||||
}
|
||||
|
||||
public function create($data){
|
||||
// $time_start = microtime(true);
|
||||
$attributes = Arr::only($data,['source','cinsiyet']);
|
||||
|
||||
$product = [
|
||||
'sku' => 'G-'.$data['product_group_id'],
|
||||
'type' => (!empty($data['color_variants']) || !empty($data['size_variants'])) ? 'configurable':'simple'
|
||||
];
|
||||
|
||||
if (array_key_exists('attributes', $data)) {
|
||||
$attributes [] = Arr::collapse($data['attributes']);
|
||||
|
||||
$grp = $this->getAttributeFamily(array_keys($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;
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
//brand
|
||||
if(!empty($data['brand']) &&
|
||||
$brand = $this->brandRepository->firstOrCreate([
|
||||
'name' => $data['brand'],
|
||||
'code' => Str::slug($data['brand']),
|
||||
]))
|
||||
{
|
||||
$product['brand_id'] = $brand->id;
|
||||
}
|
||||
|
||||
$parentProduct = $this->getModel()->create($product);
|
||||
|
||||
//product categories
|
||||
if(!empty($data['categories'])){
|
||||
$parentProduct->categories()->attach($data['categories']);
|
||||
}
|
||||
|
||||
//seller
|
||||
if($data['vendor'] && $seller = $this->vendorRepository->findOneByField('shop_title',$data['vendor'])){
|
||||
$this->createSellerProduct($parentProduct, $seller->id);
|
||||
}
|
||||
|
||||
$this->{"create_".$parentProduct->type}($parentProduct,$data);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private function create_simple($product,$data)
|
||||
{
|
||||
$originalPrice = Arr::get($data, 'price.originalPrice.value');
|
||||
$discountedPrice = Arr::get($data, 'price.discountedPrice.value');
|
||||
|
||||
$main_attributes = [
|
||||
'sku' => $product->sku,
|
||||
'product_number' => $data['product_number'],
|
||||
'name' => $data['name'],
|
||||
'weight' => $data['weight'] ?? 0.45,
|
||||
'source' => $data['url_key'],
|
||||
'status' => 1,
|
||||
'visible_individually' => 1,
|
||||
'url_key' => Str::slug($data['name']),
|
||||
// 'short_description' => $data['url_key'],
|
||||
'description' => implode(array_map(fn($value): string => '<p>' . $value['description'] . '</p>', $data['descriptions']))
|
||||
];
|
||||
|
||||
if($originalPrice > $discountedPrice){
|
||||
$main_attributes['price'] = $originalPrice;
|
||||
$main_attributes['special_price'] = $discountedPrice;
|
||||
}
|
||||
else{
|
||||
$main_attributes['price'] = $discountedPrice;
|
||||
}
|
||||
|
||||
$this->assignAttributes($product, $main_attributes);
|
||||
}
|
||||
|
||||
private function create_configurable(){
|
||||
|
||||
}
|
||||
|
||||
private function assignAttributes($product, $attributes,$check_option_values = false){
|
||||
foreach($attributes as $code => $value){
|
||||
$attribute = $this->attributeRepository->findOneByField('code', $code);
|
||||
|
||||
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' => $product->id,
|
||||
'attribute_id' => $attribute->id,
|
||||
'channel' => $channel->code,
|
||||
'locale' => $locale->code,
|
||||
'value' => $value,
|
||||
]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
foreach (core()->getAllChannels() as $channel) {
|
||||
$this->attributeValueRepository->create([
|
||||
'product_id' => $product->id,
|
||||
'attribute_id' => $attribute->id,
|
||||
'channel' => $channel->code,
|
||||
'value' => $value,
|
||||
]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($attribute->value_per_locale) {
|
||||
foreach (core()->getAllLocales() as $locale) {
|
||||
$this->attributeValueRepository->create([
|
||||
'product_id' => $product->id,
|
||||
'attribute_id' => $attribute->id,
|
||||
'locale' => $locale->code,
|
||||
'value' => $value,
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
$attr = [
|
||||
'product_id' => $product->id,
|
||||
'attribute_id' => $attribute->id,
|
||||
'value' => $value,
|
||||
];
|
||||
$this->attributeValueRepository->create($attr);
|
||||
}catch (\Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -270,7 +270,7 @@ class ProductRepository extends WProductRepository
|
|||
public function create($data){
|
||||
$time_start = microtime(true);
|
||||
|
||||
$product['sku'] = $data['sku'];
|
||||
$product['sku'] = 'G-'.$data['product_group_id'];
|
||||
// return array_map(fn($value): int => $value * 2, range(1, 5));
|
||||
|
||||
$product['type'] = (!empty($data['color_variants']) || !empty($data['size_variants'])) ? 'configurable':'simple';
|
||||
|
|
@ -300,10 +300,11 @@ class ProductRepository extends WProductRepository
|
|||
}
|
||||
//create product
|
||||
$parentProduct = $this->getModel()->create($product);
|
||||
$originalPrice = Arr::get($data, 'price.discountedPrice.value');
|
||||
$originalPrice = Arr::get($data, 'price.originalPrice.value');
|
||||
$discountedPrice = Arr::get($data, 'price.discountedPrice.value');
|
||||
$main_attributes = [
|
||||
'sku' => $parentProduct->sku,
|
||||
'product_number' => $data['product_number'],
|
||||
'name' => $data['name'],
|
||||
'weight' => $data['weight'] ?? 0.45,
|
||||
'source' => $data['url_key'],
|
||||
|
|
@ -347,7 +348,7 @@ class ProductRepository extends WProductRepository
|
|||
$description = implode(array_map(fn($value): string => '<p>' . $value['description'] . '</p>', $colorVariant['descriptions']));
|
||||
if (!empty($colorVariant['size_variants'])) {
|
||||
foreach ($colorVariant['size_variants'] as $sizeVariant) {
|
||||
if($variant = $this->createVariant($parentProduct, $colorVariant['product_number'] . $sizeVariant['size']))
|
||||
if($variant = $this->createVariant($parentProduct, "{$colorVariant['product_number']}-{$sizeVariant['itemNumber']}"))
|
||||
{
|
||||
$this->assignImages($variant, $colorVariant['images']);
|
||||
$this->assignAttributes($variant, [
|
||||
|
|
|
|||
Loading…
Reference in New Issue