PHP code example of ozdemir / aurora

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

    

ozdemir / aurora example snippets


// config/cart.php

use Ozdemir\Aurora\Cart;
use Ozdemir\Aurora\Generators\GenerateChecksum;
use Ozdemir\Aurora\Generators\GenerateSessionKey;
use Ozdemir\Aurora\Storages\SessionStorage;

return [
    'instance' => 'cart',

    'cart_class' => Cart::class,

    'storage' => SessionStorage::class,

    'cache_store' => env('CART_STORE', config('cache.default')),

    'monetary' => [
        'precision' => env('CART_CURRENCY_PRECISION', 2),
    ],

    'session_key_generator' => GenerateSessionKey::class,

    'checksum_generator' => GenerateChecksum::class,

    'calculate_using' => [
        // Custom calculators go here
    ],
];

// Create a product class implementing the Sellable interface
class SellableProduct implements Sellable
{
    use SellableTrait;
}
// Adding an item to the cart
$product = new SellableProduct(); // Replace with your actual product model
$cartItem = new CartItem($product, quantity: 2);
Cart::add($cartItem);

// Retrieving cart information
$total = Cart::total();
$itemCount = Cart::count();
$items = Cart::items();
$itemQuantity = Cart::quantity(); // total quantity

// Adding an item with options to the cart
Cart::add(
       (new CartItem($product, quantity: 1))->withOption('color', 'blue')
                 ->withOption('material', 'metal', price: 5)
                 ->withOption('size', 'large', weight: 4)
         );

// Updating item quantity
Cart::update($cartItem->hash(), quantity: 3);

// Removing an item from the cart
Cart::remove($cartItem->hash());

return [
    // ...
    'calculate_using' => [
        // discounts etc..
        ShippingCalculator::class
        TaxCalculator::class
    ],
];

class ShippingCalculator
{
    public function handle($payload, Closure $next)
    {
        [$price, $breakdowns] = $payload;
        
        $shippingPrice = Shipping::first()->amount;

        $price = $price + $shippingPrice;

        $breakdowns[] = [
            'type' => 'shipping',
            'amount' => $shippingPrice,
            // any additional values..
        ];

        return $next([$price, $breakdowns]);
    }
}

class TaxCalculator
{
    public function handle($payload, Closure $next)
    {
        [$price, $breakdowns] = $payload;
        
        $taxPrice = Tax::first()->amount;

        $price = $price + $taxPrice;

        $breakdowns[] = [
            'type' => 'tax',
            'amount' => $taxPrice,
            // any additional values..
        ];

        return $next([$price, $breakdowns]);
    }
}

$breakdowns = Cart::breakdowns();

// Output the breakdowns
print_r($breakdowns);

Array (
    [0] => Array (
        [label] => Shipping
        [value] => 10
    )
    [1] => Array (
        [label] => Tax
        [value] => 15
    )
    // Additional breakdown items based on your custom calculators
    // ...
)
bash
php artisan vendor:publish --tag="aurora-config"