PHP code example of banulakwin / laravel-pricing

1. Go to this page and download the library: Download banulakwin/laravel-pricing 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/ */

    

banulakwin / laravel-pricing example snippets


use Banulakwin\Pricing\Facades\Pricing;
use Banulakwin\Pricing\Rules\CouponRule;
use Banulakwin\Pricing\Rules\ShippingRule;
use Banulakwin\Pricing\Rules\TaxRule;

$engine = Pricing::engine();
// or: pricing()->engine();

$engine
    ->addRule(new CouponRule(['type' => 'percent', 'value' => 10, 'items' => [], 'users' => []]))
    ->addRule(new TaxRule)
    ->addRule(new ShippingRule);

$context = $engine->calculate($items, ['user_id' => auth()->id()]);

echo $context->subtotal;   // 250.00
echo $context->discount;   // 25.00
echo $context->tax;        // 22.50
echo $context->shipping;   // 10.00
echo $context->total;      // 257.50

$items = [
    ['price' => 100, 'qty' => 2, 'type' => 'product', 'id' => 1],
    ['price' => 50, 'qty' => 1, 'type' => 'product', 'id' => 2],
];

$rules = config('pricing.rules', []);
$engine = pricing()->engine();

if ($rules['coupon'] ?? false) {
    $engine->addRule(new CouponRule($coupon, ['couponable_type' => 'type', 'couponable_id' => 'id']));
}
if ($rules['tax'] ?? false) {
    $engine->addRule(new TaxRule);
}
if ($rules['shipping'] ?? false) {
    $engine->addRule(new ShippingRule);
}

$result = $engine->calculate($items, ['user_id' => auth()->id()]);
bash
php artisan vendor:publish --tag=pricing-config
bash
php artisan vendor:publish --tag=pricing-migrations
php artisan migrate

src/
  PricingServiceProvider.php
  helpers.php
  Contracts/
    PriceRule.php
    RuleValidator.php
  Engine/
    PriceContext.php
    PricingEngine.php
  Facades/
    Pricing.php
  Managers/
    PricingManager.php
  Models/
    Coupon.php
    CouponItem.php
    CouponUser.php
  Rules/
    CouponRule.php
    ShippingRule.php
    TaxRule.php
config/
  pricing.php
database/
  migrations/
    2026_05_11_000000_create_coupons_table.php
    2026_05_11_000001_create_coupon_items_table.php
    2026_05_11_000002_create_coupon_users_table.php