запилил сегодня основу модуля корзины, кому надо могу сырую версию отдать, а через пару дней залью на гитхаб
<?php defined('SYSPATH') or die('No direct script access.');
/**
*
* Shopping cart
*
* @package Kohana/Cart
* @author WinterSilence
*
* $product = array(
* 'id' => 123, //or use sku '123ABC'
* 'qty' => 2,
* 'price' => 39.95,
* 'name' => 'T-Shirt',
* 'options' => array(
* array'id' => 134, 'name' => 'Size', 'value' => 'XL'),
* array'id' => 34, 'name' => 'Color', 'value' => 'Red'),
* ),
* );
*
* id - Each product in your store must have a unique identifier. Typically this will be an "sku" or other such identifier.
* id - Each product in your store must have a unique identifier. Typically this will be an "sku" or other such identifier.
* qty - The quantity being purchased.
* price - The price of the item.
* name - The name of the item.
* options - Any additional attributes that are needed to identify the product. These must be passed via an array.
*
* Example:
* $cart = Cart::instance();
* $cart->set($id, $gty, $options);
* $product = $cart->get($id, $options);
* $all_products = $cart->get(); or use $cart->products
* $cart->delete($id);
* $cart->delete(); // delete all
* $name_first_product = $cart->products[0]['name'];
* $total_cost = $cart->total['cost'];
* $total_count = $cart->total['count'];
*
*/
class Kohana_Cart
{
// Object instance
private static $_instance;
// Configuration
protected $_config = array();
// Session object
protected $_session;
// ORM product model
protected $_model_product;
// Cart items and total cost & count
protected $_content = array();
// Private clone method
private function __clone(){}
// Private wakeup method
private function __wakeup(){}
// Protect construct method
private function __construct($config = 'default')
{
// Load configuration
$this->_config = Kohana::$config->load('cart')->get($config);
// Load Session object
$this->_session = Session::instance($this->_config['session_type']);
// Grab the shopping cart array from the session table, if it exists
if ( ! $this->_content = $this->_session->get($this->_config['session_key'], FALSE))
{
// No cart exists so we'll set some base values
$this->_content = array(
'products' => array(),
'total' => array('cost' => 0, 'count' => 0),
);
}
}
// Get instance of class
public static function instance($config = NULL)
{
if (is_null(self::$_instance) OR ! empty($config))
{
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Magic Get to config & content
*
* $cart->config->template_short
* $cart->content->products
*/
public function __get($key)
{
if (array_search($key, array('config', 'content')) !== FALSE)
{
return $this->{'_'.$key};
}
}
// Save cart content
protected function _save()
{
$this->_session->set(
$this->_config['session_key'],
$this->_content,
$this->_config['session_lifetime']
);
}
// Generate complete id
protected function _get_id($id, array $options = array())
{
foreach ($options as $key => $value)
{
$id .= '_'.$key.':'.$value;
}
return $id;
}
// Set new total values
protected function _set_total()
{
$this->_content['total'] = array('cost' => 0, 'count' => 0);
foreach ($this->_content['products'] as $product)
{
$this->_content['total']['cost'] += $product['price'] * $product['qty'];
$this->_content['total']['count'] += $product['qty'];
}
}
/**
* Get product with options from database
* TODO: add options
*/
protected function _get_product($id, array $options = array())
{
// Create model only when it's needed
if ( ! is_object($this->_model_product))
{
$this->_model_product = ORM::factory($this->_config['model_product']);
}
$this->_model_product->select(
array($this->_config['model_columns']['id'], 'id'),
array($this->_config['model_columns']['name'], 'name'),
array($this->_config['model_columns']['price'], 'price'),
array($this->_config['model_columns']['gty'], 'gty')
)
->where($this->_config['model_columns']['id'], '=', $id)
->find();
if ($this->_model_product->loaded())
{
return Arr::extract($this->_model_product->as_array(), array_keys($this->_config['model_columns']));
}
else
{
throw new Kohana_Exception('Product with id :id not found', array(':id' => HTML::chars($id)));
//ORM_Validation_Exception
}
}
// Set product(s) from cart content
public function set($id, $qty = 1, array $options = array())
{
$qty = max(1, (int)$qty);
$cart_id = $this->_get_id($id, $options);
$product = $this->_get_product($id, $options);
if ($product['qty'] >= $qty)
{
if (isset($this->_content['products'][$cart_id]))
{
if ($product['qty'] >= ($this->_content['products'][$cart_id]['qty'] + $qty))
{
$this->_content['products'][$cart_id]['qty'] += $qty;
}
else
{
throw new Kohana_Exception('In stock only :qty items', array(':qty' => $product['qty']));
}
}
else
{
$product['qty'] = $qty;
$this->_content['products'][$cart_id] = $product;
}
}
else
{
throw new Kohana_Exception('In stock only :qty items', array(':qty' => $product['qty']));
}
// Save changes
$this->_set_total();
$this->_save();
return $this;
}
// Get product(s) from cart content
public function get($id = NULL, array $options = array())
{
if (empty($id))
{
// Get all
return $this->_content['products'];
}
else
{
// Get one
$id = $this->_get_id($id, $options);
return $this->_content['products'][$id];
}
}
// Delete products
public function delete($id = NULL, array $options = array())
{
if (empty($id))
{
// Delete all
$this->_content = array(
'products' => array(),
'total' => array('cost' => 0, 'count' => 0),
);
}
else
{
// Delete item
$id = $this->_get_id($id, $options);
unset($this->_content['products'][$id]);
$this->_set_total();
}
// Save changes
$this->_save();
return $this;
}
}