diff --git a/doc/11_Cart_Manager.md b/doc/11_Cart_Manager.md index 469e69e1..e6b12a5b 100644 --- a/doc/11_Cart_Manager.md +++ b/doc/11_Cart_Manager.md @@ -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` diff --git a/src/CartManager/AbstractCart.php b/src/CartManager/AbstractCart.php index 4eb7213a..17c3de49 100644 --- a/src/CartManager/AbstractCart.php +++ b/src/CartManager/AbstractCart.php @@ -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 diff --git a/src/CartManager/AbstractCartItem.php b/src/CartManager/AbstractCartItem.php index 883e72fa..884ea1d3 100644 --- a/src/CartManager/AbstractCartItem.php +++ b/src/CartManager/AbstractCartItem.php @@ -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); + } + } + } }