From 1ea6ae815497cf01d479bf2325406ac04cb2594e Mon Sep 17 00:00:00 2001 From: Obinna Elvis Okechukwu Date: Mon, 30 Oct 2023 20:24:33 +0100 Subject: [PATCH] Feature delivery coupon (#38) * make changes to the coupons table and coupons settings * apply coupon on delivery fee full integration * added data migration to the coupon migration file and moved those code mentioned from beforeApply to calculateDeliveryDiscount function * Fix code style Signed-off-by: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com> * Fix code style Signed-off-by: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com> --------- Signed-off-by: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com> Co-authored-by: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com> --- actions/RedeemsCoupon.php | 2 - cartconditions/Coupon.php | 35 ++++++++++++---- ...d_to_cart_item_to_apply_coupon_on_enum.php | 40 +++++++++++++++++++ language/en/default.php | 5 ++- models/config/coupons_model.php | 18 +++++---- 5 files changed, 82 insertions(+), 18 deletions(-) create mode 100644 database/migrations/2023_10_19_010000_change_is_limited_to_cart_item_to_apply_coupon_on_enum.php diff --git a/actions/RedeemsCoupon.php b/actions/RedeemsCoupon.php index 9b99a66..9c7292a 100644 --- a/actions/RedeemsCoupon.php +++ b/actions/RedeemsCoupon.php @@ -2,11 +2,9 @@ namespace Igniter\Coupons\Actions; -use Carbon\Carbon; use Igniter\Coupons\Models\Coupons_history_model; use Igniter\Flame\Cart\CartCondition; use Igniter\Flame\Traits\ExtensionTrait; -use Illuminate\Support\Facades\Event; use System\Actions\ModelAction; class RedeemsCoupon extends ModelAction diff --git a/cartconditions/Coupon.php b/cartconditions/Coupon.php index 2eb2db3..cd8f82b 100644 --- a/cartconditions/Coupon.php +++ b/cartconditions/Coupon.php @@ -74,8 +74,7 @@ public function onLoad() $this->validateCoupon($couponModel); $this->getApplicableItems($couponModel); - } - catch (Exception $ex) { + } catch (Exception $ex) { if (!optional($couponModel)->auto_apply) flash()->alert($ex->getMessage())->now(); @@ -86,7 +85,7 @@ public function onLoad() public function beforeApply() { $couponModel = $this->getModel(); - if (!$couponModel || $couponModel->is_limited_to_cart_item) + if (!$couponModel || $couponModel->apply_coupon_on == 'menu_items') return false; } @@ -94,8 +93,10 @@ public function getActions() { $value = optional($this->getModel())->discountWithOperand(); - // if we are item limited and not a % we need to apportion - if (stripos($value, '%') === false && optional($this->getModel())->is_limited_to_cart_item) { + if (optional($this->getModel())->apply_coupon_on == 'delivery_fee') { + $value = $this->calculateDeliveryDiscount(); + } // if we are item limited and not a % we need to apportion + elseif (stripos($value, '%') === false && optional($this->getModel())->apply_coupon_on == 'menu_items') { $value = $this->calculateApportionment($value); } @@ -144,6 +145,24 @@ protected function calculateApportionment($value) return $value; } + protected function calculateDeliveryDiscount() + { + $cartSubtotal = Cart::subtotal(); + $deliveryCharge = Location::coveredArea()->deliveryAmount($cartSubtotal); + $couponModel = optional($this->getModel()); + if ($couponModel->isFixed()) { + if ($couponModel->discount > $deliveryCharge) { + $value = $deliveryCharge; + } else { + $value = $couponModel->discount; + } + } else { + $value = $deliveryCharge * ($couponModel->discount * 0.01); + } + + return '-'.$value; + } + protected function validateCoupon($couponModel) { $user = Auth::getUser(); @@ -166,8 +185,8 @@ protected function validateCoupon($couponModel) throw new ApplicationException(lang('igniter.cart::default.alert_coupon_maximum_reached')); if (($couponModel->customer_redemptions - || optional($couponModel->customers)->isNotEmpty() - || optional($couponModel->customer_groups)->isNotEmpty()) && !$user + || optional($couponModel->customers)->isNotEmpty() + || optional($couponModel->customer_groups)->isNotEmpty()) && !$user ) throw new ApplicationException(lang('igniter.coupons::default.alert_coupon_login_required')); if ($user && $couponModel->customerHasMaxRedemption($user)) @@ -185,7 +204,7 @@ public static function isApplicableTo($cartItem) if (!$couponModel = self::$couponModel) return false; - if (!$couponModel->is_limited_to_cart_item) + if ($couponModel->apply_coupon_on != 'menu_items') return false; if (!$applicableItems = self::$applicableItems) diff --git a/database/migrations/2023_10_19_010000_change_is_limited_to_cart_item_to_apply_coupon_on_enum.php b/database/migrations/2023_10_19_010000_change_is_limited_to_cart_item_to_apply_coupon_on_enum.php new file mode 100644 index 0000000..0371bab --- /dev/null +++ b/database/migrations/2023_10_19_010000_change_is_limited_to_cart_item_to_apply_coupon_on_enum.php @@ -0,0 +1,40 @@ +enum( + 'apply_coupon_on', + ['whole_cart', 'menu_items', 'delivery_fee'] + )->default('whole_cart')->after('order_restriction'); + }); + $this->updateApplyCouponOnEnum(); + Schema::table('igniter_coupons', function (Blueprint $table) { + $table->dropColumn('is_limited_to_cart_item'); + }); + } + + // migrate is_limited_to_cart_item to the new apply_coupon_on enum that supports multiple options + protected function updateApplyCouponOnEnum() + { + DB::table('igniter_coupons') + ->where('is_limited_to_cart_item', 1)->get()->each( + function ($model) { + DB::table('igniter_coupons') + ->where('coupon_id', $model->coupon_id) + ->update([ + 'apply_coupon_on' => 'menu_items', + ]); + } + ); + } +} diff --git a/language/en/default.php b/language/en/default.php index 924d9c3..571eb17 100644 --- a/language/en/default.php +++ b/language/en/default.php @@ -24,6 +24,9 @@ 'text_redeemed' => 'Redeemed', 'text_not_redeemed' => 'Not Yet Redeemed', 'text_coupon' => 'Coupon [%s]', + 'text_cart_restriction_whole_cart' => 'Whole Cart', + 'text_cart_restriction_menu_items' => 'Menu Items', + 'text_cart_restriction_delivery_fee' => 'Delivery Fee', 'alert_coupon_login_required' => 'Please login or register to use this coupon', @@ -46,7 +49,7 @@ 'label_customer_group' => 'Customer Groups', 'label_validity' => 'Validity', 'label_order_restriction' => 'Order Restriction', - 'label_cart_restriction' => 'Do not apply to the whole cart', + 'label_cart_restriction' => 'Apply this coupon on', 'label_categories' => 'Apply discount to menu items from these categories', 'label_menus' => 'Apply discount to these menu items', 'label_date' => 'Date', diff --git a/models/config/coupons_model.php b/models/config/coupons_model.php index 763f78a..2c199aa 100644 --- a/models/config/coupons_model.php +++ b/models/config/coupons_model.php @@ -356,11 +356,15 @@ 'nameFrom' => 'location_name', 'comment' => 'lang:igniter.coupons::default.help_locations', ], - 'is_limited_to_cart_item' => [ + 'apply_coupon_on' => [ 'tab' => 'lang:igniter.coupons::default.text_tab_restrictions', 'label' => 'lang:igniter.coupons::default.label_cart_restriction', - 'type' => 'switch', - 'default' => 0, + 'type' => 'select', + 'options' => [ + 'whole_cart'=> 'lang:igniter.coupons::default.text_cart_restriction_whole_cart', + 'menu_items'=> 'lang:igniter.coupons::default.text_cart_restriction_menu_items', + 'delivery_fee'=> 'lang:igniter.coupons::default.text_cart_restriction_delivery_fee', + ], ], 'categories' => [ 'tab' => 'lang:igniter.coupons::default.text_tab_restrictions', @@ -369,8 +373,8 @@ 'comment' => 'lang:igniter.coupons::default.help_categories', 'trigger' => [ 'action' => 'show', - 'field' => 'is_limited_to_cart_item', - 'condition' => 'checked', + 'field' => 'apply_coupon_on', + 'condition' => 'value[menu_items]', ], ], 'menus' => [ @@ -381,8 +385,8 @@ 'nameFrom' => 'menu_name', 'trigger' => [ 'action' => 'show', - 'field' => 'is_limited_to_cart_item', - 'condition' => 'checked', + 'field' => 'apply_coupon_on', + 'condition' => 'value[menu_items]', ], ], 'history' => [