add better livewire support

- and reset collection indexes properly
This commit is contained in:
Joel Male
2024-09-08 08:32:51 +10:00
parent 9a59857825
commit 7a2c41e02d
4 changed files with 240 additions and 90 deletions
+11 -3
View File
@@ -340,7 +340,7 @@ public function condition(array|CartCondition $condition): self
*
* @param bool $active - If true, only conditions actually applied on the cart are returned
*/
public function getConditions(bool $active = false): CartConditionCollection
public function getConditions(bool $array = false, bool $active = false): CartConditionCollection|array
{
$conditions = new CartConditionCollection(
$this->session->get($this->sessionKeyCartConditions)
@@ -358,7 +358,9 @@ public function getConditions(bool $active = false): CartConditionCollection
});
}
return $conditions;
return $array ? $conditions->map(function (CartCondition $condition) {
return $condition->toArray();
})->toArray() : $conditions;
}
/**
@@ -404,6 +406,9 @@ public function removeCartCondition($conditionName)
$conditions->pull($conditionName);
// reset indexes
$conditions = $conditions->values();
$this->saveConditions($conditions);
}
@@ -456,6 +461,9 @@ public function removeItemCondition($itemId, $conditionName)
}
}
// reset indexes
$item['conditions'] = array_values($item['conditions']);
$this->update($itemId, [
'conditions' => $item['conditions'],
]);
@@ -728,7 +736,7 @@ protected function addItem($id, $item)
/**
* save the cart
*
* @param $cart CartCollection
* @param $cart CartCollection
*/
protected function save($cart)
{
+6 -4
View File
@@ -8,15 +8,12 @@
class CartCondition
{
/**
* @var array
*/
private $args;
/**
* the parsed raw value of the condition
*/
public $parsedRawValue = 0;
private $parsedRawValue = 0;
/**
* @param array $args (name, type, target, value)
@@ -269,4 +266,9 @@ protected function validate($args)
throw new InvalidConditionException($validator->messages()->first());
}
}
public function toArray(): array
{
return $this->args;
}
}