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 all 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
12 changes: 12 additions & 0 deletions doc/11_Cart_Manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,15 @@ Once set, the cart manager uses all specific settings of the currently active ch
in the configuration (identified by tenant name).

See also [Demo](https://github.com/pimcore/demo/blob/11.x/config/ecommerce/base-ecommerce.yaml#L197) for some examples.


## Adding Custom Properties to Cart Items

Following steps are necessary to add additional custom properties to cart items:

1) Extend `Pimcore\Bundle\EcommerceFrameworkBundle\CartManager\CartItem` implementation and add your custom properties including getters/setters.
2) Extend `Cart::getCartItemClassName` implementation and make sure your custom `CartItem` implementation gets returned.
3) Provide the custom properties as key-value pairs in `$params` parameter in the following methods:
1) `Pimcore\Bundle\EcommerceFrameworkBundle\CartManager\AbstractCart::addItem`
2) `Pimcore\Bundle\EcommerceFrameworkBundle\CartManager\AbstractCart::updateItem`
3) `Pimcore\Bundle\EcommerceFrameworkBundle\CartManager\CartManagerInterface::addToCart`
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 (method_exists($item, 'setCustomProperties')) {
$item->setCustomProperties($params);
}

$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);
}
}
}
}
Loading