Skip to content

Commit

Permalink
Feature delivery coupon (#38)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Fix code style

Signed-off-by: Sam Poyigi <[email protected]>

---------

Signed-off-by: Sam Poyigi <[email protected]>
Co-authored-by: Sam Poyigi <[email protected]>
  • Loading branch information
obinnaelviso and sampoyigi authored Oct 30, 2023
1 parent 8f7199c commit 1ea6ae8
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 18 deletions.
2 changes: 0 additions & 2 deletions actions/RedeemsCoupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 27 additions & 8 deletions cartconditions/Coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -86,16 +85,18 @@ 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;
}

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);
}

Expand Down Expand Up @@ -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();
Expand All @@ -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))
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Igniter\Coupons\Database\Migrations;

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class ChangeIsLimitedToCartItemToApplyCouponOnEnum extends Migration
{
public function up()
{
Schema::table('igniter_coupons', function (Blueprint $table) {
$table->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',
]);
}
);
}
}
5 changes: 4 additions & 1 deletion language/en/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',

Expand All @@ -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',
Expand Down
18 changes: 11 additions & 7 deletions models/config/coupons_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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' => [
Expand All @@ -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' => [
Expand Down

0 comments on commit 1ea6ae8

Please sign in to comment.