Skip to content

Commit 2602f70

Browse files
committed
remove multi array support for conditions constructors and update readme docs
1 parent 1ff7bdc commit 2602f70

File tree

3 files changed

+82
-241
lines changed

3 files changed

+82
-241
lines changed

README.md

Lines changed: 31 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -204,17 +204,24 @@ $subTotal = Cart::getTotal();
204204
## Conditions
205205

206206
Laravel Shopping Cart supports cart conditions.
207-
Condiitons are very useful in terms of (coupons,discounts,sale,per-item sale and discounts etc.)
207+
Conditions are very useful in terms of (coupons,discounts,sale,per-item sale and discounts etc.)
208208
See below carefully on how to use conditions.
209209

210210
Conditions can be added on:
211-
1.) Cart
212-
2.) Item
211+
212+
1.) Whole Cart Value bases
213+
214+
2.) Per-Item Bases
213215

214216
First let's add a condition on a Cart Bases:
215217

216-
There are also several ways of adding a condtion on a cart:
217-
NOTE: When adding a condition on a cart bases, the 'target' should have value of 'total'.
218+
There are also several ways of adding a condition on a cart:
219+
NOTE:
220+
221+
When adding a condition on a cart bases, the 'target' should have value of 'total'.
222+
The order of operation also during calculation will vary on the order you have added the conditions.
223+
224+
Also, when adding conditions, the 'value' field will be the bases of calculation.
218225

219226
```php
220227

@@ -244,28 +251,23 @@ $condition2 = new CartCondition(array(
244251
Cart::condition($condition1);
245252
Cart::condition($condition2);
246253

247-
// or add multiple conditions one condition instances
248-
$condition = new CartCondition(array(
249-
array(
250-
'name' => 'COUPON LESS 12.5%',
251-
'type' => 'tax',
252-
'target' => 'total',
253-
'value' => '-12.5%',
254-
),
255-
array(
256-
'name' => 'Express Shipping $15',
257-
'type' => 'shipping',
258-
'target' => 'total',
259-
'value' => '+15',
260-
)
261-
)
262-
);
263-
Cart::condition($condition);
254+
// or add multiple conditions as array
255+
Cart::condition([$condition1, $condition2]);
256+
257+
// To get all applied conditions on a cart, use below:
258+
$carConditions = Cart::getConditions();
259+
foreach($carConditions as $condition)
260+
{
261+
$condition->getTarget(); // the target of which the condition was applied
262+
$condition->getName(); // the name of the condition
263+
$condition->getType(); // the type
264+
$condition->getValue(); // the value of the condition
265+
}
264266
```
265267

266268
NOTE: All cart based conditions should be applied before calling **Cart::getTotal()**
267269

