Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit 6e7f4e30ce90cea7622ee66f3c67e1bd49f46945 1 parent 3f32fd2
Vladimir rakkture authored
6 ECartPositionBehaviour.php
@@ -4,7 +4,7 @@
4 4 * position in the cart
5 5 *
6 6 * @author pirrat <mrakobesov@gmail.com>
7   - * @version 0.8
  7 + * @version 0.9
8 8 * @package ShoppingCart
9 9 *
10 10 * Can be used with non-AR models.
@@ -35,7 +35,7 @@ class ECartPositionBehaviour extends CActiveRecordBehavior {
35 35 *
36 36 */
37 37 public function getSumPrice($withDiscount = true) {
38   - $fullSum = $this->owner->getPrice() * $this->quantity;
  38 + $fullSum = $this->getOwner()->getPrice() * $this->quantity;
39 39 if($withDiscount)
40 40 $fullSum -= $this->discountPrice;
41 41 return $fullSum;
@@ -63,7 +63,7 @@ public function setQuantity($newVal) {
63 63 */
64 64 public function __wakeup() {
65 65 if ($this->refresh === true)
66   - $this->owner->refresh();
  66 + $this->getOwner()->refresh();
67 67 }
68 68
69 69 /**
17 EShoppingCart.php
@@ -3,7 +3,7 @@
3 3 * Shopping cart class
4 4 *
5 5 * @author pirrat <mrakobesov@gmail.com>
6   - * @version 0.8
  6 + * @version 0.9
7 7 * @package ShoppingCart
8 8 */
9 9
@@ -16,6 +16,8 @@ class EShoppingCart extends CMap {
16 16 public $refresh = true;
17 17
18 18 public $discounts = array();
  19 +
  20 + public $cartId = __CLASS__;
19 21
20 22 /**
21 23 * Cart-wide discount sum
@@ -31,7 +33,7 @@ public function init(){
31 33 * Restores the shopping cart from the session
32 34 */
33 35 public function restoreFromSession() {
34   - $data = Yii::app()->getUser()->getState(__CLASS__);
  36 + $data = unserialize(Yii::app()->getUser()->getState($this->cartId));
35 37 if (is_array($data) || $data instanceof Traversable)
36 38 foreach ($data as $key => $product)
37 39 parent::add($key, $product);
@@ -94,7 +96,8 @@ public function update(IECartPosition $position, $quantity) {
94 96 throw new InvalidArgumentException('invalid argument 1, product must implement CComponent interface');
95 97
96 98 $key = $position->getId();
97   -
  99 +
  100 + $position->detachBehavior("CartPosition");
98 101 $position->attachBehavior("CartPosition", new ECartPositionBehaviour());
99 102 $position->setRefresh($this->refresh);
100 103
@@ -106,7 +109,7 @@ public function update(IECartPosition $position, $quantity) {
106 109 parent::add($key, $position);
107 110
108 111 $this->applyDiscounts();
109   - $this->onUpdatePoistion(new CEvent($this));
  112 + $this->onUpdatePosition(new CEvent($this));
110 113 $this->saveState();
111 114 }
112 115
@@ -115,7 +118,7 @@ public function update(IECartPosition $position, $quantity) {
115 118 * @return void
116 119 */
117 120 protected function saveState() {
118   - Yii::app()->getUser()->setState(__CLASS__, $this->toArray());
  121 + Yii::app()->getUser()->setState($this->cartId, serialize($this->toArray()));
119 122 }
120 123
121 124 /**
@@ -165,8 +168,8 @@ public function onRemovePosition($event) {
165 168 * @param $event
166 169 * @return void
167 170 */
168   - public function onUpdatePoistion($event) {
169   - $this->raiseEvent('onUpdatePoistion', $event);
  171 + public function onUpdatePosition($event) {
  172 + $this->raiseEvent('onUpdatePosition', $event);
170 173 }
171 174
172 175 /**
2  IECartPosition.php
@@ -4,7 +4,7 @@
4 4 * IECartPosition
5 5 *
6 6 * @author pirrat <mrakobesov@gmail.com>
7   - * @version 0.8
  7 + * @version 0.9
8 8 * @package ShoppingCart
9 9 */
10 10 interface IECartPosition {
2  IEDiscount.php
@@ -3,7 +3,7 @@
3 3 * Discount abstract class
4 4 *
5 5 * @author pirrat <mrakobesov@gmail.com>
6   - * @version 0.8
  6 + * @version 0.9
7 7 * @package ShoppingCart
8 8 *
9 9 */
7 changelog.txt
... ... @@ -1,3 +1,10 @@
  1 +0.9
  2 +---
  3 +- Added optional cartId property to EShoppingCart (rak)
  4 +- Renamed onUpdatePoistion to onUpdatePosition (rak)
  5 +- Fixed the bug when ECartPositionBehaviour is attached multiple times (rak)
  6 +- Added ability to connect EShoppingCart by necessity (rak)
  7 +
1 8 0.8
2 9 ---
3 10 - Added ECartPositionBehavior::setDiscountPrice (Sam Dark)
18 readme_en.txt
@@ -9,12 +9,14 @@ It uses user session as a cart data storage.
9 9 Installing and configuring
10 10 --------------------------
11 11
  12 +### 1 way: Registration in the config file
12 13 Add to `protected/config/main.php`:
13 14 ~~~
14 15 [php]
15 16 'import'=>array(
16 17 'ext.yiiext.components.shoppingCart.*'
17 18 ),
  19 +
18 20 'components' => array(
19 21 'shoppingCart' =>
20 22 array(
@@ -22,6 +24,18 @@ Add to `protected/config/main.php`:
22 24 ),
23 25 )
24 26 ~~~
  27 +### 2 way: Registration by necessity
  28 +~~~
  29 +[php]
  30 +$cart = Yii::createComponent(array(
  31 + 'class' => 'ext.yiiext.components.shoppingCart.EShoppingCart'
  32 +));
  33 +//Important!
  34 +$cart->init();
  35 +
  36 +$book = Book::model()->findByPk(1);
  37 +$cart->put($book);
  38 +~~~
25 39
26 40 Preparing a model
27 41 -----------------
@@ -251,12 +265,12 @@ Usage:
251 265 ~~~
252 266 [php]
253 267 $cN = new CallCenterNotifier();
254   -Yii::app()->shoppingCart->attachEventHandler('onUpdatePoistion',array($cN, 'updatePositionInShoppingCart'));
  268 +Yii::app()->shoppingCart->attachEventHandler('onUpdatePosition',array($cN, 'updatePositionInShoppingCart'));
255 269
256 270 $book = Book::model()->findByPk(1);
257 271 Yii::app()->shoppingCart->put($book);
258 272 ~~~
259   -When onUpdatePoistion event is fired, call center will be notified.
  273 +When onUpdatePosition event is fired, call center will be notified.
260 274
261 275 Working with a cart as CMap
262 276 ---------------------------
18 readme_ru.txt
@@ -8,7 +8,7 @@ Shopping Cart
8 8
9 9 Установка и настройка
10 10 ---------------------
11   -
  11 +### 1 вариант: Подключение через конфиг
12 12 В `protected/config/main.php` добавить:
13 13 ~~~
14 14 [php]
@@ -23,6 +23,18 @@ Shopping Cart
23 23 ),
24 24 )
25 25 ~~~
  26 +### 2 вариант: Подключение по необходимости
  27 +~~~
  28 +[php]
  29 +$cart = Yii::createComponent(array(
  30 + 'class' => 'ext.yiiext.components.shoppingCart.EShoppingCart'
  31 +));
  32 +//Важно
  33 +$cart->init();
  34 +
  35 +$book = Book::model()->findByPk(1);
  36 +$cart->put($book);
  37 +~~~
26 38
27 39 Подготавливаем модель
28 40 ---------------------
@@ -256,12 +268,12 @@ class TestDiscount extends IEDiscount {
256 268 ~~~
257 269 [php]
258 270 $cN = new CallCenterNotifier();
259   -Yii::app()->shoppingCart->attachEventHandler('onUpdatePoistion',array($cN, 'updatePositionInShoppingCart'));
  271 +Yii::app()->shoppingCart->attachEventHandler('onUpdatePosition',array($cN, 'updatePositionInShoppingCart'));
260 272
261 273 $book = Book::model()->findByPk(1);
262 274 Yii::app()->shoppingCart->put($book);
263 275 ~~~
264   -При срабатывание события onUpdatePoistion, об этом будет оповещен call center.
  276 +При срабатывание события onUpdatePosition, об этом будет оповещен call center.
265 277
266 278 CMap - работаем с корзиной, как с массивом.
267 279 -------------------------------------------

0 comments on commit 6e7f4e3

Please sign in to comment.
Something went wrong with that request. Please try again.