Skip to content

Commit 8724841

Browse files
authored
Merge pull request #136 from darryldecode/dev
Merge for version 4.0
2 parents 4221103 + ffdf66d commit 8724841

13 files changed

+197
-115
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
language: php
22

33
php:
4-
- 5.4
5-
- 5.5
64
- 5.6
5+
- 7.0
6+
- 7.1
7+
- 7.2
78

89
before_script:
910
- travis_retry composer self-update

README.md

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ For Laravel 5.1~:
1919
```composer require "darryldecode/cart:~2.0"```
2020

2121
For Laravel 5.5 or 5.6~:
22-
```composer require "darryldecode/cart:~3.0"```
22+
```composer require "darryldecode/cart:~4.0"```
2323

2424
## CONFIGURATION
2525

@@ -344,19 +344,21 @@ First let's add a condition on a Cart Bases:
344344
There are also several ways of adding a condition on a cart:
345345
NOTE:
346346

347-
When adding a condition on a cart bases, the 'target' should have value of 'subtotal'.
348-
And when adding a condition on an item, the 'target' should be 'item'.
347+
When adding a condition on a cart bases, the 'target' should have value of 'subtotal' or 'total'.
348+
If the target is "subtotal" then this condition will be applied to subtotal.
349+
If the target is "total" then this condition will be applied to total.
349350
The order of operation also during calculation will vary on the order you have added the conditions.
350351

351-
Also, when adding conditions, the 'value' field will be the bases of calculation.
352+
Also, when adding conditions, the 'value' field will be the bases of calculation. You can change this order
353+
by adding 'order' parameter in CartCondition.
352354

353355
```php
354356

355357
// add single condition on a cart bases
356358
$condition = new \Darryldecode\Cart\CartCondition(array(
357359
'name' => 'VAT 12.5%',
358360
'type' => 'tax',
359-
'target' => 'subtotal',
361+
'target' => 'subtotal', // this condition will be applied to cart's subtotal when getSubTotal() is called.
360362
'value' => '12.5%',
361363
'attributes' => array( // attributes field is optional
362364
'description' => 'Value added tax',
@@ -371,23 +373,38 @@ Cart::session($userId)->condition($condition); // for a speicifc user's cart
371373
$condition1 = new \Darryldecode\Cart\CartCondition(array(
372374
'name' => 'VAT 12.5%',
373375
'type' => 'tax',
374-
'target' => 'subtotal',
376+
'target' => 'subtotal', // this condition will be applied to cart's subtotal when getSubTotal() is called.
375377
'value' => '12.5%',
376378
'order' => 2
377379
));
378380
$condition2 = new \Darryldecode\Cart\CartCondition(array(
379381
'name' => 'Express Shipping $15',
380382
'type' => 'shipping',
381-
'target' => 'subtotal',
383+
'target' => 'subtotal', // this condition will be applied to cart's subtotal when getSubTotal() is called.
382384
'value' => '+15',
383385
'order' => 1
384386
));
385387
Cart::condition($condition1);
386388
Cart::condition($condition2);
387389

388-
// The property 'Order' lets you add different conditions through for example a shopping process with multiple
390+
// Note that after adding conditions that are targeted to be applied on subtotal, the result on getTotal()
391+
// will also be affected as getTotal() depends in getSubTotal() which is the subtotal.
392+
393+
// add condition to only apply on totals, not in subtotal
394+
$condition = new \Darryldecode\Cart\CartCondition(array(
395+
'name' => 'Express Shipping $15',
396+
'type' => 'shipping',
397+
'target' => 'total', // this condition will be applied to cart's total when getTotal() is called.
398+
'value' => '+15',
399+
'order' => 1 // the order of calculation of cart base conditions. The bigger the later to be applied.
400+
));
401+
Cart::condition($condition);
402+
403+
// The property 'order' lets you control the sequence of conditions when calculated. Also it lets you add different conditions through for example a shopping process with multiple
389404
// pages and still be able to set an order to apply the conditions. If no order is defined defaults to 0
390405

406+
// NOTE!! On current version, 'order' parameter is only applicable for conditions for cart bases. It does not support on per item conditions.
407+
391408
// or add multiple conditions as array
392409
Cart::condition([$condition1, $condition2]);
393410

@@ -417,19 +434,23 @@ $condition = Cart::getCondition('VAT 12.5%');
417434
$conditionCalculatedValue = $condition->getCalculatedValue($subTotal);
418435
```
419436