268-
Then Finaly you can call **Cart::getTotal()** to get the Cart Total with the applied conditions.
270+
Then Finally you can call **Cart::getTotal()** to get the Cart Total with the applied conditions.
269271
```php
270272
$cartTotal = Cart::getTotal(); // the total will be calculated based on the conditions you ave provided
271273
```
@@ -302,47 +304,12 @@ $product = array(
302304
Cart::add($product);
303305

304306
// you may also add multiple condition on an item
305-
$itemConditions = new CartCondition(array(
306-
array(
307-
'name' => 'SALE 5%',
308-
'type' => 'sale',
309-
'target' => 'subtotal',
310-
'value' => '-5%',
311-
),
312-
array(
313-
'name' => 'Item Gift Pack 25.00',
314-
'type' => 'promo',
315-
'target' => 'subtotal',
316-
'value' => '-25',
317-
),
318-
array(
319-
'name' => 'MISC',
320-
'type' => 'misc',
321-
'target' => 'subtotal',
322-
'value' => '+10',
323-
)
324-
));
325-
326-
$item = array(
327-
'id' => 456,
328-
'name' => 'Sample Item 1',
329-
'price' => 100,
330-
'quantity' => 1,
331-
'attributes' => array(),
332-
'conditions' => $itemConditions
333-
);
334-
335-
Cart::add($item);
336-
337-
// This is also valid
338307
$itemCondition1 = new CartCondition(array(
339-
array(
340-
'name' => 'SALE 5%',
341-
'type' => 'sale',
342-
'target' => 'subtotal',
343-
'value' => '-5%',
344-
)
345-
));
308+
'name' => 'SALE 5%',
309+
'type' => 'sale',
310+
'target' => 'subtotal',
311+
'value' => '-5%',
312+
));
346313
$itemCondition2 = new CartCondition(array(
347314
'name' => 'Item Gift Pack 25.00',
348315
'type' => 'promo',
@@ -370,7 +337,7 @@ Cart::add($item);
370337

371338
NOTE: All cart per-item conditions should be applied before calling **Cart::getSubTotal()**
372339

373-
Then Finaly you can call **Cart::getSubTotal()** to get the Cart sub total with the applied conditions.
340+
Then Finally you can call **Cart::getSubTotal()** to get the Cart sub total with the applied conditions.
374341
```php
375342
$cartSubTotal = Cart::getSubTotal(); // the subtotal will be calculated based on the conditions you have provided
376343
```

src/Darryldecode/Cart/CartCondition.php

Lines changed: 12 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,15 @@ class CartCondition {
1919

2020
/**
2121
* @param array $args (name, type, target, value)
22+
* @throws InvalidConditionException
2223
*/
2324
public function __construct(array $args)
2425
{
2526
$this->args = $args;
2627

2728
if( Helpers::isMultiArray($args) )
2829
{
29-
foreach($args as $arg)
30-
{
31-
$this->validate($arg);
32-
}
30+
Throw new InvalidConditionException('Multi dimensional array is not supported.');
3331
}
3432
else
3533
{
@@ -40,118 +38,41 @@ public function __construct(array $args)
4038
/**
4139
* the target of where the condition is applied
4240
*
43-
* @param null $conditionName | if no conditionName provided, it will only pull the first item on conditions array
4441
* @return mixed
4542
*/
46-
public function getTarget($conditionName = null)
43+
public function getTarget()
4744
{
48-
if( Helpers::isMultiArray($this->args) )
49-
{
50-
if( $conditionName )
51-
{
52-
foreach($this->args as $condData)
53-
{
54-
if( $condData['name'] == $conditionName )
55-
{
56-
return $condData['target'];
57-
}
58-
}
59-
}
60-
else
61-
{
62-
return $this->args[0]['target'];
63-
}
64-
} else {
65-
return $this->args['target'];
66-
}
45+
return $this->args['target'];
6746
}
6847

6948
/**
7049
* the name of the condition
7150
*
72-
* @param null $key
7351
* @return mixed
7452
*/
75-
public function getName($key = null)
53+
public function getName()
7654
{
77-
if( Helpers::isMultiArray($this->args) )
78-
{
79-
if( $key )
80-
{
81-
foreach($this->args as $k => $v)
82-
{
83-
if( $k == $key )
84-
{
85-
return $v['name'];
86-
}
87-
}
88-
}
89-
else
90-
{
91-
return $this->args[0]['name'];
92-
}
93-
} else {
94-
return $this->args['name'];
95-
}
55+
return $this->args['name'];
9656
}
9757

9858
/**
99-
* the type of the condition, if no condition name provided it will just pull
100-
* the first condition argument
59+
* the type of the condition
10160
*
102-
* @param null $conditionName
10361
* @return mixed
10462
*/
105-
public function getType($conditionName = null)
63+
public function getType()
10664
{
107-
if( Helpers::isMultiArray($this->args) )
108-
{
109-
if( $conditionName )
110-
{
111-
foreach($this->args as $condData)
112-
{
113-
if( $condData['name'] == $conditionName )
114-
{
115-
return $condData['type'];
116-
}
117-
}
118-
}
119-
else
120-
{
121-
return $this->args[0]['type'];
122-
}
123-
} else {
124-
return $this->args['type'];
125-
}
65+
return $this->args['type'];
12666
}
12767

12868
/**
12969
* the value of this the condition
13070
*
131-
* @param null $conditionName
13271
* @return mixed
13372
*/
134-
public function getValue($conditionName = null)
73+
public function getValue()
13574
{
136-
if( Helpers::isMultiArray($this->args) )
137-
{
138-
if( $conditionName )
139-
{
140-
foreach($this->args as $condData)
141-
{
142-
if( $condData['name'] == $conditionName )
143-
{
144-
return $condData['value'];
145-
}
146-
}
147-
}
148-
else
149-
{
150-
return $this->args[0]['value'];
151-
}
152-
} else {
153-
return $this->args['value'];
154-
}
75+
return $this->args['value'];
15576
}
15677

15778
/**
@@ -162,29 +83,7 @@ public function getValue($conditionName = null)
16283
*/
16384
public function applyCondition($totalOrSubTotalOrPrice)
16485
{
165-
if( Helpers::isMultiArray($this->args) )
166-
{
167-
$originalPrice = $totalOrSubTotalOrPrice;
168-
169-
$newPrice = 0.00;
170-
171-
$processed = 0;
172-
173-
foreach($this->args as $arg)
174-
{
175-
( $processed > 0 ) ? $toBeCalculated = $newPrice : $toBeCalculated = $originalPrice;
176-
177-
$newPrice = $this->apply($toBeCalculated, $arg['value']);
178-
179-
$processed++;
180-
}
181-
182-
return $newPrice;
183-
}
184-
else
185-
{
186-
return $this->apply($totalOrSubTotalOrPrice, $this->getValue());
187-
}
86+
return $this->apply($totalOrSubTotalOrPrice, $this->getValue());
18887
}
18988

19089
/**

0 commit comments

Comments
 (0)