From 3c95e11df7f2d78984bbc12fb7e156cfd049d805 Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Tue, 4 Jun 2019 14:54:42 +0530 Subject: [PATCH] Cart rule labels bug fixed --- .../promotions/cart-rule/create.blade.php | 4 +- .../views/promotions/cart-rule/edit.blade.php | 29 +++++++------ .../Http/Controllers/CartRuleController.php | 18 +------- .../src/Repositories/CartRuleRepository.php | 42 +++++++++++++++++-- 4 files changed, 59 insertions(+), 34 deletions(-) diff --git a/packages/Webkul/Admin/src/Resources/views/promotions/cart-rule/create.blade.php b/packages/Webkul/Admin/src/Resources/views/promotions/cart-rule/create.blade.php index dc2f58fa7..421db019d 100644 --- a/packages/Webkul/Admin/src/Resources/views/promotions/cart-rule/create.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/promotions/cart-rule/create.blade.php @@ -316,7 +316,7 @@ - +
{{--
@@ -366,7 +366,7 @@
- + @{{ errors.first('label') }}
diff --git a/packages/Webkul/Admin/src/Resources/views/promotions/cart-rule/edit.blade.php b/packages/Webkul/Admin/src/Resources/views/promotions/cart-rule/edit.blade.php index c26ece9a1..051a47cdc 100644 --- a/packages/Webkul/Admin/src/Resources/views/promotions/cart-rule/edit.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/promotions/cart-rule/edit.blade.php @@ -38,6 +38,8 @@
+ +
@@ -232,9 +234,6 @@
- - -
@@ -352,7 +351,14 @@
@foreach($cart_rule[3]->labels as $label) + [{{ $label->channel->code }}] +
+ + + + @{{ errors.first('label') }} +
@endforeach
@@ -405,15 +411,14 @@ dedicated_label: true, global_label: null, - label: { - @foreach(core()->getAllChannels() as $channel) - @foreach($channel->locales as $locale) - {{ trim($channel->code) }} : { - {{ trim($locale->code) }}: '' - }, - @endforeach - @endforeach - }, + labels: [], + // label: { + // @foreach(core()->getAllChannels() as $channel) + // @foreach($channel->locales as $locale) + // {{ trim($channel->code) }} : null, + // @endforeach + // @endforeach + // }, criteria: null, conditions: @json($cart_rule[0]).conditions, diff --git a/packages/Webkul/Discount/src/Http/Controllers/CartRuleController.php b/packages/Webkul/Discount/src/Http/Controllers/CartRuleController.php index 5fc1c3a79..aca8d490e 100644 --- a/packages/Webkul/Discount/src/Http/Controllers/CartRuleController.php +++ b/packages/Webkul/Discount/src/Http/Controllers/CartRuleController.php @@ -105,7 +105,6 @@ class CartRuleController extends Controller 'channels' => 'required|array', 'status' => 'required|boolean', 'use_coupon' => 'boolean|required', - // 'auto_generation' => 'boolean|sometimes', 'usage_limit' => 'numeric|min:0', 'per_customer' => 'numeric|min:0', 'action_type' => 'required|string', @@ -282,8 +281,6 @@ class CartRuleController extends Controller $data = request()->all(); - dd($data); - if ($data['starts_from'] == "" || $data['ends_till'] == "") { $data['starts_from'] = null; $data['ends_till'] = null; @@ -362,20 +359,7 @@ class CartRuleController extends Controller $ruleGroupUpdated = $this->cartRule->CustomerGroupSync($customer_groups, $ruleUpdated); $ruleChannelUpdated = $this->cartRule->ChannelSync($channels, $ruleUpdated); - // if (isset($labels['global'])) { - // foreach (core()->getAllChannels() as $channel) { - // $label1['channel_id'] = $channel->id; - // foreach ($channel->locales as $locale) { - // $label1['locale_id'] = $locale->id; - // $label1['label'] = $labels['global']; - - // $ruleLabelUpdated = $this->cartRuleLabel->create($label1); - // } - // } - // } else { - // $label2['label'] = $labels['global']; - // $ruleLabelUpdated = $this->cartRuleLabel->create($label2); - // } + $labelsUpdated = $this->cartRule->LabelSync($labels, $ruleUpdated); if (isset($coupons)) { $coupons['cart_rule_id'] = $ruleUpdated->id; diff --git a/packages/Webkul/Discount/src/Repositories/CartRuleRepository.php b/packages/Webkul/Discount/src/Repositories/CartRuleRepository.php index 79fdb6450..3eebebc06 100644 --- a/packages/Webkul/Discount/src/Repositories/CartRuleRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CartRuleRepository.php @@ -33,7 +33,7 @@ class CartRuleRepository extends Repository $this->cartRuleChannels = $cartRuleChannels; $this->cartRuleCustomerGroups = $cartRuleCustomerGroups; $this->cartRuleCoupons = $cartRuleCoupons; - $this->cartRuleLabels = $cartRuleLabels; + $this->cartRuleLabel = $cartRuleLabels; parent::__construct($app); } @@ -146,15 +146,51 @@ class CartRuleRepository extends Repository /** * To sync the labels associated with the cart rule */ - public function LabelsSync($labels, $cartRule) + public function LabelSync($labels, $cartRule) { + foreach ($labels as $channelCode => $value) { + $localeCode = array_keys($value)[0]; + $localeValue = array_values($value)[0]; + $updated = 0; + foreach ($cartRule->labels as $label) { + if ($label->channel->code == $channelCode && $label->locale->code == $localeCode) { + $label->update([ + 'label' => $localeValue + ]); + + $updated = 1; + } + } + + if ($updated == 0) { + foreach (core()->getAllChannels() as $channel) { + if ($channel->code == $channelCode) { + $newLabel['channel_id'] = $channel->id; + } + + foreach($channel->locales as $locale) { + if ($localeCode == $locale->code) { + $newLabel['locale_id'] = $locale->id; + $newLabel['label'] = $localeValue; + $newLabel['cart_rule_id'] = $cartRule->id; + + $ruleLabelCreated = $this->CartRuleLabels->create($newLabel); + } + } + } + } + + $updated = 0; + } + + return true; } /** * To sync the coupons associated with the cart rule */ - public function CouponsSync($coupon, $cartRule) + public function CouponSync($coupon, $cartRule) { }