initial functionality

This commit is contained in:
Joel Male
2024-09-03 20:22:03 +10:00
parent 2ccba20119
commit a269c188e4
4 changed files with 320 additions and 61 deletions
+46 -54
View File
@@ -10,59 +10,22 @@
class Cart
{
/**
* the item storage
*/
protected $session;
/**
* the event dispatcher
*/
protected $events;
/**
* the cart session key
*/
protected $instanceName;
/**
* the session key use for the cart
*/
protected $sessionKey;
/**
* the session key use to persist cart items
*/
protected $sessionKeyCartItems;
/**
* the session key use to persist cart conditions
*/
protected $sessionKeyCartConditions;
/**
* Configuration to pass to ItemCollection
*/
protected $config;
/**
* This holds the currently added item id in cart for association
*/
protected $currentItemId;
/**
* The number of decimals to use when formatting prices
*/
protected $decimals;
/**
* The decimal point character
*/
protected $decPoint;
/**
* our object constructor
*/
public function __construct($session, $events, $instanceName, $session_key, $config)
{
$this->events = $events;
@@ -73,6 +36,7 @@ public function __construct($session, $events, $instanceName, $session_key, $con
$this->sessionKeyCartConditions = $this->sessionKey . '_cart_conditions';
$this->config = $config;
$this->currentItemId = null;
$this->fireEvent('created');
}
@@ -109,20 +73,16 @@ public function getInstanceName()
/**
* get an item on a cart by item ID
*
* @return mixed
*/
public function get($itemId)
public function get($itemId): ?ItemCollection
{
return $this->getContent()->get($itemId);
}
/**
* check if an item exists by item ID
*
* @return bool
* check if we have this item in the cart by item ID
*/
public function has($itemId)
public function has($itemId): bool
{
return $this->getContent()->has($itemId);
}
@@ -144,10 +104,9 @@ public function add(
array|CartCondition $conditions = [],
?string $associatedModel = null
): self {
// check if its an array of items
// check if we are adding an array of items
if (is_array($id)) {
// the first argument is an array, now we will need to check if it is a multi dimensional
// array, if so, we will iterate through each item and call add again
// check if we are adding a multi dimensional array
if (Helpers::isMultiArray($id)) {
foreach ($id as $item) {
$this->add(
@@ -175,6 +134,7 @@ public function add(
return $this;
}
// if we are here, we are adding a single item
$data = [
'id' => $id,
'name' => $name,
@@ -191,11 +151,8 @@ public function add(
// validate data
$item = $this->validate($data);
// get the cart
$inCart = $this->get($id);
// if the item is already in the cart we will just update it
if ($inCart) {
if ($this->get($id)) {
$this->update($id, $item);
} else {
$this->addItem($id, $item);
@@ -378,11 +335,31 @@ public function condition(array|CartCondition $condition): self
/**
* get conditions applied on the cart
*
* @return CartConditionCollection
* @param bool $active - If true, only conditions actually applied on the cart are returned
*/
public function getConditions()
public function getConditions(bool $active = false): CartConditionCollection
{
return new CartConditionCollection($this->session->get($this->sessionKeyCartConditions));
$conditions = new CartConditionCollection(
$this->session->get($this->sessionKeyCartConditions)
);
if ($active) {
return $conditions->filter(function (CartCondition $condition) {
$target = $condition->getTarget();
$amount = $target == 'subtotal' ? $this->getSubTotal(false) : $this->getTotal();
return $condition->getMinimum() <= $amount;
})->filter(function (CartCondition $condition) {
$target = $condition->getTarget();
$amount = $target == 'subtotal' ? $this->getSubTotal(false) : $this->getTotal();
return $condition->getMaximum() >= $amount;
});
}
return $conditions;
}
/**
@@ -575,6 +552,12 @@ public function getSubTotal($formatted = true): int|float|string
->filter(function (CartCondition $condition) {
return $condition->getTarget() === 'subtotal';
})
->filter(function (CartCondition $condition) use ($subTotalSum) {
return $condition->getMinimum() === null || $condition->getMinimum() <= $subTotalSum;
})
->filter(function (CartCondition $condition) use ($subTotalSum) {
return $condition->getMaximum() === null || $condition->getMaximum() >= $subTotalSum;
})
->sortBy(function (CartCondition $condition) {
return $condition->getOrder();
});
@@ -607,6 +590,15 @@ public function getTotal(): int|float
$conditionsForTotal = $this->getConditions()
->filter(function (CartCondition $condition) {
return $condition->getTarget() === 'total';
})
->filter(function (CartCondition $condition) use ($subTotal) {
return $condition->getMinimum() === null || $condition->getMinimum() <= $subTotal;
})
->filter(function (CartCondition $condition) use ($subTotal) {
return $condition->getMaximum() === null || $condition->getMaximum() >= $subTotal;
})
->sortBy(function (CartCondition $condition) {
return $condition->getOrder();
});
// if no conditions were added, just return the sub total
+16
View File
@@ -65,6 +65,22 @@ public function getType()
return $this->args['type'];
}
/**
* Get the minimum value of this condition
*/
public function getMinimum(): int|float|null
{
return $this->args['minimum'] ?? null;
}
/**
* Get the maximum value of this condition
*/
public function getMaximum(): int|float|null
{
return $this->args['maximum'] ?? null;
}
/**
* get the additional attributes of a condition
*