PHP code example of slim-gee / dbcart-laravel

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

    

slim-gee / dbcart-laravel example snippets


$cart = app('cart'); //using app() helper

$cart = App::make('cart');

use NabeelAlalmai\DBcart\Models\Cart;
//...
$cart = Cart::current();

$cart->addItem([
    'product_id' => 1,
    'unit_price' => 10.5,
    'quantity' => 1
]); 

$items = $cart->items // by dynamic property access
$items = $cart->items()->get()  
$items = $cart->items()->where('quantity', '>=', 2)->get()
  
$cart->updateItem([
    'id' => 2
], [
    'product_id' => 1,
    'unit_price' => 10.5,
    'quantity' => 1
]); 

$cart->removeItem([
    'id' => 2
]); 

$cart->clear(); 
    
$cart->checkout();


$cart = app('cart');
$wishlist = app('cart', ['name' => 'wishlist']);

//move all wishlist items to cart
$wishlist->moveItemsTo($cart);

$cart = app('cart');
$wishlist = app('cart', ['name' => 'wishlist']);

//move an wishlist item to cart
$item = $wishlist->items()->where(['product_id' => 1])->first();
$item->moveTo($cart);


$total_price = $cart->total_price; // cart total price
$item_count = $cart->item_count; // cart items count
$date_placed = $cart->placed_at; // returns Carbon instance

use NabeelAlalmai\DBcart\Models\Cart;

// get carts based on their status: active/expired/pending/complete
$active_carts = Cart::active()->get();
$expired_carts = Cart::expired()->get();
$pending_carts = Cart::pending()->get();
$completed_carts = Cart::completed()->get();

$cart = app('cart'); // default cart, same as: app('cart', [ 'name' => 'default'];
$sales_cart = app('cart', [ 'name' => 'sales']);
$wishlist = app('cart', [ 'name' => 'wishlist']);

use NabeelAlalmai\DBcart\Models\Cart;
//...
$cart = Cart::current();
$sales_cart =  Cart::current('sales');
$wishlist =  Cart::current('wishlist');

$pending_sales_carts = Cart::instance('sales')->pending()->get();
    
    namespace App;
        
    use NabeelAlalmai\DBcart\Models\Cart as BaseCart;

    class Cart extends BaseCart
    {
        //override or add your methods here ...

        public function getSubTotalAttribute(){
            return $this->attributes['total_price'];
        }
        public function getGrandTotalAttribute(){
            //taking discount, tax etc. into account            
            return $this->sub_total - $this->discount;
        }
        
    }
    

    'cart_model' => App\Cart::class,
    

    $cart = App::make('cart');
    
bash
    php artisan vendor:publish --provider="NabeelAlalmai\DBcart\CartServiceProvider" --tag="migrations"
    
bash
    php artisan migrate
    
sh
php artisan vendor:publish --provider="NabeelAlalmai\DBcart\CartServiceProvider" --tag=config