From 35a9e90780f0d59f75992f7b895d6d93e803de21 Mon Sep 17 00:00:00 2001 From: Herbert Maschke Date: Fri, 7 Feb 2020 11:16:00 +0100 Subject: [PATCH 1/4] add improvements to locale determination on product creation --- packages/Webkul/Core/src/Core.php | 3 + packages/Webkul/Core/src/Models/Channel.php | 14 +++- .../Webkul/Product/src/Models/Product.php | 71 +++++++++++++++---- 3 files changed, 73 insertions(+), 15 deletions(-) diff --git a/packages/Webkul/Core/src/Core.php b/packages/Webkul/Core/src/Core.php index d39459dff..2e4b1d167 100755 --- a/packages/Webkul/Core/src/Core.php +++ b/packages/Webkul/Core/src/Core.php @@ -135,6 +135,9 @@ class Core 'https://' . request()->getHttpHost(), ])->first(); + if (! $channel) + $channel = $this->channelRepository->findOneByField('code', config('app.channel')); + if (! $channel) $channel = $this->channelRepository->first(); diff --git a/packages/Webkul/Core/src/Models/Channel.php b/packages/Webkul/Core/src/Models/Channel.php index 963418413..1908a4872 100755 --- a/packages/Webkul/Core/src/Models/Channel.php +++ b/packages/Webkul/Core/src/Models/Channel.php @@ -10,7 +10,19 @@ use Webkul\Core\Contracts\Channel as ChannelContract; class Channel extends Model implements ChannelContract { - protected $fillable = ['code', 'name', 'description', 'theme', 'home_page_content', 'footer_content', 'hostname', 'default_locale_id', 'base_currency_id', 'root_category_id', 'home_seo']; + protected $fillable = [ + 'code', + 'name', + 'description', + 'theme', + 'home_page_content', + 'footer_content', + 'hostname', + 'default_locale_id', + 'base_currency_id', + 'root_category_id', + 'home_seo', + ]; /** * Get the channel locales. diff --git a/packages/Webkul/Product/src/Models/Product.php b/packages/Webkul/Product/src/Models/Product.php index ce9ab9d08..d73e46527 100755 --- a/packages/Webkul/Product/src/Models/Product.php +++ b/packages/Webkul/Product/src/Models/Product.php @@ -2,6 +2,7 @@ namespace Webkul\Product\Models; +use Webkul\Core\Models\Channel; use Illuminate\Database\Eloquent\Model; use Webkul\Attribute\Models\AttributeFamilyProxy; use Webkul\Category\Models\CategoryProxy; @@ -181,8 +182,8 @@ class Product extends Model implements ProductContract public function inventory_source_qty($inventorySourceId) { return $this->inventories() - ->where('inventory_source_id', $inventorySourceId) - ->sum('qty'); + ->where('inventory_source_id', $inventorySourceId) + ->sum('qty'); } /** @@ -243,6 +244,7 @@ class Product extends Model implements ProductContract * * @param Group $group * @param bool $skipSuperAttribute + * * @return Collection */ public function getEditableAttributes($group = null, $skipSuperAttribute = true) @@ -253,7 +255,8 @@ class Product extends Model implements ProductContract /** * Get an attribute from the model. * - * @param string $key + * @param string $key + * * @return mixed */ public function getAttribute($key) @@ -262,8 +265,9 @@ class Product extends Model implements ProductContract if (isset($this->id)) { $this->attributes[$key] = ''; - $attribute = core()->getSingletonInstance(\Webkul\Attribute\Repositories\AttributeRepository::class) - ->getAttributeByCode($key); + $attribute = core() + ->getSingletonInstance(\Webkul\Attribute\Repositories\AttributeRepository::class) + ->getAttributeByCode($key); $this->attributes[$key] = $this->getCustomAttributeValue($attribute); @@ -284,8 +288,9 @@ class Product extends Model implements ProductContract $hiddenAttributes = $this->getHidden(); if (isset($this->id)) { - $familyAttributes = core()->getSingletonInstance(\Webkul\Attribute\Repositories\AttributeRepository::class) - ->getFamilyAttributes($this->attribute_family); + $familyAttributes = core() + ->getSingletonInstance(\Webkul\Attribute\Repositories\AttributeRepository::class) + ->getFamilyAttributes($this->attribute_family); foreach ($familyAttributes as $attribute) { if (in_array($attribute->code, $hiddenAttributes)) { @@ -306,24 +311,41 @@ class Product extends Model implements ProductContract */ public function getCustomAttributeValue($attribute) { - if (! $attribute) + if (! $attribute) { return; + } $channel = request()->get('channel') ?: (core()->getCurrentChannelCode() ?: core()->getDefaultChannelCode()); - $locale = request()->get('locale') ?: app()->getLocale(); + $locale = $this->determineLocale($channel); if ($attribute->value_per_channel) { if ($attribute->value_per_locale) { - $attributeValue = $this->attribute_values()->where('channel', $channel)->where('locale', $locale)->where('attribute_id', $attribute->id)->first(); + $attributeValue = $this + ->attribute_values() + ->where('channel', $channel) + ->where('locale', $locale) + ->where('attribute_id', $attribute->id) + ->first(); } else { - $attributeValue = $this->attribute_values()->where('channel', $channel)->where('attribute_id', $attribute->id)->first(); + $attributeValue = $this + ->attribute_values() + ->where('channel', $channel) + ->where('attribute_id', $attribute->id) + ->first(); } } else { if ($attribute->value_per_locale) { - $attributeValue = $this->attribute_values()->where('locale', $locale)->where('attribute_id', $attribute->id)->first(); + $attributeValue = $this + ->attribute_values() + ->where('locale', $locale) + ->where('attribute_id', $attribute->id) + ->first(); } else { - $attributeValue = $this->attribute_values()->where('attribute_id', $attribute->id)->first(); + $attributeValue = $this + ->attribute_values() + ->where('attribute_id', $attribute->id) + ->first(); } } @@ -333,7 +355,8 @@ class Product extends Model implements ProductContract /** * Overrides the default Eloquent query builder * - * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query + * * @return \Illuminate\Database\Eloquent\Builder */ public function newEloquentBuilder($query) @@ -356,4 +379,24 @@ class Product extends Model implements ProductContract { return $this; } + + /** + * Determine if the given channel has a default locale configured. + * If not, return the channel given in the request. + * If none given, return the fallback channel given in the application configuration + * + * @param string $channel + * + * @return string + */ + private function determineLocale(string $channel): string + { + $channelModel = Channel::where(['code' => $channel])->first(); + + if ($channelModel) { + return $channelModel->default_locale->code; + } + + return request()->get('locale') ?: app()->getLocale(); + } } \ No newline at end of file From 38dbb6384d61087e8131506776fe0643947fd331 Mon Sep 17 00:00:00 2001 From: Herbert Maschke Date: Mon, 10 Feb 2020 15:05:03 +0100 Subject: [PATCH 2/4] obey the default channel and configured default locale in product edit form --- .../views/catalog/products/edit.blade.php | 15 ++-- .../Http/Controllers/ProductController.php | 69 ++++++++++++------- 2 files changed, 49 insertions(+), 35 deletions(-) diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/products/edit.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/products/edit.blade.php index 6adba4afc..4b57617fc 100755 --- a/packages/Webkul/Admin/src/Resources/views/catalog/products/edit.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/catalog/products/edit.blade.php @@ -6,9 +6,6 @@ @section('content')
- get('locale') ?: app()->getLocale(); ?> - get('channel') ?: core()->getDefaultChannelCode(); ?> - {!! view_render_event('bagisto.admin.catalog.product.edit.before', ['product' => $product]) !!}
@@ -28,7 +25,7 @@ @foreach (core()->getAllChannels() as $channelModel) @@ -41,7 +38,7 @@ @foreach (core()->getAllLocales() as $localeModel) @@ -114,11 +111,11 @@ $channel_locale = []; if ($attribute->value_per_channel) { - array_push($channel_locale, $channel); + array_push($channel_locale, $channelCode); } if ($attribute->value_per_locale) { - array_push($channel_locale, $locale); + array_push($channel_locale, $localeCode); } ?> @@ -181,9 +178,9 @@