Permalink
Browse files

shopping cart:

- Added ECartPositionBehavior::setDiscountPrice
- Added EShoppingCart::setDiscountPrice

git-svn-id: http://yiiext.googlecode.com/svn/trunk/app/extensions/yiiext/components/shoppingCart@312 175f29da-e50b-11de-93f0-cd01e99e34ab
  • Loading branch information...
1 parent 40a90ac commit 178888a2daf357328756681f815344c223acbc4f @samdark samdark committed Oct 17, 2010
Showing with 93 additions and 50 deletions.
  1. +17 −14 ECartPositionBehaviour.php
  2. +38 −9 EShoppingCart.php
  3. +1 −1 IECartPosition.php
  4. +29 −20 changelog.txt
  5. +4 −4 discounts/TestDiscount.php
  6. +2 −1 readme_en.txt
  7. +2 −1 readme_ru.txt
View
@@ -4,15 +4,15 @@
* position in the cart
*
* @author pirrat <mrakobesov@gmail.com>
- * @version 0.7
+ * @version 0.8
* @package ShoppingCart
*
- * Can be used with not AR models.
+ * Can be used with non-AR models.
*/
class ECartPositionBehaviour extends CActiveRecordBehavior {
/**
- * number of positions
+ * Positions number
* @var int
*/
private $quantity = 0;
@@ -23,16 +23,11 @@ class ECartPositionBehaviour extends CActiveRecordBehavior {
private $refresh = true;
/**
- * Сумма скидки на позицию
+ * Position discount sum
* @var float
*/
private $discountPrice = 0.0;
-
- public function init() {
-
- }
-
/**
* Returns total price for all units of the position
* @param bool $withDiscount
@@ -64,29 +59,37 @@ public function setQuantity($newVal) {
}
/**
- * Magic method, call on restore from session
- * Refresh data to model
+ * Magic method. Called on session restore.
*/
public function __wakeup() {
if ($this->refresh === true)
$this->owner->refresh();
}
/**
- *
+ * If we need to refresh model on restoring session.
+ * Default is true.
* @param boolean $refresh
*/
public function setRefresh($refresh) {
$this->refresh = $refresh;
}
/**
- * Установить сумму скидки на позицию
- * @param $price
+ * Add $price to position discount sum
+ * @param float $price
* @return void
*/
public function addDiscountPrice($price) {
$this->discountPrice += $price;
}
+ /**
+ * Set position discount sum
+ * @param float $price
+ * @return void
+ */
+ public function setDiscountPrice($price) {
+ $this->discountPrice = $price;
+ }
}
View
@@ -3,7 +3,7 @@
* Shopping cart class
*
* @author pirrat <mrakobesov@gmail.com>
- * @version 0.7
+ * @version 0.8
* @package ShoppingCart
*/
@@ -16,14 +16,14 @@ class EShoppingCart extends CMap {
public $refresh = true;
public $discounts = array();
+
/**
- * Сумма скидки на всю корзину
+ * Cart-wide discount sum
* @var float
*/
protected $discountPrice = 0.0;
- public function init() {
-
+ public function init(){
$this->restoreFromSession();
}
@@ -41,7 +41,7 @@ public function restoreFromSession() {
/**
* Add item to the shopping cart
* If the position was previously added to the cart,
- * then information of it is updated, and count increases by $quantity
+ * then information about it is updated, and count increases by $quantity
* @param IECartPosition $position
* @param int count of elements positions
*/
@@ -59,6 +59,8 @@ public function put(IECartPosition $position, $quantity = 1) {
/**
+ * Add $value items to position with $key specified
+ * @return void
* @param mixed $key
* @param mixed $value
*/
@@ -80,9 +82,9 @@ public function remove($key) {
/**
* Updates the position in the shopping cart
- * If the position was previously added, then it will be updated in shopping cart,
- * if the position was not previously in the cart, it will be added there.
- * If the count of less than 1, the position will be deleted.
+ * If position was previously added, then it will be updated in shopping cart,
+ * if position was not previously in the cart, it will be added there.
+ * If count is less than 1, the position will be deleted.
*
* @param IECartPosition $position
* @param int $quantity
@@ -149,16 +151,26 @@ public function getCost($withDiscount = true) {
return $price;
}
+ /**
+ * onRemovePosition event
+ * @param $event
+ * @return void
+ */
public function onRemovePosition($event) {
$this->raiseEvent('onRemovePosition', $event);
}
+ /**
+ * onUpdatePoistion event
+ * @param $event
+ * @return void
+ */
public function onUpdatePoistion($event) {
$this->raiseEvent('onUpdatePoistion', $event);
}
/**
- * apply discounts for all positions
+ * Apply discounts to all positions
* @return void
*/
protected function applyDiscounts() {
@@ -170,6 +182,22 @@ protected function applyDiscounts() {
}
}
+ /**
+ * Set cart-wide discount sum
+ *
+ * @param float $price
+ * @return void
+ */
+ public function setDiscountPrice($price){
+ $this->discountPrice = $price;
+ }
+
+ /**
+ * Add $price to cart-wide discount sum
+ *
+ * @param float $price
+ * @return void
+ */
public function addDiscountPrice($price){
$this->discountPrice += $price;
}
@@ -184,6 +212,7 @@ public function getPositions()
}
/**
+ * Returns if cart is empty
* @return bool
*/
public function isEmpty()
View
@@ -4,7 +4,7 @@
* IECartPosition
*
* @author pirrat <mrakobesov@gmail.com>
- * @version 0.7
+ * @version 0.8
* @package ShoppingCart
*/
interface IECartPosition {
View
@@ -1,29 +1,38 @@
-0.7
-[+] Updated English readme.
-[*] Refactor: renamed variables and methods.
-[*] Updated documentation.
-[+] Add Discounts.
-[+] Add new Methods.
-[+] Add Events.
-[+] New Unit tests.
+0.8 (next)
+===
+- Added ECartPositionBehavior::setDiscountPrice (Sam Dark)
+- Added EShoppingCart::setDiscountPrice (Sam Dark)
+0.7
+===
+- Updated English readme (Sam Dark)
+- Refactor: renamed variables and methods.
+- Updated documentation.
+- Add Discounts.
+- Add new Methods.
+- Add Events.
+- New Unit tests.
0.6
-[*] New naming conventions.
-[+] English readme.
+===
+- New naming conventions.
+- English readme.
0.5rc2
-[*] Fixed bug when product count was set to zero when restoring from session.
-[*] Changed behaviour of put() method.
-[+] Added update() method. Updates cart object info.
+======
+- Fixed bug when product count was set to zero when restoring from session.
+- Changed behaviour of put() method.
+- Added update() method. Updates cart object info.
0.5
-[*] Now only IECartPosition object could be put into cart.
-[*] Method names changed.
-[+] Unit tests.
-[+] PHP native interface checks.
-[*] Renamed cart class to CShoppingCart.
-[+] Single model can be put in array style.
+===
+- Now only IECartPosition object could be put into cart.
+- Method names changed.
+- Unit tests.
+- PHP native interface checks.
+- Renamed cart class to CShoppingCart.
+- Single model can be put in array style.
0.2
-[+] Initial relase.
+===
+- Initial relase.
@@ -1,19 +1,19 @@
<?php
/**
- * тестовая скидка, применяется если добавленно больше 1 позиции товара:
- * добавляешь в корзину две единицы товара и получаешь скидку на первую позицию в $rate %
+ * Test discount is applied when there are more than one item in position:
+ * if there are two items in the same position (two equal products), add $rate % discount
+ * to the first item.
*/
class TestDiscount extends IEDiscount {
/**
- * Скидка в %
+ * Discount %
*/
public $rate = 30;
public function apply() {
foreach ($this->shoppingCart as $position) {
$quantity = $position->getQuantity();
if ($quantity > 1) {
- $productPrice = $position->getPrice();
$discountPrice = $this->rate * $position->getPrice() / 100;
$position->addDiscountPrice($discountPrice);
}
View
@@ -194,7 +194,8 @@ will change cart total or a single position price.
Discount is a class that implements IEDiscount and defines apply() method that describes
how exactly discount is applied.
-Discount is calculated by applying addDiscountPrice() position's method (Position::addDiscountPrice) or whole cart method (EShoppingCart::addDiscountPrice).
+Discount is calculated by applying Position::addDiscountPrice/Position::setDiscountPrice position's
+method or EShoppingCart::addDiscountPrice/EShoppingCart::setDiscountPrice cart-wide method.
This methods are getting one parameter that holds a value of cart total reduction or individual position reduction.
Discount class example:
View
@@ -198,7 +198,8 @@ $price = $position->getQuantity(); //2
В методе apply должен быть описан механизм применения скидки.
-Применяется она путем вызова метода addDiscountPrice либо у позиции, либо у корзины( EShoppingCart::addDiscountPrice и Position::addDiscountPrice).
+Применяется она путем вызова метода Position::addDiscountPrice/Position::setDiscountPrice у позиции,
+либо EShoppingCart::addDiscountPrice/EShoppingCart::setDiscountPrice у корзины.
В метод передается значение на которое будет уменьшена стоимость(всей корзины или отдельной позиции соответственно).
Пример класса скидки:

0 comments on commit 178888a

Please sign in to comment.