Product flat implementation working, now binding it with the product update evento to write the
This commit is contained in:
parent
07daf7d310
commit
67dbce6fa8
|
|
@ -775,5 +775,15 @@ class Core
|
|||
}
|
||||
|
||||
return $merged;
|
||||
}
|
||||
}
|
||||
|
||||
public function convertEmptyStringsToNull($array) {
|
||||
foreach($array as $key => $value) {
|
||||
if($value == "" || $value == "null") {
|
||||
$array[$key] = null;
|
||||
}
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
|
|
@ -15,8 +15,10 @@ class AddNewFromToColumnsInProductFlat extends Migration
|
|||
{
|
||||
if (Schema::hasTable('product_flat')) {
|
||||
Schema::table('product_flat', function (Blueprint $table) {
|
||||
$table->integer('tax_category_id')->unsigned()->nullable()->after('size_label');
|
||||
$table->date('new_to')->after('new');
|
||||
$table->date('new_from')->after('new');
|
||||
$table->unique(['product_id', 'channel', 'locale'], 'product_flat_unique_index');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -30,8 +32,10 @@ class AddNewFromToColumnsInProductFlat extends Migration
|
|||
{
|
||||
if (Schema::hasTable('product_flat')) {
|
||||
Schema::table('product_flat', function (Blueprint $table) {
|
||||
$table->dropColumn('tax_category_id')->unsigned();
|
||||
$table->dropColumn('new_from');
|
||||
$table->dropColumn('new_to');
|
||||
$table->dropIndex('product_flat_unique_index');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,8 +8,6 @@ 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;
|
||||
|
|
@ -64,15 +62,6 @@ class ProductController extends Controller
|
|||
*/
|
||||
protected $productGrid;
|
||||
|
||||
/**
|
||||
* ProductFlat Repository Object
|
||||
*
|
||||
* @vatr array
|
||||
*/
|
||||
protected $productFlat;
|
||||
protected $productAttributeValue;
|
||||
protected $attribute;
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
|
|
@ -87,9 +76,7 @@ class ProductController extends Controller
|
|||
Category $category,
|
||||
InventorySource $inventorySource,
|
||||
Product $product,
|
||||
ProductGrid $productGrid,
|
||||
ProductFlat $productFlat,
|
||||
ProductAttributeValue $productAttributeValue)
|
||||
ProductGrid $productGrid)
|
||||
{
|
||||
$this->attributeFamily = $attributeFamily;
|
||||
|
||||
|
|
@ -101,10 +88,6 @@ class ProductController extends Controller
|
|||
|
||||
$this->productGrid = $productGrid;
|
||||
|
||||
$this->productFlat = $productFlat;
|
||||
|
||||
$this->productAttributeValue = $productAttributeValue;
|
||||
|
||||
$this->_config = request('_config');
|
||||
}
|
||||
|
||||
|
|
@ -273,123 +256,4 @@ class ProductController extends Controller
|
|||
|
||||
return redirect()->route('admin.catalog.products.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing for the product flat sync method on product creation and updation
|
||||
*/
|
||||
public function testProductFlat() {
|
||||
$product = $this->product->find(4);
|
||||
$productAttributes = $product->attribute_family->custom_attributes;
|
||||
$allLocales = core()->getAllLocales();
|
||||
$productsFlat = array();
|
||||
$attributeNames = array();
|
||||
$channelLocaleMap = array();
|
||||
$nonDependentAttributes = array();
|
||||
$localeDependentAttributes = array();
|
||||
$channelDependentAttributes = array();
|
||||
$channelLocaleDependentAttributes = array();
|
||||
|
||||
foreach($productAttributes as $key => $productAttribute) {
|
||||
$attributeNames[$key] = [
|
||||
'id' => $productAttribute->id,
|
||||
'code' => $productAttribute->code,
|
||||
'value_per_locale' => $productAttribute->value_per_locale,
|
||||
'value_per_channel' => $productAttribute->value_per_channel,
|
||||
];
|
||||
}
|
||||
|
||||
foreach($productAttributes as $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;
|
||||
|
||||
foreach($productAttributes as $productAttribute) {
|
||||
foreach($productFlatObjects as $flatKey => $productFlatObject) {
|
||||
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';
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dd($productFlatObjects);
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,8 @@ namespace Webkul\Product\Listeners;
|
|||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
use Webkul\Product\Repositories\ProductFlatRepository as ProductFlat;
|
||||
use Webkul\Product\Repositories\ProductAttributeValueRepository as ProductAttributeValue;
|
||||
/**
|
||||
* Products Flat Event handler
|
||||
*
|
||||
|
|
@ -13,6 +15,23 @@ use Illuminate\Database\Schema\Blueprint;
|
|||
*/
|
||||
class ProductsFlat
|
||||
{
|
||||
/**
|
||||
* ProductFlat Repository Object
|
||||
*
|
||||
* @vatr array
|
||||
*/
|
||||
protected $productFlat;
|
||||
|
||||
protected $productAttributeValue;
|
||||
|
||||
protected $attribute;
|
||||
|
||||
public function __construct(ProductFlat $productFlat, ProductAttributeValue $productAttributeValue) {
|
||||
$this->productAttributeValue = $productAttributeValue;
|
||||
|
||||
$this->productFlat = $productFlat;
|
||||
}
|
||||
|
||||
/**
|
||||
* After the attribute is created
|
||||
*
|
||||
|
|
@ -92,4 +111,158 @@ class ProductsFlat
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function afterProductCreated($Product) {
|
||||
$product = $Product;
|
||||
$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 {
|
||||
//perform update
|
||||
}
|
||||
|
||||
unset($tempFlatObject);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -17,4 +17,6 @@ class ProductFlat extends Model
|
|||
protected $table = 'product_flat';
|
||||
|
||||
protected $guarded = ['id', 'created_at', 'updated_at'];
|
||||
|
||||
public $timestamps = false;
|
||||
}
|
||||
Loading…
Reference in New Issue