Internal overhaul of Cart/Checkout and other minor fixes

This commit is contained in:
Deon George
2013-12-02 15:16:28 +11:00
parent f8a5b153cf
commit 06b87c5135
26 changed files with 256 additions and 228 deletions

View File

@@ -16,10 +16,6 @@ class Cart {
$this->id = is_null($id) ? Session::instance()->id() : $id;
}
public static function instance($id=NULL) {
return new Cart($id);
}
/**
* Return a list of items in the cart
*/
@@ -29,6 +25,20 @@ class Cart {
->find_all();
}
public function checkout() {
$result = array();
$checkout = ORM::factory('Checkout')->where_active()->find_all()->as_array();
foreach ($this->contents() as $cio)
$checkout = array_intersect($checkout,$cio->checkout()->as_array());
foreach ($checkout as $cko)
$result[$cko->id] = $cko->name;
return $result;
}
public function delete() {
foreach (ORM::factory('Cart')->where('session_id','=',$this->id)->find_all() as $co)
$co->delete();
@@ -46,6 +56,10 @@ class Cart {
return $this->id;
}
public static function instance($id=NULL) {
return new Cart($id);
}
public function total($format=FALSE) {
$total = 0;
@@ -54,43 +68,5 @@ class Cart {
return $format ? Currency::display($total) : $total;
}
/**
* Print an HTML cart list
*
* @param bool $detail List a detailed cart or a summary cart
*/
public function cart_block() {
// @todo To implement.
return '';
// If the cart is empty, we'll return here.
if (! count($this->contents()))
return 'The cart is empty.';
Style::add(array(
'type'=>'file',
'data'=>'css/cart_blocklist.css',
));
$output = '<table class="cart_blocklist" border="0">';
foreach ($this->contents() as $item) {
$ppa = $item->product->get_price_array();
$pdata = Period::details($item->recurr_schedule,$item->product->price_recurr_weekday,time(),TRUE);
$output .= View::factory('cart/block_list')
->set('item',$item)
->set('price_setup',$item->quantity*$ppa[$item->recurr_schedule]['price_setup'])
->set('price_firstinvoice',$item->quantity*$ppa[$item->recurr_schedule]['price_base']*$pdata['prorata']);
}
$output .= '<tr class="submit">';
$output .= sprintf('<td colspan="3">%s&nbsp;%s</td>',
Form::button('checkout','Checkout',array('type' => 'submit')),
Form::button('empty','Empty',array('type' => 'submit')));
$output .= '</tr>';
$output .= '</table>';
return $output;
}
}
?>

View File

@@ -16,52 +16,21 @@ class Controller_Cart extends Controller_TemplateDefault {
* List the cart contents
*/
public function action_index() {
$output = '';
$co = Cart::instance();
// If the cart is empty, we'll return here.
if (! count($co->contents()))
Block::add(array(
'title'=>_('Empty Cart'),
'body'=>_('The cart is empty')
));
if (! $co->contents()->count())
$this->template->content = _('Cart is Empty');
else {
Block::add(array(
'title'=>_('Cart Items'),
'body'=>Table::display(
$co->contents(),
NULL,
array(
'item()->q'=>array('label'=>'Quantity'),
'item()->i'=>array('label'=>'Item'),
'item()->t'=>array('label'=>'Total','class'=>'right'),
),
array(
'type'=>'list',
)
),
));
$output = View::factory('cart/view')->set('o',$co);
$checkout = ORM::factory('Checkout')->where_active()->find_all()->as_array();
foreach ($co->contents() as $cio)
$checkout = array_intersect($checkout,$cio->checkout()->as_array());
$payopt = array();
foreach ($checkout as $cko)
$payopt[$cko->id] = $cko->name;
$output .= _('Total amount due for payment').' '.$co->total(TRUE);
$output .= Form::open('checkout/before');
$output .= Form::select('checkout_id',$payopt);
$output .= Form::submit('submit',_('Checkout'));
$output .= View::factory('cart/payment')->set('o',$co);
$output .= Form::close();
Block::add(array(
'title'=>_('Payment'),
'body'=>$output,
));
Block::factory()
->body($output);
}
}
@@ -69,24 +38,19 @@ class Controller_Cart extends Controller_TemplateDefault {
* Add an item to the cart
*/
public function action_add() {
$cart = ORM::factory('Cart');
$co = ORM::factory('Cart');
$cart->session_id = Session::instance()->id();
$co->values(Request::current()->post());
$co->session_id = Session::instance()->id();
if ($cart->values(Request::current()->post())->check())
$cart->save();
else
throw new Kohana_Exception('Unable to add to cart');
if (! $this->save($co))
throw HTTP_Exception::factory(501,_('There was a problem adding the item to the cart.'));
if ($cart->saved())
HTTP::redirect('cart/index');
else
throw new Kohana_Exception(_('There was a problem adding the item to the cart.'));
HTTP::redirect('cart/index');
}
public function action_empty() {
foreach (ORM::factory('Cart')->where('session_id','=',Session::instance()->id())->find_all() as $co)
$co->delete();
Cart::instance()->delete();
$this->template->content = _('Cart Emptied');
}

View File

@@ -10,13 +10,14 @@
* @license http://dev.osbill.net/license.html
*/
class Model_Cart extends ORM_OSB {
protected $_belongs_to = array(
'product'=>array(),
);
// Cart doesnt use the update column
protected $_updated_column = FALSE;
protected $_belongs_to = array(
'product'=>array(),
'module'=>array(),
);
protected $_serialize_column = array(
'module_data',
);
@@ -30,46 +31,26 @@ class Model_Cart extends ORM_OSB {
),
);
private $mo;
public function __construct($id = NULL) {
// Load our Model
parent::__construct($id);
// Autoload our Sub Items
if ($this->loaded())
$this->_load_sub_items();
return $this;
}
private function _load_sub_items() {
$this->mo = ORM::factory('Module',$this->module_id)->instance($this->module_item);
if (! $this->mo->loaded())
throw new Kohana_Exception('Item :item not loaded?',array(':item'=>$this->module_item));
}
public function checkout() {
if (! method_exists($this->mo,'checkout'))
if (! method_exists($this->mo(),'checkout'))
throw new Kohana_Exception('Module :module doesnt implement checkout?',array(':module'=>get_class($this->mo)));
return $this->mo->checkout();
return $this->mo()->checkout();
}
public function item() {
if (! method_exists($this->mo,'cart_item'))
if (! method_exists($this->mo(),'cart_item'))
throw new Kohana_Exception('Module :module doesnt implement cart_item?',array(':module'=>get_class($this->mo)));
return $this->mo->cart_item();
return $this->mo()->cart_item();
}
public function mo() {
return $this->mo;
return $this->module->instance($this->module_item);
}
public function motype() {
return strtolower(preg_replace('/^Model_/','',get_class($this->mo)));
return strtolower(preg_replace('/^Model_/','',get_class($this->mo())));
}
}
?>