From 7fd7dfc0953885e5e29e4cfac4c8767248d81037 Mon Sep 17 00:00:00 2001 From: Daniel Blaichinger Date: Mon, 5 Feb 2024 11:40:11 +0100 Subject: [PATCH] Add possibility to set custom properties to cart item (#143) * Introduce setCustomProperties to CartItem which makes it possible to use $params in Cart::addItem * Introduce setCustomProperties to CartItem which makes it possible to use $params in Cart::addItem * Fixes review comment in AbstractCart.php Co-authored-by: Sebastian Blank * Remove method declaration from CartItemInterface * adding some docs * Added more details to 11_Cart_Manager.md --------- Co-authored-by: Sebastian Blank Co-authored-by: Christian Fasching --- doc/11_Cart_Manager.md | 12 ++++++++++++ src/CartManager/AbstractCart.php | 4 ++++ src/CartManager/AbstractCartItem.php | 16 ++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/doc/11_Cart_Manager.md b/doc/11_Cart_Manager.md index 383c49959..c8afab82f 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 4eb7213a0..17c3de49c 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 883e72fa2..884ea1d38 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); + } + } + } }