route('POST /api/cart/add', '\Controller\Cart->add'); $f3->route('GET /api/cart/del', '\Controller\Cart->del'); $f3->route('GET /api/cart/more', '\Controller\Cart->more'); $f3->route('GET /api/cart/less', '\Controller\Cart->less'); $this->basket = new \Basket( 'cart' ); //$this->basket->drop(); } function index() { $f3 = \Base::instance(); $tmp = $this->basket->find(); $cart = []; foreach ($tmp as $i) { $cart[] = [ 'name' => $i->get('name'), 'price' => $i->get('price'), 'amount' => $i->get('amount') ]; } $cart_total = 0.0; foreach ($cart as $item) { $cart_total += $item['amount'] * $item['price']; } $f3->set('cart_meta',array_merge( $f3->get('cart_meta') ? : [], [ 'cart_total' => $cart_total ]) ); $f3->set('cart', $cart); $this->sanitize_cart(); } function sanitize_cart() { $f3 = \Base::instance(); } function add() { $f3 = \Base::instance(); $caller = $f3->get('POST.caller'); $name = $f3->get('POST.name'); $price = $f3->get('POST.price'); $amount = $f3->get('POST.amount') ? : 1; $previously_in_cart = $this->basket->find('name',$name); if (count($previously_in_cart) > 0) { // item already in cart // add to counter $this->basket->load('name',$name); $this->basket->set('amount',$this->basket->get('amount') + $amount); $this->basket->save(); $this->basket->reset(); } else { $this->basket->set('name',$name); $this->basket->set('price',$price); $this->basket->set('amount',$amount); $this->basket->save(); $this->basket->reset(); }; //echo $caller; $f3->reroute($caller); } function del($name = false) { $f3 = \Base::instance(); $caller = $f3->get('GET.caller'); $name = $name ? : urldecode($f3->get('GET.name')); $this->basket->erase('name', $name); $f3->reroute($caller); } function more() { $f3 = \Base::instance(); $caller = $f3->get('GET.caller'); $this->change_quantity(1); $f3->reroute($caller); } function less() { $f3 = \Base::instance(); $caller = $f3->get('GET.caller'); $this->change_quantity(-1); $f3->reroute($caller); } function change_quantity($amount = 0) { $f3 = \Base::instance(); $caller = $f3->get('GET.caller'); $name = urldecode($f3->get('GET.name')); $this->basket->load('name', $name); $new_amount = $this->basket->get('amount') + $amount; if ($new_amount > 0) { $this->basket->set('amount',$this->basket->get('amount') + $amount); } else { $this->del($name); } $this->basket->save(); $this->basket->reset(); } function checkout() { $t = \Template::instance(); return $t->render('checkout.htm'); } }