Product flat create and update now both implemented

This commit is contained in:
Prashant Singh 2019-01-23 17:08:17 +05:30
parent 67dbce6fa8
commit 604f9d7da5
3 changed files with 172 additions and 3 deletions

View File

@ -8,6 +8,8 @@ use Illuminate\Support\Facades\Event;
use Webkul\Product\Http\Requests\ProductForm;
use Webkul\Product\Repositories\ProductRepository as Product;
use Webkul\Product\Repositories\ProductGridRepository as ProductGrid;
use Webkul\Product\Repositories\ProductFlatRepository as ProductFlat;
use Webkul\Product\Repositories\ProductAttributeValueRepository as ProductAttributeValue;
use Webkul\Attribute\Repositories\AttributeFamilyRepository as AttributeFamily;
use Webkul\Category\Repositories\CategoryRepository as Category;
use Webkul\Inventory\Repositories\InventorySourceRepository as InventorySource;
@ -61,6 +63,8 @@ class ProductController extends Controller
* @var array
*/
protected $productGrid;
protected $productFlat;
protected $productAttributeValue;
/**
* Create a new controller instance.
@ -76,7 +80,9 @@ class ProductController extends Controller
Category $category,
InventorySource $inventorySource,
Product $product,
ProductGrid $productGrid)
ProductGrid $productGrid,
ProductFlat $productFlat,
ProductAttributeValue $productAttributeValue)
{
$this->attributeFamily = $attributeFamily;
@ -88,6 +94,10 @@ class ProductController extends Controller
$this->productGrid = $productGrid;
$this->productFlat = $productFlat;
$this->productAttributeValue = $productAttributeValue;
$this->_config = request('_config');
}
@ -256,4 +266,160 @@ class ProductController extends Controller
return redirect()->route('admin.catalog.products.index');
}
public function testProductFlat() {
$product = $this->product->find(2);
$productAttributes = $product->attribute_family->custom_attributes;
$allLocales = core()->getAllLocales();
$productsFlat = array();
$channelLocaleMap = array();
$nonDependentAttributes = array();
$localeDependentAttributes = array();
$channelDependentAttributes = array();
$channelLocaleDependentAttributes = array();
foreach($productAttributes as $key => $productAttribute) {
if($productAttribute->value_per_channel) {
if($productAttribute->value_per_locale) {
array_push($channelLocaleDependentAttributes, ['id' => $productAttribute->id, 'code' => $productAttribute->code]);
} else {
array_push($channelDependentAttributes, ['id' => $productAttribute->id, 'code' => $productAttribute->code]);
}
} else if($productAttribute->value_per_locale && !$productAttribute->value_per_channel) {
array_push($localeDependentAttributes, ['id' => $productAttribute->id, 'code' => $productAttribute->code]);
} else {
array_push($nonDependentAttributes, ['id' => $productAttribute->id, 'code' => $productAttribute->code]);
}
}
foreach(core()->getAllChannels() as $channel) {
$dummy = [
'product_id' => $product->id,
'channel' => $channel->code,
'locale' => null,
'data' => $channelDependentAttributes
];
array_push($channelLocaleMap, $dummy);
$dummy = [];
foreach($channel->locales as $locale) {
$dummy = [
'product_id' => $product->id,
'channel' => $channel->code,
'locale' => $locale->code,
'data' => $channelLocaleDependentAttributes
];
array_push($channelLocaleMap, $dummy);
$dummy = [];
}
}
$dummy = [
'product_id' => $product->id,
'channel' => null,
'locale' => null,
'data' => $nonDependentAttributes
];
array_push($channelLocaleMap, $dummy);
$dummy = [];
foreach($allLocales as $key => $allLocale) {
$dummy = [
'product_id' => $product->id,
'channel' => null,
'locale' => $allLocale->code,
'data' => $localeDependentAttributes
];
array_push($channelLocaleMap, $dummy);
$dummy = [];
}
$productFlatObjects = $channelLocaleMap;
$keyOfNonDependentAttributes = null;
foreach($productAttributes as $productAttribute) {
foreach($productFlatObjects as $flatKey => $productFlatObject) {
if($productFlatObject['channel'] == null && $productFlatObject['locale'] == null) {
$keyOfNonDependentAttributes = $flatKey;
}
foreach($productFlatObject['data'] as $key => $value) {
if($productAttribute->code == $value['code']) {
$valueOf = $this->productAttributeValue->findOneWhere([
'product_id' => $product->id,
'channel' => $productFlatObject['channel'],
'locale' => $productFlatObject['locale'],
'attribute_id' => $productAttribute->id
]);
if($valueOf != null) {
$productAttributeColumn = $this->productAttributeValue->model()::$attributeTypeFields[$productAttribute->type];
$valueOf = $valueOf->{$productAttributeColumn};
$productFlatObjects[$flatKey][$productAttribute->code] = $valueOf;
} else {
$productFlatObjects[$flatKey][$productAttribute->code] = 'null';
}
}
}
}
}
$nonDependentAttributes = $productFlatObjects[$keyOfNonDependentAttributes];
array_forget($nonDependentAttributes, ['product_id', 'channel', 'locale', 'data', 'visible_individually', 'width', 'height', 'depth']);
unset($productFlatObjects[$keyOfNonDependentAttributes]);
$productFlatEntryObject = array();
$tempFlatObject = array();
foreach($productFlatObjects as $flatKey => $productFlatObject) {
unset($productFlatObject['data']);
if(isset($productFlatObject['short_description'])) {
$productFlatObject['description'] = $productFlatObject['short_description'];
unset($productFlatObject['short_description']);
}
if(isset($productFlatObject['meta_title'])) {
unset($productFlatObject['meta_title']);
unset($productFlatObject['meta_description']);
unset($productFlatObject['meta_keywords']);
}
$tempFlatObject = array_merge($productFlatObject, $nonDependentAttributes);
$tempFlatObject = core()->convertEmptyStringsToNull($tempFlatObject);
$exists = $this->productFlat->findWhere([
'product_id' => $product->id,
'channel' => $tempFlatObject['channel'],
'locale' => $tempFlatObject['locale']
]);
if($exists->count() == 0) {
$result = $this->productFlat->create($tempFlatObject);
} else {
$result = $exists->first();
$result->update($tempFlatObject);
}
unset($tempFlatObject);
}
return true;
}
}

View File

@ -255,11 +255,14 @@ class ProductsFlat
]);
if($exists->count() == 0) {
$result = $this->productFlat->create($tempFlatObject);
dd('this does not exists');
// $result = $this->productFlat->create($tempFlatObject);
} else {
//perform update
dd('product already exists');
}
dd($productFlatObjects);
unset($tempFlatObject);
}

View File

@ -108,7 +108,7 @@ class ProductGridRepository extends Repository
return $this->getModel()->where('product_id', $variant->id)->update($gridObject);
}
}
return false;
}
}