This package provides a framework and shop system agnostic implementation of a shopping cart.

1.0.0 2019-03-18 16:28 UTC

README

This package provides a framework and shop system agnostic implementation of a shopping cart.

Description

The Cart class can do nothing but hold a currency, products, vouchers and shippings.

What the Cart class by purpose can not do:

  • Calculating prices
  • Rejecting vouchers because due to certain constraints

Installation

composer require michaelkaefer/cart

Usage

Instantiation

// The default currency is Euro but all currencies of the package payum/iso4217 can be used
$usDollar = (new ISO4217())->findByAlpha3('USD');

$cart = new Cart($usDollar);

Your client code

// To use this package your client code must implement implement IdentifiableInterface
class YourProduct implements IdentifiableInterface
{
    // ...
    public function getId(): int
    {
        // ...
    }
}

class YourVoucher implements IdentifiableInterface
{
    // ...
    public function getId(): int
    {
        // ...
    }
}

class YourShipping implements IdentifiableInterface
{
    // ...
    public function getId(): int
    {
        // ...
    }
}

// Create some products with certain IDs
$bread = new YourProduct(1);
$butter = new YourProduct(2);

Add products to the cart

// Now you can specify cart lines which hold an item and a quantity for the item
$oneTimeTheBread = new Line($bread, 1);
$threeTimesTheButter = new Line($butter, 3);
$twoTimesTheBreadAgain = new Line($bread, 4);

// Now you can add a product ...
$cart->addProduct($oneTimeTheBread);

// ... or multiple products
$cart->addProduct([
    $threeTimesTheButter,
    $twoTimesTheBreadAgain,
]);

// Has the cart products?
$cart->hasProducts(); // true

// Now your cart looks like this
try {
    $bread = $cart->findProduct(1)->getItem();
    $quantity = $cart->findProduct(1)->getQuantity(); // is 5
} catch(\InvalidArgumentException $e) {
    // no bread found in the cart
}

try {
    $butter = $cart->findProduct(2)->getItem();
    $quantity = $cart->findProduct(2)->getQuantity(); // is 3
} catch(\InvalidArgumentException $e) {
    // no butter found in the cart
}

// Iterate
foreach ($cart->getProducts() as $productLine) {
    $item = $productLine->getItem();
    $quantity = $productLine->getQuantity();
}

// Remove butter from the cart
$cart->removeProduct(2);

Add vouchers

$specialOffer = new YourVoucher(1);

$cart->addVoucher($specialOffer);

$cart->hasVouchers(); // true

try {
    $specialOffer = $cart->findVoucher(1)->getItem();
    $quantity = $cart->findVoucher(1)->getQuantity();
} catch(\InvalidArgumentException $e) {
    // not found
}

$cart->removeVoucher(2);

Add shippings

$somePostSAervice = new YourShipping(1);

$cart->addShipping($somePostService);

$cart->hasShippings(); // true

try {
    $somePostService = $cart->findShipping(1)->getItem();
    $quantity = $cart->findShipping(1)->getQuantity();
} catch(\InvalidArgumentException $e) {
    // not found
}

$cart->removeShipping(2);