test updates & working on calculating condition values based on orders

This commit is contained in:
Joel Male
2024-08-15 17:21:19 +10:00
parent 77fa8601b9
commit 85ae0c30c5
10 changed files with 1758 additions and 972 deletions
+41 -1
View File
@@ -341,7 +341,14 @@ #### Getting conditions on the cart: **Cart::getConditions()**
$condition->getAttributes(); // the attributes of the condition, returns an empty [] if no attributes added
```
### Calculating condition value: **Cart::getCondition('Tax: 10%')->getCalculatedValue($subTotal)**
### Calculating condition value
There are 2 ways to calculate the value of a condition:
1. Using the `getCalculatedValue` method on the condition instance
2. Using the `getCalculatedValueForCondition` method on the cart instance and passing the condition name
#### Using the `getCalculatedValue` method on the condition instance
```php
$subTotal = Cart::getSubTotal();
@@ -349,6 +356,39 @@ ### Calculating condition value: **Cart::getCondition('Tax: 10%')->getCalculated
$conditionCalculatedValue = $condition->getCalculatedValue($subTotal);
```
#### Using the `getCalculatedValueForCondition` method on the cart instance
This method automatically calculates the value of a condition by it's name based on the order of the conditions.
```php
Cart::add([
'id' => 1,
'name' => 'Apple iPhone 15',
'price' => 200,
'quantity' => 1,
'attributes' => [],
]);
$couponDiscount = new CartCondition([
'name' => 'Coupon Discount',
'type' => 'discount',
'target' => 'subtotal',
'value' => '-200',
'order' => 1,
]);
$giftCard = new CartCondition([
'name' => 'Gift Card',
'type' => 'discount',
'target' => 'subtotal',
'value' => '-200',
'order' => 2,
]);
Cart::getCalculatedValueForCondition('Coupon Discount'); // returns 200
Cart::getCalculatedValueForCondition('Gift Card'); // returns 0 as the coupon discount is applied first and brings the subtotal to 0
```
### Condition on items
Item conditions are useful if you have discounts to be applied specifically on an item and not on the whole cart value.