PHP code example of tefo / cart

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

    

tefo / cart example snippets


use Tefo\Cart\HasCart;
use Tefo\Cart\InteractsWithCard;

class Product extends Model implements HasCart
{
    use InteractsWithCard;
    // ...
}

Cart::addItem($product);

Cart::addItem($product, ['quantity' => 2], ['color' => 'red']);

Cart::add([$product_1, $product_2]);

Cart::add([$product_1, $product_2], [['quantity' => 1], ['quantity' => 2]], [$product_1_Variation, $product_2_Variation]);

$itemId = '92e1b081-17b9-4f7a-8f91-91da9dbcd6ca'; // its uuid

Cart::update($itemId, $updatedProduct); // Will update the id, name and price

Cart::update($itemId, ['name' => 'Product 1']); // Will update the name

$itemId = '92e1b081-17b9-4f7a-8f91-91da9dbcd6ca';

Cart::updateQuantity($itemId, 2); // Will update the quantity

$itemId = '92e1b081-17b9-4f7a-8f91-91da9dbcd6ca';

Cart::remove($itemId);

$itemId = '92e1b081-17b9-4f7a-8f91-91da9dbcd6ca';

Cart::get($itemId);

Cart::content();

Cart::session('wishlist')->content();

Cart::destroy();

Cart::total();

Cart::total($decimals, $decimalSeperator, $thousandSeperator);

Cart::tax();

Cart::tax($decimals, $decimalSeperator, $thousandSeperator);

Cart::subtotal();

Cart::subtotal($decimals, $decimalSeperator, $thousandSeperator);

Cart::count();

$cart->search(function ($cartItem, $itemId) {
	return $cartItem->id === 1;
});

Cart::content()->count();

Cart::content()->groupBy('id');

Cart::session('shopping')->addItem(['id' => 1, 'name' => 'Product 1', 'price' => 9.99], ['quantity' => 1]);

// Get the content of the 'shopping' cart
Cart::content();

Cart::session('wishlist')->addItem($product_2, ['quantity' => 2], ['size' => 'medium']);

// Get the content of the 'wishlist' cart
Cart::content();

// If you want to get the content of the 'shopping' cart again
Cart::session('shopping')->content();

// And the count of the 'wishlist' cart again
Cart::session('wishlist')->count();


// First we'll add the item to the cart.
$cartItem = Cart::addItem(['id' => 1, 'name' => 'Product 1', 'price' => 9.99], ['quantity' => 2], ['size' => 'large']);

// Next we associate a model with the item.
Cart::associate($cartItem->itemId, 'Product');

// Or even easier, call the associate method on the CartItem!
$cartItem->associate('Product');

// You can even make it a one-liner
Cart::addItem(['id' => 2, 'name' => 'Product 2', 'price' => 9.99], ['quantity' => 1], ['size' => 'large'])->associate('Product');

// Now, when iterating over the content of the cart, you can access the model.
foreach(Cart::content() as $row) {
	echo 'You have ' . $row->quantity . ' items of ' . $row->model->name . ' with description: "' . $row->model->description . '" in your cart.';
}