PHP code example of boddasaad / laravel-discountable

1. Go to this page and download the library: Download boddasaad/laravel-discountable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

boddasaad / laravel-discountable example snippets


$student = Student::find(1);
// Check if the voucher is valid and can be applied (useful for UI validation before applying)
$student->checkVoucher('SUMMER2023', 100);

// Redeem the voucher, applying the discount
$student->redeemVoucher('SUMMER2023', 100);

return [
    /*
     * Database table name that will be used to store vouchers.
     */
    'table' => 'vouchers',

    /*
     * Database table name that will be used to store voucher usages.
     */
    'usage_table' => 'voucher_usages',

    /*
     * List of characters that will be used for voucher code generation.
     */
    'characters' => '23456789ABCDEFGHJKLMNPQRSTUVWXYZ',

    /*
     * Voucher code prefix.
     *
     * Example: foo
     * Generated Code: foo-AGXF-1NH8
     */
    'prefix' => null,

    /*
     * Voucher code suffix.
     *
     * Example: foo
     * Generated Code: AGXF-1NH8-foo
     */
    'suffix' => null,

    /*
     * Code mask.
     * All asterisks will be removed by random characters.
     */
    'mask' => '****-****',

    /*
     * Separator to be used between prefix, code and suffix.
     */
    'separator' => '-',
];

namespace App\Models;

use BoddaSaad\Voucher\Traits\CanRedeemVouchers;

class User extends Authenticatable
{
    use CanRedeemVouchers;
    # ...
}

use BoddaSaad\Voucher\Facades\Voucher;

$voucher = Voucher::maximumRedeems(1000)
        ->discount('percentage', 20)
        ->date('2025-01-01', '2025-12-31')
        ->minimumQualifyingAmount(50)
        ->maximumDiscountAmount(100)
        ->maxUsagesPerModel(1)
        ->data(['description' => 'Holiday Discount'])
        ->create();

auth()->user()->checkVoucher('SUMMER2023', 100);

[
    'valid' => true,
    'final_amount' => 80, // The amount after applying the voucher
    'voucher_id' => 1 // The ID of the voucher
]

[
    'valid' => false,
    'message' => 'Voucher has expired' // The reason why the voucher is not valid
]

auth()->user()->checkVoucher('SUMMER2023', 100);
// Will return true if the voucher was successfully redeemed, or false if it was not.