PHP code example of mrnewport / laravel-stow

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

    

mrnewport / laravel-stow example snippets


return [
    'instances' => [
        'cart' => [Product::class, Item::class],
        // Additional instances...
    ],
];

$cart = new Basket('cart'); // restricted to Product and Item (see stow.php config)
$wishlist = new Basket(); // Unrestricted, any item implementing Stowable interface

class Product implements Stowable
{
    // ...
}

$product = new Product();
$cart->add($product, 1, ['size' => 'M', 'color' => 'blue']);

$item = new Item(); // Assume this class implements Stowable as well
$cart->add($item, 2);

// Service has not been explicitly allowed in the 'cart' instance (see stow.php config)
$service = new Service();
$cart->add($service); // Throws UnstowableObjectException

$wishlist->add($service); // instances without restrictions can accept any Stowable

$basketItem1 = $cart->add($product); // new product added because options don't match, quantity in cart is 1
$basketItem2->add($product, 3, ['size' => 'M', 'color' => 'blue']); // product with same options already in cart, quantity incremented and is now 3

$cart->change($basketItem, 3, ['size' => 'L']); // Update quantity and options

$cart->remove($basketItem);

$basketItems = $cart->basketItems->with('stowable')->get();

// group items by stowable type
$grouped = $basketItems->groupBy('stowable_type');

// filter items by stowable class
$filtered = $basketItems->filter(function($item) {
    return $item->stowable instanceof Product;
});

// iterate over items
foreach($basketItems as $basketItem) {
    $stowable = $basketItem->stowable;
    // ...
}

$wishlist->merge($cart);

$clone = $wishlist->clone();

$cart->delete();

namespace App\Listeners;

use MrNewport\LaravelStow\Events\BasketUpdatedEvent;

class YourCustomListener
{
    public function handle(BasketUpdatedEvent $event)
    {
        $basket = $event->basket; // Access the updated basket
        // Implement your custom logic here
    }
}

protected $listen = [
    'MrNewport\LaravelStow\Events\BasketUpdatedEvent' => [
        'App\Listeners\YourCustomListener',
    ],
];
bash
php artisan vendor:publish --provider="MrNewport\LaravelStow\StowProvider"
php artisan migrate
bash
php artisan make:listener YourCustomListener --event=BasketUpdatedEvent