420-
NOTE: All cart based conditions should be applied before calling **Cart::getTotal()**
437+
> NOTE: All cart based conditions should be added to cart's conditions before calling **Cart::getTotal()**
438+
and if there are also conditions that are targeted to be applied to subtotal, it should be added to cart's conditions
439+
before calling **Cart::getSubTotal()**
421440

422-
Then Finally you can call **Cart::getTotal()** to get the Cart Total with the applied conditions.
423441
```php
424-
$cartTotal = Cart::getTotal(); // the total will be calculated based on the conditions you ave provided
442+
$cartTotal = Cart::getSubTotal(); // the subtotal with the conditions targeted to "subtotal" applied
443+
$cartTotal = Cart::getTotal(); // the total with the conditions targeted to "total" applied
444+
$cartTotal = Cart::session($userId)->getSubTotal(); // for a specific user's cart
425445
$cartTotal = Cart::session($userId)->getTotal(); // for a specific user's cart
426446
```
427447

428448
Next is the Condition on Per-Item Bases.
429449

430450
This is very useful if you have coupons to be applied specifically on an item and not on the whole cart value.
431451

432-
NOTE: When adding a condition on a per-item bases, the 'target' should have value of 'item'.
452+
> NOTE: When adding a condition on a per-item bases, the 'target' parameter is not needed or can be omitted.
453+
unlike when adding conditions or per cart bases.
433454

434455
Now let's add condition on an item.
435456

@@ -439,7 +460,6 @@ Now let's add condition on an item.
439460
$saleCondition = new \Darryldecode\Cart\CartCondition(array(
440461
'name' => 'SALE 5%',
441462
'type' => 'tax',
442-
'target' => 'item',
443463
'value' => '-5%',
444464
));
445465

@@ -460,19 +480,16 @@ Cart::add($product);
460480
$itemCondition1 = new \Darryldecode\Cart\CartCondition(array(
461481
'name' => 'SALE 5%',
462482
'type' => 'sale',
463-
'target' => 'item',
464483
'value' => '-5%',
465484
));
466485
$itemCondition2 = new CartCondition(array(
467486
'name' => 'Item Gift Pack 25.00',
468487
'type' => 'promo',
469-
'target' => 'item',
470488
'value' => '-25',
471489
));
472490
$itemCondition3 = new \Darryldecode\Cart\CartCondition(array(
473491
'name' => 'MISC',
474492
'type' => 'misc',
475-
'target' => 'item',
476493
'value' => '+10',
477494
));
478495

