Permalink
Browse files

EShoppingCart updated to 0.9

- Added optional cartId property to EShoppingCart 
- Renamed onUpdatePoistion to onUpdatePosition (Issue 41)
- Fixed the bug when ECartPositionBehaviour is attached multiple times 
- Added ability to connect EShoppingCart by necessity

git-svn-id: http://yiiext.googlecode.com/svn/trunk/app/extensions/yiiext/components/shoppingCart@418 175f29da-e50b-11de-93f0-cd01e99e34ab
  • Loading branch information...
1 parent 3f32fd2 commit 6e7f4e30ce90cea7622ee66f3c67e1bd49f46945 @VladimirRakovich VladimirRakovich committed Aug 4, 2011
Showing with 53 additions and 17 deletions.
  1. +3 −3 ECartPositionBehaviour.php
  2. +10 −7 EShoppingCart.php
  3. +1 −1 IECartPosition.php
  4. +1 −1 IEDiscount.php
  5. +7 −0 changelog.txt
  6. +16 −2 readme_en.txt
  7. +15 −3 readme_ru.txt
@@ -4,7 +4,7 @@
* position in the cart
*
* @author pirrat <mrakobesov@gmail.com>
- * @version 0.8
+ * @version 0.9
* @package ShoppingCart
*
* Can be used with non-AR models.
@@ -35,7 +35,7 @@ class ECartPositionBehaviour extends CActiveRecordBehavior {
*
*/
public function getSumPrice($withDiscount = true) {
- $fullSum = $this->owner->getPrice() * $this->quantity;
+ $fullSum = $this->getOwner()->getPrice() * $this->quantity;
if($withDiscount)
$fullSum -= $this->discountPrice;
return $fullSum;
@@ -63,7 +63,7 @@ public function setQuantity($newVal) {
*/
public function __wakeup() {
if ($this->refresh === true)
- $this->owner->refresh();
+ $this->getOwner()->refresh();
}
/**
View
@@ -3,7 +3,7 @@
* Shopping cart class
*
* @author pirrat <mrakobesov@gmail.com>
- * @version 0.8
+ * @version 0.9
* @package ShoppingCart
*/
@@ -16,6 +16,8 @@ class EShoppingCart extends CMap {
public $refresh = true;
public $discounts = array();
+
+ public $cartId = __CLASS__;
/**
* Cart-wide discount sum
@@ -31,7 +33,7 @@ public function init(){
* Restores the shopping cart from the session
*/
public function restoreFromSession() {
- $data = Yii::app()->getUser()->getState(__CLASS__);
+ $data = unserialize(Yii::app()->getUser()->getState($this->cartId));
if (is_array($data) || $data instanceof Traversable)
foreach ($data as $key => $product)
parent::add($key, $product);
@@ -94,7 +96,8 @@ public function update(IECartPosition $position, $quantity) {
throw new InvalidArgumentException('invalid argument 1, product must implement CComponent interface');
$key = $position->getId();
-
+
+ $position->detachBehavior("CartPosition");
$position->attachBehavior("CartPosition", new ECartPositionBehaviour());
$position->setRefresh($this->refresh);
@@ -106,7 +109,7 @@ public function update(IECartPosition $position, $quantity) {
parent::add($key, $position);
$this->applyDiscounts();
- $this->onUpdatePoistion(new CEvent($this));
+ $this->onUpdatePosition(new CEvent($this));
$this->saveState();
}
@@ -115,7 +118,7 @@ public function update(IECartPosition $position, $quantity) {
* @return void
*/
protected function saveState() {
- Yii::app()->getUser()->setState(__CLASS__, $this->toArray());
+ Yii::app()->getUser()->setState($this->cartId, serialize($this->toArray()));
}
/**
@@ -165,8 +168,8 @@ public function onRemovePosition($event) {
* @param $event
* @return void
*/
- public function onUpdatePoistion($event) {
- $this->raiseEvent('onUpdatePoistion', $event);
+ public function onUpdatePosition($event) {
+ $this->raiseEvent('onUpdatePosition', $event);
}
/**
View
@@ -4,7 +4,7 @@
* IECartPosition
*
* @author pirrat <mrakobesov@gmail.com>
- * @version 0.8
+ * @version 0.9
* @package ShoppingCart
*/
interface IECartPosition {
View
@@ -3,7 +3,7 @@
* Discount abstract class
*
* @author pirrat <mrakobesov@gmail.com>
- * @version 0.8
+ * @version 0.9
* @package ShoppingCart
*
*/
View
@@ -1,3 +1,10 @@
+0.9
+---
+- Added optional cartId property to EShoppingCart (rak)
+- Renamed onUpdatePoistion to onUpdatePosition (rak)
+- Fixed the bug when ECartPositionBehaviour is attached multiple times (rak)
+- Added ability to connect EShoppingCart by necessity (rak)
+
0.8
---
- Added ECartPositionBehavior::setDiscountPrice (Sam Dark)
View
@@ -9,19 +9,33 @@ It uses user session as a cart data storage.
Installing and configuring
--------------------------
+### 1 way: Registration in the config file
Add to `protected/config/main.php`:
~~~
[php]
'import'=>array(
'ext.yiiext.components.shoppingCart.*'
),
+
'components' => array(
'shoppingCart' =>
array(
'class' => 'ext.yiiext.components.shoppingCart.EShoppingCart',
),
)
~~~
+### 2 way: Registration by necessity
+~~~
+[php]
+$cart = Yii::createComponent(array(
+ 'class' => 'ext.yiiext.components.shoppingCart.EShoppingCart'
+));
+//Important!
+$cart->init();
+
+$book = Book::model()->findByPk(1);
+$cart->put($book);
+~~~
Preparing a model
-----------------
@@ -251,12 +265,12 @@ Usage:
~~~
[php]
$cN = new CallCenterNotifier();
-Yii::app()->shoppingCart->attachEventHandler('onUpdatePoistion',array($cN, 'updatePositionInShoppingCart'));
+Yii::app()->shoppingCart->attachEventHandler('onUpdatePosition',array($cN, 'updatePositionInShoppingCart'));
$book = Book::model()->findByPk(1);
Yii::app()->shoppingCart->put($book);
~~~
-When onUpdatePoistion event is fired, call center will be notified.
+When onUpdatePosition event is fired, call center will be notified.
Working with a cart as CMap
---------------------------
View
@@ -8,7 +8,7 @@ Shopping Cart
Установка и настройка
---------------------
-
+### 1 вариант: Подключение через конфиг
В `protected/config/main.php` добавить:
~~~
[php]
@@ -23,6 +23,18 @@ Shopping Cart
),
)
~~~
+### 2 вариант: Подключение по необходимости
+~~~
+[php]
+$cart = Yii::createComponent(array(
+ 'class' => 'ext.yiiext.components.shoppingCart.EShoppingCart'
+));
+//Важно
+$cart->init();
+
+$book = Book::model()->findByPk(1);
+$cart->put($book);
+~~~
Подготавливаем модель
---------------------
@@ -256,12 +268,12 @@ class TestDiscount extends IEDiscount {
~~~
[php]
$cN = new CallCenterNotifier();
-Yii::app()->shoppingCart->attachEventHandler('onUpdatePoistion',array($cN, 'updatePositionInShoppingCart'));
+Yii::app()->shoppingCart->attachEventHandler('onUpdatePosition',array($cN, 'updatePositionInShoppingCart'));
$book = Book::model()->findByPk(1);
Yii::app()->shoppingCart->put($book);
~~~
-При срабатывание события onUpdatePoistion, об этом будет оповещен call center.
+При срабатывание события onUpdatePosition, об этом будет оповещен call center.
CMap - работаем с корзиной, как с массивом.
-------------------------------------------

0 comments on commit 6e7f4e3

Please sign in to comment.