Giter Club home page Giter Club logo

kirby-cart's Introduction

Kirby Cart

Create session related pages. This repository is not meant to be a full blown cart solution. It only comes with a simple helper class. Therefore you need to add routes and/or controller logic by yourself. Use the cart() helper function to get the cart instance.

Options

Available options and their default values.

c::set('cart.root', 'carts');        // page containing carts
c::set('cart.blueprint', 'cart');    // cart blueprint
c::set('cart.session.timeout', 120); // session timeout
c::set('cart.session.lifetime', 0);  // session lifetime

Exemplary routes

$kirby->set('route', array(
  'pattern' => 'cart/add/(:all)',
  'method' => 'POST',
  'action' => 'LukasKleinschmidt\CartController::add'
));

$kirby->set('route', array(
  'pattern' => array(
    'cart/increment/(:all)/(:num)',
    'cart/increment/(:all)',
  ),
  'method' => 'POST',
  'action' => 'LukasKleinschmidt\CartController::increment'
));

$kirby->set('route', array(
  'pattern' => array(
    'cart/decrement/(:all)/(:num)',
    'cart/decrement/(:all)',
  ),
  'method' => 'POST',
  'action' => 'LukasKleinschmidt\CartController::decrement'
));

$kirby->set('route', array(
  'pattern' => 'cart/update/(:all)/(:num)',
  'method' => 'POST',
  'action' => 'LukasKleinschmidt\CartController::update'
));

$kirby->set('route', array(
  'pattern' => 'cart/delete/(:all)',
  'method' => 'POST',
  'action' => 'LukasKleinschmidt\CartController::delete'
));

$kirby->set('route', array(
  'pattern' => 'cart.json',
  'method' => 'POST',
  'action' => 'LukasKleinschmidt\CartController::json'
));

Exemplary controller

namespace LukasKleinschmidt;

use V;
use R;
use Response;

class CartController {

  public static function add($uri) {

    $cart = cart();
    $page = page($uri);

    if(!$page) {
      return static::error();
    }

    if($cart->find($page->hash())) {
      static::increment($page->hash());
    } else {
      $items = $cart->children();
      $items->create($page->hash(), 'item', array(
        'quantity' => 1,
        'page' => $page->uri(),
      ))->sort($items->count());
    }

    if(r::ajax()) {
      return static::json();
    }

    static::redirect();

  }

  public static function delete($uid) {

    $item = cart()->find($uid);

    if(!$item) {
      return static::error();
    }

    $item->delete(true);

    static::sort();

    if(r::ajax()) {
      return static::json();
    }

    static::redirect();

  }

  public static function update($uid, $quantity) {

    $item = cart()->find($uid);

    if(!$item || !v::integer($quantity)) {
      return static::error();
    }

    if($quantity == 0) {
      return static::delete($item->hash());
    } else {
      $item->update(compact($quantity));
    }

    if(r::ajax()) {
      return static::json();
    }

    static::redirect();

  }

  public static function increment($uid, $by = 1) {

    $item = cart()->find($uid);

    if(!$item || !v::integer($by)) {
      return static::error();
    }

    $item->increment('quantity', $by);

    if(r::ajax()) {
      return static::json();
    }

    static::redirect();

  }

  public static function decrement($uid, $by = 1) {

    $item = cart()->find($uid);

    if(!$item || !v::integer($by)) {
      return static::error();
    }

    if($item->quantity()->value() - $by <= 0) {
      return static::delete($uid);
    } else {
      $item->decrement('quantity', $by);
    }

    if(r::ajax()) {
      return static::json();
    }

    static::redirect();

  }

  public static function json() {
    $cart  = cart();
    $page  = $cart->page();
    $items = array();

    foreach($cart->children() as $item) {
      $items[$item->hash()] = array(
        'modified' => $item->modified(),
        'quantity' => (int) $item->quantity()->value(),
        'total' => $item->total(),
      );
    }

    $data = array(
      'modified' => $item->modified(),
      'quantity' => $page->quantity(),
      'total' => $page->total(),
      'items' => $items,
    );

    return response::json($data);
  }

  public static function sort() {

    $num = 0;

    foreach(cart()->children() as $item) {
      $num++;
      $item->sort($num);
    }

  }

  public static function redirect() {
    if($url = get('_redirect')) go($url);
  }

  public static function error() {
    return site()->visit('error');
  }

}

kirby-cart's People

Contributors

lukaskleinschmidt avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

kirby-cart's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.