From 9b9ff3cd5d254337ea20ed9069f1ec2258a83b62 Mon Sep 17 00:00:00 2001 From: Jorge Jacques Date: Thu, 25 May 2017 17:50:41 -0500 Subject: [PATCH 1/3] add shipping cost support --- CartPositionInterface.php | 4 +++- ShippingCostBehavior.php | 28 ++++++++++++++++++++++++++++ ShippingCostCalculationEvent.php | 20 ++++++++++++++++++++ ShoppingCart.php | 30 ++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 ShippingCostBehavior.php create mode 100644 ShippingCostCalculationEvent.php diff --git a/CartPositionInterface.php b/CartPositionInterface.php index 367a05b..6fe8406 100644 --- a/CartPositionInterface.php +++ b/CartPositionInterface.php @@ -15,6 +15,8 @@ interface CartPositionInterface { /** Triggered on cost calculation */ const EVENT_COST_CALCULATION = 'costCalculation'; + /** Triggered on after cart shipping calculation */ + const EVENT_SHIPPING_COST_CALCULATION = 'shippingCostcalculation'; /** * @return integer @@ -40,5 +42,5 @@ public function setQuantity($quantity); /** * @return int */ - public function getQuantity(); + public function getQuantity(); } \ No newline at end of file diff --git a/ShippingCostBehavior.php b/ShippingCostBehavior.php new file mode 100644 index 0000000..f538a19 --- /dev/null +++ b/ShippingCostBehavior.php @@ -0,0 +1,28 @@ + 'onShippingCostCalculation', + ]; + } + + /** + * @param CostCalculationEvent $event + */ + public function onShippingCostCalculation($event) + { + + } +} \ No newline at end of file diff --git a/ShippingCostCalculationEvent.php b/ShippingCostCalculationEvent.php new file mode 100644 index 0000000..834f7fd --- /dev/null +++ b/ShippingCostCalculationEvent.php @@ -0,0 +1,20 @@ +getCost($withDiscount); + + $shippingCostEvent = new ShippingCostCalculationEvent([]); + $this->trigger(self::EVENT_SHIPPING_COST_CALCULATION, $shippingCostEvent); + $cost = $shippingCostEvent->shippingValue; + return $cost; + } + + /** + * Return full cart total cost + * @param $withDiscount + * @return int + */ + public function getTotal($withDiscount = false) + { + $cost = $this->getCost($withDiscount); + $shipping = $this->getShippingCost($withDiscount); + + return $cost + $shipping; + } + /** * Returns hash (md5) of the current cart, that is unique to the current combination * of positions, quantities and costs. This helps us fast compare if two carts are the same, or not, also From 4b47d409aca3b3517011f52428e132b16240bf3b Mon Sep 17 00:00:00 2001 From: Jorge Jacques Date: Thu, 25 May 2017 17:59:25 -0500 Subject: [PATCH 2/3] add shipping cost docs --- README.md | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/README.md b/README.md index 4d5a24a..31c0555 100644 --- a/README.md +++ b/README.md @@ -217,3 +217,63 @@ $cart->on(ShoppingCart::EVENT_COST_CALCULATION, function ($event) { $event->discountValue = 100; }); ``` + +5. During the calculation the following events are triggered: +- `ShoppingCart::EVENT_COST_CALCULATION` once per calculation. +- `CartPositionInterface::EVENT_COST_CALCULATION` for each position in the cart. + +You can also subscribe on this events to perform discount calculation: + +```php +$cart->on(ShoppingCart::EVENT_COST_CALCULATION, function ($event) { + $event->discountValue = 100; +}); +``` + + +Using shipping cost +--------------- + +Shipping costs are implemented as behaviors that could attached to the cart or it's positions. To use them, follow this steps: + +1. Define discount class as a subclass of yz\shoppingcart\ShippingCostBehavior + +```php +// app/components/FixedShippingCost.php + +class FixedShippingCost extends ShippingCostBehavior +{ + public $freeSince = 0; + public $ammount = 0; + + /** + * @param ShippingCostCalculationEvent $event + */ + public function onShippingCostCalculation($event) + { + if($this->owner->getCost() < $this->freeSince) + $event->shippingValue = $this->ammount; + else + $event->shippingValue = 0; + } +} +``` + +2. Add this behavior to the cart: + +```php +$cart->attachBehavior('fixedShippingCost', ['class' => 'app\components\FixedShippingCost', 'ammount' => 10, 'freeSince' => 999]); +``` + +3. To get shipping cost with discount applied: + +```php +$cost = \Yii::$app->cart->getShippingCost(true); +``` + +4. To get cart total: + +```php +$total = \Yii::$app->cart->getTotal(true); +``` + From d75af4208ac67b02344b25459928ef17bb15f002 Mon Sep 17 00:00:00 2001 From: Jorge Jacques Date: Sat, 3 Jun 2017 22:31:51 -0500 Subject: [PATCH 3/3] delete unused line --- ShoppingCart.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/ShoppingCart.php b/ShoppingCart.php index 2c2fe4d..d200dbb 100644 --- a/ShoppingCart.php +++ b/ShoppingCart.php @@ -287,8 +287,6 @@ public function getCost($withDiscount = false) */ public function getShippingCost($withDiscount = false) { - $cost = $this->getCost($withDiscount); - $shippingCostEvent = new ShippingCostCalculationEvent([]); $this->trigger(self::EVENT_SHIPPING_COST_CALCULATION, $shippingCostEvent); $cost = $shippingCostEvent->shippingValue;