# Class: Cart

  • A user can add a record to his shopping cart (we call it basket)
  • Because every user has his own basket, we store the information in a session variable, called cart
  • Because we need different actions (add item to cart, remove item from cart, clear the cart, ...) on different places ( shop detail, basket overview, ...), we'll make a new Cart class that handles all these actions
  • We can call these actions inside a view, inside a controller, inside a Livewire component, ...

# What are sessions?

  • A session can be defined as a server-side storage of information that is desired to persist throughout the user's interaction with the web site or web application

# How are sessions stored?

  • There are different ways to store a session
  • Most of the time, we use file based sessions but we can also use database based sessions or redis based sessions
  • Take a look at the session configuration file config/session.php
    • The driver refers to the variable SESSION_DRIVER inside the .env file
    • The lifetime refers to the variable SESSION_LIFETIME inside the .env file
    • The expire_on_close is set to false, so the session is still active even if you close the browser
'driver' => env('SESSION_DRIVER', 'database'),
...
'lifetime' => env('SESSION_LIFETIME', 120),
'expire_on_close' => false,
1
2
3
4
  • Now, take a look at the session variables inside .env:
    • update the SESSION_DRIVER from database to file
    • SESSION_LIFETIME is set to 120 minutes, or 2 hours
 


SESSION_DRIVER=file     #  replace database with file
SESSION_LIFETIME=120
1
2
  • When you login to the site, you'll find one or more files with a random name inside the folder storage/framework/sessions
  • You can open a file to see what's inside a session

# Handle sessions

Action Laravel (helper) Laravel (facade) Pure PHP
set session()->put('key', 'val') Session::put('key', 'val') $_SESSION['key'] = 'val'
get session()->get('key') Session::get('key') $_SESSION['key']
remove key session()->forget('key') Session::forget('key') unset($_SESSION['key'])
remove keys session()->forget(['key1', 'key2']) Session::->forget(['key1', 'key2'])
remove all keys session()->flush() Session::flush() session_destroy()

# The Cart class

REMARK

  • In the next chapters, we'll use the Cart class to handle the shopping cart
  • To give you a better understanding what's going on, we'll explain the code by an example of what we want to achieve
  • Before we proceed to the Cart class itself, first take a look at the cart logic
  • Below you find an example of what's stored inside the session variable cart (in the next chapter, we'll actually get this result)
    • 2 times the record with $id = 11 ( 2 * 16.49 € = 32.98 €)
    • 1 time the record with $id = 15 ( 1 * 9.99 € = 9.99 €)
    • Total items inside your basket = 3
    • Total price for your basket = 32.98 € + 9.99 € = 42.97

Session cart

  • The cart is an associative array with three keys:
    • the key records contains an associative array where the key represents the record id and the value contains (an associative array with) all the fields we need in the orderlines table
    • The key totalQty contains the number of items in our cart
    • The key totalPrice contains the total price of our cart

REMARKS

  • The Card is build upon a static class witch has the advantages of:
    • no need to instantiate the class
    • static properties and methods can be accessed directly with the scope resolution operator (_::_)
  • See static class example in /php/classes

# Create the Cart class

  • Create a new PHP Class (not a new file) Cart.php inside the folder app/Helpers
    (In PhpStorm: right-click on the folder and choose New -> PHP Class)

# Initialize the $card property

  • Replace the content of the class with this code:
    • Line 10 - 14he private static property $cart is an associative array with the three keys: records , totalQty and totalPrice
      Because this is a private property, we can only access it from inside the class
    • Line 17 - 20: the public static method init() checks if the session variable cart exists
      • If the session variable cart exists, copy the session variable to the $cart variable
      • If not, use the default value of the $cart variable
    • Line 32: call the init() method to make sure the $cart variable is initialized
      REMARK: this is the only place where we call the init() method, so we can be sure the $cart variable is initialized
<?php

namespace App\Helpers;

use App\Models\Record;
use Storage;

class Cart
{
    private static array $cart = [
        'records' => [],
        'totalQty' => 0,
        'totalPrice' => 0
    ];

    // initialize the cart
    public static function init(): void
    {
        self::$cart = session()->get('cart') ?? self::$cart;
    }
}

Cart::init();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# Add/delete record to/from the cart and empty the cart

# Get data from the cart

  • With the six getters we can retrieve all the information or pieces of the information from the cart
    • We provide all possible combinations here, even if we may not need them (yet)

# Add an alias for the Cart class

  • We can add an alias to the Cart class in the config/app.php file
    • This way we can use the Cart class without the App\Helpers\ namespace
    • We can also use the Cart class in the routes/web.php file without the namespace
  • Open the config/app.php file:
    • Line 3: add the Card class to the aliases array at the bottom of the file


 


'aliases' => Facade::defaultAliases()->merge([
    // 'ExampleClass' => App\Example\ExampleClass::class,
    'Cart' => App\Helpers\Cart::class,
])->toArray(),
1
2
3
4
Last Updated: 4/20/2023, 6:19:50 PM