mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 22:01:48 +00:00
fix: resolve Cart.php regressions after driver refactor
- Cast \$value to string in updateQuantityRelative() before passing to preg_match() and str_replace() to fix TypeError when integer quantities are passed - Reconstruct ItemAttributeCollection and CartCondition objects from serialized arrays in getContent() so attribute property access (->size) and condition methods (.getName()) work correctly after deserialization - Store CartCondition objects directly (not serialized to arrays) in saveConditions() to preserve object identity for parsedRawValue tracking via getCalculatedValue() - Remove ->values() re-indexing from serializeItems() conditions serialization to preserve original condition array indices after partial removal
This commit is contained in:
+23
-7
@@ -464,7 +464,24 @@ public function getTotalQuantity(): int|float
|
|||||||
public function getContent(): CartCollection
|
public function getContent(): CartCollection
|
||||||
{
|
{
|
||||||
return (new CartCollection($this->driver->getItems()))
|
return (new CartCollection($this->driver->getItems()))
|
||||||
->transform(fn($item) => $item instanceof ItemCollection ? $item : new ItemCollection($item, $this->config))
|
->transform(function ($item) {
|
||||||
|
if ($item instanceof ItemCollection) {
|
||||||
|
return $item;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_array($item) && isset($item['attributes']) && is_array($item['attributes'])) {
|
||||||
|
$item['attributes'] = new ItemAttributeCollection($item['attributes']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_array($item) && isset($item['conditions']) && is_array($item['conditions'])) {
|
||||||
|
$item['conditions'] = array_map(
|
||||||
|
fn($c) => is_array($c) ? new CartCondition($c) : $c,
|
||||||
|
$item['conditions']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ItemCollection($item, $this->config);
|
||||||
|
})
|
||||||
->reject(fn($item) => ! ($item instanceof ItemCollection));
|
->reject(fn($item) => ! ($item instanceof ItemCollection));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -549,7 +566,6 @@ private function serializeItems(CartCollection $cart): array
|
|||||||
} elseif (is_array($conditions) || $conditions instanceof \Traversable) {
|
} elseif (is_array($conditions) || $conditions instanceof \Traversable) {
|
||||||
$data['conditions'] = collect($conditions)
|
$data['conditions'] = collect($conditions)
|
||||||
->map(fn($c) => $c instanceof CartCondition ? $c->toArray() : $c)
|
->map(fn($c) => $c instanceof CartCondition ? $c->toArray() : $c)
|
||||||
->values()
|
|
||||||
->toArray();
|
->toArray();
|
||||||
} else {
|
} else {
|
||||||
$data['conditions'] = [];
|
$data['conditions'] = [];
|
||||||
@@ -565,7 +581,7 @@ private function serializeItems(CartCollection $cart): array
|
|||||||
|
|
||||||
private function serializeConditions(CartConditionCollection $conditions): array
|
private function serializeConditions(CartConditionCollection $conditions): array
|
||||||
{
|
{
|
||||||
return $conditions->map(fn(CartCondition $c) => $c->toArray())->toArray();
|
return $conditions->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function itemHasConditions(ItemCollection $item): bool
|
protected function itemHasConditions(ItemCollection $item): bool
|
||||||
@@ -583,13 +599,13 @@ protected function itemHasConditions(ItemCollection $item): bool
|
|||||||
|
|
||||||
protected function updateQuantityRelative(ItemCollection $item, string $key, mixed $value): ItemCollection
|
protected function updateQuantityRelative(ItemCollection $item, string $key, mixed $value): ItemCollection
|
||||||
{
|
{
|
||||||
if (preg_match('/\-/', $value) === 1) {
|
if (preg_match('/\-/', (string) $value) === 1) {
|
||||||
$value = (float) str_replace('-', '', $value);
|
$value = (float) str_replace('-', '', (string) $value);
|
||||||
if (($item[$key] - $value) > 0) {
|
if (($item[$key] - $value) > 0) {
|
||||||
$item[$key] -= $value;
|
$item[$key] -= $value;
|
||||||
}
|
}
|
||||||
} elseif (preg_match('/\+/', $value) === 1) {
|
} elseif (preg_match('/\+/', (string) $value) === 1) {
|
||||||
$item[$key] += (float) str_replace('+', '', $value);
|
$item[$key] += (float) str_replace('+', '', (string) $value);
|
||||||
} else {
|
} else {
|
||||||
$item[$key] += (float) $value;
|
$item[$key] += (float) $value;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user