@@ -488,14 +505,16 @@ $item = array(
488505
Cart::add($item);
489506
```
490507

491-
NOTE: All cart per-item conditions should be applied before calling **Cart::getSubTotal()**
508+
> NOTE: All cart per-item conditions should be added before calling **Cart::getSubTotal()**
492509
493-
Then Finally you can call **Cart::getSubTotal()** to get the Cart sub total with the applied conditions.
510+
Then Finally you can call **Cart::getSubTotal()** to get the Cart sub total with the applied conditions on each of the items.
494511
```php
495-
$cartSubTotal = Cart::getSubTotal(); // the subtotal will be calculated based on the conditions you have provided
512+
// the subtotal will be calculated based on the conditions added that has target => "subtotal"
513+
// and also conditions that are added on per item
514+
$cartSubTotal = Cart::getSubTotal();
496515
```
497516

498-
Add condition to exisiting Item on the cart: **Cart::addItemCondition($productId, $itemCondition)**
517+
Add condition to existing Item on the cart: **Cart::addItemCondition($productId, $itemCondition)**
499518

500519
Adding Condition to an existing Item on the cart is simple as well.
501520

@@ -507,7 +526,6 @@ $productID = 456;
507526
$coupon101 = new CartCondition(array(
508527
'name' => 'COUPON 101',
509528
'type' => 'coupon',
510-
'target' => 'item',
511529
'value' => '-5%',
512530
));
513531

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
"illuminate/translation": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*"
1717
},
1818
"require-dev": {
19-
"mockery/mockery": "~0.9",
20-
"phpunit/phpunit": "~4.0",
19+
"mockery/mockery": "^1.0",
20+
"phpunit/phpunit": "^5.0",
2121
"symfony/var-dumper": "2.7.*@dev"
2222
},
2323
"autoload": {

src/Darryldecode/Cart/Cart.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -559,11 +559,37 @@ public function getSubTotal($formatted = true)
559559
{
560560
$cart = $this->getContent();
561561

562-
$sum = $cart->sum(function ($item) {
562+
$sum = $cart->sum(function (ItemCollection $item) {
563563
return $item->getPriceSumWithConditions(false);
564564
});
565565

566-
return Helpers::formatValue(floatval($sum), $formatted, $this->config);
566+
// get the conditions that are meant to be applied
567+
// on the subtotal and apply it here before returning the subtotal
568+
$conditions = $this
569+
->getConditions()
570+
->filter(function (CartCondition $cond) {
571+
return $cond->getTarget() === 'subtotal';
572+
});
573+
574+
// if there is no conditions, lets just return the sum
575+
if(!$conditions->count()) return Helpers::formatValue(floatval($sum), $formatted, $this->config);
576+
577+
// there are conditions, lets apply it
578+
$newTotal = 0.00;
579+
$process = 0;
580+
581+
$conditions->each(function (CartCondition $cond) use ($sum, &$newTotal, &$process) {
582+
583+
// if this is the first iteration, the toBeCalculated
584+
// should be the sum as initial point of value.
585+
$toBeCalculated = ($process > 0) ? $newTotal : $sum;
586+
587+
$newTotal = $cond->applyCondition($toBeCalculated);
588+
589+
$process++;
590+
});
591+
592+
return Helpers::formatValue(floatval($newTotal), $formatted, $this->config);
567593
}
568594

569595
/**
@@ -581,8 +607,8 @@ public function getTotal()
581607

582608
$conditions = $this
583609
->getConditions()
584-
->filter(function ($cond) {
585-
return $cond->getTarget() === 'subtotal';
610+
->filter(function (CartCondition $cond) {
611+
return $cond->getTarget() === 'total';
586612
});
587613

588614
// if no conditions were added, just return the sub total
@@ -591,7 +617,7 @@ public function getTotal()
591617
}
592618

593619
$conditions
594-
->each(function ($cond) use ($subTotal, &$newTotal, &$process) {
620+
->each(function (CartCondition $cond) use ($subTotal, &$newTotal, &$process) {
595621
$toBeCalculated = ($process > 0) ? $newTotal : $subTotal;
596622

597623
$newTotal = $cond->applyCondition($toBeCalculated);

src/Darryldecode/Cart/CartCondition.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ public function __construct(array $args)
4343
}
4444

4545
/**
46-
* the target of where the condition is applied
46+
* the target of where the condition is applied.
47+
* NOTE: On conditions added to per item bases, target is not needed.
4748
*
4849
* @return mixed
4950
*/
5051
public function getTarget()
5152
{
52-
return $this->args['target'];
53+
return (isset($this->args['target'])) ? $this->args['target'] : '';
5354
}
5455

5556
/**
@@ -263,7 +264,6 @@ protected function validate($args)
263264
$rules = array(
264265
'name' => 'required',
265266
'type' => 'required',
266-
'target' => 'required',
267267
'value' => 'required',
268268
);
269269

src/Darryldecode/Cart/ItemCollection.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,14 @@ public function getPriceWithConditions($formatted = true)
9494
{
9595
foreach($this->conditions as $condition)
9696
{
97-
if( $condition->getTarget() === 'item' )
98-
{
99-
( $processed > 0 ) ? $toBeCalculated = $newPrice : $toBeCalculated = $originalPrice;
100-
$newPrice = $condition->applyCondition($toBeCalculated);
101-
$processed++;
102-
}
97+
( $processed > 0 ) ? $toBeCalculated = $newPrice : $toBeCalculated = $originalPrice;
98+
$newPrice = $condition->applyCondition($toBeCalculated);
99+
$processed++;
103100
}
104101
}
105102
else
106103
{
107-
if( $this['conditions']->getTarget() === 'item' )
108-
{
109-
$newPrice = $this['conditions']->applyCondition($originalPrice);
110-
}
104+
$newPrice = $this['conditions']->applyCondition($originalPrice);
111105
}
112106

113107
return Helpers::formatValue($newPrice, $formatted, $this->config);

0 commit comments

Comments
 (0)