PHP code example of mathewberry / cart

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

    

mathewberry / cart example snippets


composer 

Cart` => \Mathewberry\Cart\Facades\Cart::class
 
$product = Cart::get($product_id);

// Example
[
    "id" => 24,
    "price" => 99.99,
    "quantity" => 2,
    "options" => [...]
]

{{ $product['options']['name'] }} <small>{{ $product['options']['model'] }}</small><br>
<img src="{{ $product['options']['image'] }}">
<a href="{{ route('products', ['id' => $product['id']]) }}"><br>

$products = Cart::content();

// Example
[
    [
        "id" => 24,
        "price" => 99.99,
        "quantity" => 2,
        "options" => [...]
    ],
    [
        "id" => 34,
        "price" => 79.99,
        "quantity" => 1,
        "options" => [...]
    ]
]

// Return the total with shipping or voucher
Cart::subtotal();

/* DATABASE ROW
 *
 * Id: 24
 * Name: 'My Product'
 * Model: 'AAA900'
 * Price: 99.99
 *
 */

$product = \App\Models\Product::select(['id', 'model', 'price'])->find(24);
$product_id = $product->id;
$price = $product->price;
$quantity = 1;

// You may add any options you wish.
$options = [
     'name' => $product->name,
     'image' => asset('images/my-product-image.jpg'),
     'model' => $product->model
];

Cart::add($product_id, $price, $quantity, $options)

// Returns the total of the cart
Cart::total();

// Returns the delivery cost
Cart::delivery();

// Example Data
$id = 24;
$code = "MB25OFF"; // 25% Off
$discount = 25.00; // 25%
$is_fixed = false; // Percentage
$display = '-' . number_format($discount, 2) . '%'; // How it should be displayed to the customer.

Cart::voucher($id, $code, $discount, $is_fixed, $display);

Cart::voucher();

[
    "id" => $voucher_id, // Voucher id
    "code" => $voucher_code, // Voucher code
    "display" => $voucher_display, // Customer friendly, e.g: "25%"
    "is_fixed" => $voucher_fixed, // Percentage voucher or fixed price
    "discount" => $voucher_discount_value // Value of voucher
]

Code: {{ Cart::voucher()['code'] }}<br>
Deducted: {{ Cart::voucher()['display'] }}

// Returns true or false whether or not the product exists in the cart.
Cart::has($product_id);

// Calculates the product price times quantity.
Cart::sum($product_id);

// Return a list of all the products in the cart.
Cart::products();

// Add one to the current quantity
Cart::update($product_id);

// Custom quantity to add to existing quantity
Cart::update($product_id, $quantity);

// Remove a specific product from the cart.
Cart::remove($product_id);

// Clear the cart of ALL of it's data.
Cart::clear();
config/app.php