PHP code example of aiarmada / cart

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

    

aiarmada / cart example snippets


use AIArmada\Cart\Facades\Cart;

// Add items
Cart::add('laptop-001', 'MacBook Pro 16"', 249900, 1, [
    'sku' => 'MBP16-2024',
    'color' => 'Space Gray',
]);

// Get totals
echo Cart::count();           // Total quantity
echo Cart::total()->format(); // "$2,499.00"

// Apply conditions
Cart::addDiscount('promo', '10%');
Cart::addTax('vat', '8%');
Cart::addShipping('standard', '15.00');

// Work with items
$item = Cart::get('laptop-001');
Cart::update('laptop-001', ['quantity' => 2]);
Cart::remove('laptop-001');

// Multiple instances
Cart::setInstance('wishlist')->add('monitor-001', 'Display', 59900);

use AIArmada\Cart\Storage\StorageInterface;

$this->app->bind(StorageInterface::class, function ($app): StorageInterface {
    return new App\Cart\Storage\CustomStorage(...);
});

use AIArmada\Cart\Conditions\CartCondition;
use AIArmada\Cart\Conditions\Enums\ConditionPhase;
use AIArmada\Cart\Conditions\Target;

// Percentage discount
Cart::addDiscount('summer-sale', '20%');

// Fixed amount
Cart::addDiscount('welcome', '-10.00');

// Custom condition
$condition = new CartCondition(
    name: 'vip-discount',
    type: 'discount',
    target: Target::cart()->phase(ConditionPhase::CART_SUBTOTAL)->applyAggregate()->build(),
    value: '-15%',
    rules: [fn($cart) => auth()->user()?->isVip()],
);
Cart::addCondition($condition);

use AIArmada\Chip\Gateways\ChipGateway;

$gateway = app(ChipGateway::class);
$cart = app(\AIArmada\Cart\Cart::class);

$payment = $gateway->createPayment($cart, $customer, [
    'success_url' => route('checkout.success'),
    'failure_url' => route('checkout.failed'),
]);

return redirect($payment->getCheckoutUrl());

// config/cart.php
'migration' => [
    'auto_migrate_on_login' => true,
    'merge_strategy' => 'add_quantities', // or keep_highest_quantity, keep_user_cart
],

use AIArmada\Cart\Events\ItemAdded;

Event::listen(ItemAdded::class, function ($event) {
    Log::info('Item added', ['item' => $event->item->id]);
});
bash
php artisan vendor:publish --tag=cart-config
bash
php artisan vendor:publish --tag=cart-migrations
php artisan migrate