PHP code example of missionx-co / discounts-engine
1. Go to this page and download the library: Download missionx-co/discounts-engine 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/ */
use MissionX\DiscountsEngine\Enums\DiscountPriority;
(new Discount)
->priority(DiscountPriority::High)
(new Discount)->minPurchaseAmount(50)
(new Discount)->minQty(4)
(new Discount)->combineWithOtherDiscounts()
(new Discount)->forceCombineWithOtherDiscounts()
(new Discount)->setIsAutomatic()
use MissionX\DiscountsEngine\Enums\DiscountType;
// the following configuration will be applied to all applicable items
(new AmountOffProductDiscount)
->amount(10)
->limitToItems(
fn (array $items) => array_filter(
$items,
fn (Item $item) => $item->type == 'product'
)
)
// the following configuration will be applied to item with id=2 only
(new AmountOffProductDiscount)
->amount(10, DiscountType::FixedAmount)
->limitToItems(
fn (array $items) => array_filter(
$items,
fn (Item $item) => $item->type == 'product'
)
)
->minPurchaseAmount(200)
->selectAffectedItemsUsing(
fn (array $items) => array_filter(
$items,
fn (Item $item) => $item->id == 2
)
)
$discount = (new AmountOffOrderDiscount())->amount(20);
$discount2 = (new AmountOffOrderDiscount())->amount(10)->forceCombineWithOtherDiscounts();
$discount3 = (new AmountOffOrderDiscount())->amount(30)->combineWithOtherDiscounts();
// won't be applied because min purcahse amount is greater than the total amount
$discount4 = (new AmountOffOrderDiscount())->amount(20)->forceCombineWithOtherDiscounts()->minPurchaseAmount(300);
$group = (new DiscountsEngine)
->addDiscount($twentyPercentOff)
->addDiscount($twentyPercentOffLimited)
->addDiscount($amountOffProduct)
->process($this->items());
$group->savings(); // 82 (10% + 30%)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.