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
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
@@ -549,7 +566,6 @@ private function serializeItems(CartCollection $cart): array
|
||||
} elseif (is_array($conditions) || $conditions instanceof \Traversable) {
|
||||
$data['conditions'] = collect($conditions)
|
||||
->map(fn($c) => $c instanceof CartCondition ? $c->toArray() : $c)
|
||||
->values()
|
||||
->toArray();
|
||||
} else {
|
||||
$data['conditions'] = [];
|
||||
@@ -565,7 +581,7 @@ private function serializeItems(CartCollection $cart): 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
|
||||
@@ -583,13 +599,13 @@ protected function itemHasConditions(ItemCollection $item): bool
|
||||
|
||||
protected function updateQuantityRelative(ItemCollection $item, string $key, mixed $value): ItemCollection
|
||||
{
|
||||
if (preg_match('/\-/', $value) === 1) {
|
||||
$value = (float) str_replace('-', '', $value);
|
||||
if (preg_match('/\-/', (string) $value) === 1) {
|
||||
$value = (float) str_replace('-', '', (string) $value);
|
||||
if (($item[$key] - $value) > 0) {
|
||||
$item[$key] -= $value;
|
||||
}
|
||||
} elseif (preg_match('/\+/', $value) === 1) {
|
||||
$item[$key] += (float) str_replace('+', '', $value);
|
||||
} elseif (preg_match('/\+/', (string) $value) === 1) {
|
||||
$item[$key] += (float) str_replace('+', '', (string) $value);
|
||||
} else {
|
||||
$item[$key] += (float) $value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user