Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to set custom properties to cart item #143

Merged
merged 11 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/CartManager/AbstractCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ public function updateItem(string $itemKey, CheckoutableInterface $product, int
$item->setSubItems($subItems);
}

if (!empty($params)) {
$item->setCustomProperties($params);
}
dblaichinger marked this conversation as resolved.
Show resolved Hide resolved

$this->items[$itemKey] = $item;

// trigger cart has been modified
Expand Down
16 changes: 16 additions & 0 deletions src/CartManager/AbstractCartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,20 @@ public function setIsLoading(bool $isLoading): void
{
$this->isLoading = $isLoading;
}

/**
* Sets custom properties to CartItem when provided in AbstractCart::addItem
*
* @param array $params
* @return void
*/
public function setCustomProperties(array $params): void
{
foreach ($params as $key => $value) {
$method = 'set' . ucfirst($key);
if (method_exists($this, $method)) {
$this->{$method}($value);
}
}
}
}
2 changes: 2 additions & 0 deletions src/CartManager/CartItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,6 @@ public function setAddedDateTimestamp(int $time): void;
* @return string
*/
public function getName(): string;

public function setCustomProperties(array $params): void;
}
Loading