From 663fbc174f60baf4154c70d730f62ec7f6398ceb Mon Sep 17 00:00:00 2001 From: Daniel Blaichinger Date: Thu, 14 Dec 2023 09:27:50 +0100 Subject: [PATCH 1/6] Introduce setCustomProperties to CartItem which makes it possible to use $params in Cart::addItem --- src/CartManager/AbstractCart.php | 4 ++++ src/CartManager/AbstractCartItem.php | 16 ++++++++++++++++ src/CartManager/CartItemInterface.php | 2 ++ 3 files changed, 22 insertions(+) diff --git a/src/CartManager/AbstractCart.php b/src/CartManager/AbstractCart.php index 4eb7213a0..a284b6a2c 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 (!empty($params)) { + $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); + } + } + } } diff --git a/src/CartManager/CartItemInterface.php b/src/CartManager/CartItemInterface.php index 2c1c8a627..d352ba825 100644 --- a/src/CartManager/CartItemInterface.php +++ b/src/CartManager/CartItemInterface.php @@ -112,4 +112,6 @@ public function setAddedDateTimestamp(int $time): void; * @return string */ public function getName(): string; + + public function setCustomProperties(array $params): void; } From 4583e5d74c04638f1d250ee9c33697b26ab29eef Mon Sep 17 00:00:00 2001 From: Daniel Blaichinger Date: Thu, 14 Dec 2023 09:27:50 +0100 Subject: [PATCH 2/6] Introduce setCustomProperties to CartItem which makes it possible to use $params in Cart::addItem --- src/CartManager/AbstractCart.php | 4 ++++ src/CartManager/AbstractCartItem.php | 16 ++++++++++++++++ src/CartManager/CartItemInterface.php | 2 ++ 3 files changed, 22 insertions(+) diff --git a/src/CartManager/AbstractCart.php b/src/CartManager/AbstractCart.php index 4eb7213a0..a284b6a2c 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 (!empty($params)) { + $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); + } + } + } } diff --git a/src/CartManager/CartItemInterface.php b/src/CartManager/CartItemInterface.php index 2c1c8a627..d352ba825 100644 --- a/src/CartManager/CartItemInterface.php +++ b/src/CartManager/CartItemInterface.php @@ -112,4 +112,6 @@ public function setAddedDateTimestamp(int $time): void; * @return string */ public function getName(): string; + + public function setCustomProperties(array $params): void; } From 733a6c697fde947228b68b0a2afb60d25c65a589 Mon Sep 17 00:00:00 2001 From: Daniel Blaichinger Date: Fri, 15 Dec 2023 13:38:31 +0100 Subject: [PATCH 3/6] Fixes review comment in AbstractCart.php Co-authored-by: Sebastian Blank --- src/CartManager/AbstractCart.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/CartManager/AbstractCart.php b/src/CartManager/AbstractCart.php index a284b6a2c..f3ba0f526 100644 --- a/src/CartManager/AbstractCart.php +++ b/src/CartManager/AbstractCart.php @@ -158,9 +158,7 @@ public function updateItem(string $itemKey, CheckoutableInterface $product, int $item->setSubItems($subItems); } - if (!empty($params)) { - $item->setCustomProperties($params); - } + $item->setCustomProperties($params); $this->items[$itemKey] = $item; From 307258d3d586490e6157666fbaa7aa42a432889a Mon Sep 17 00:00:00 2001 From: Daniel Blaichinger Date: Sat, 16 Dec 2023 11:29:19 +0100 Subject: [PATCH 4/6] Remove method declaration from CartItemInterface --- src/CartManager/AbstractCart.php | 4 +++- src/CartManager/CartItemInterface.php | 2 -- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CartManager/AbstractCart.php b/src/CartManager/AbstractCart.php index f3ba0f526..17c3de49c 100644 --- a/src/CartManager/AbstractCart.php +++ b/src/CartManager/AbstractCart.php @@ -158,7 +158,9 @@ public function updateItem(string $itemKey, CheckoutableInterface $product, int $item->setSubItems($subItems); } - $item->setCustomProperties($params); + if (method_exists($item, 'setCustomProperties')) { + $item->setCustomProperties($params); + } $this->items[$itemKey] = $item; diff --git a/src/CartManager/CartItemInterface.php b/src/CartManager/CartItemInterface.php index d352ba825..2c1c8a627 100644 --- a/src/CartManager/CartItemInterface.php +++ b/src/CartManager/CartItemInterface.php @@ -112,6 +112,4 @@ public function setAddedDateTimestamp(int $time): void; * @return string */ public function getName(): string; - - public function setCustomProperties(array $params): void; } From 6fde05353898a6a5e1feda972d7f639fb334e289 Mon Sep 17 00:00:00 2001 From: Christian Fasching Date: Mon, 29 Jan 2024 10:28:43 +0100 Subject: [PATCH 5/6] adding some docs --- doc/11_Cart_Manager.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/doc/11_Cart_Manager.md b/doc/11_Cart_Manager.md index 469e69e1c..0a2d0714a 100644 --- a/doc/11_Cart_Manager.md +++ b/doc/11_Cart_Manager.md @@ -178,3 +178,12 @@ 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 `CartItem` implementation and extend it with your custom property fields and getters/setters. +2) Extend `Cart` implementation and make use your custom `CartItem` implementation is used. +3) Provide the custom properties as key-value pairs in `params` parameter in `addItem` and `updateItem` of `Cart` or `CartManager`. From b43fa91b230f4891bfa1d4d388705e76a899caa4 Mon Sep 17 00:00:00 2001 From: Daniel Blaichinger Date: Fri, 2 Feb 2024 15:08:40 +0100 Subject: [PATCH 6/6] Added more details to 11_Cart_Manager.md --- doc/11_Cart_Manager.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/doc/11_Cart_Manager.md b/doc/11_Cart_Manager.md index 0a2d0714a..e6b12a5be 100644 --- a/doc/11_Cart_Manager.md +++ b/doc/11_Cart_Manager.md @@ -184,6 +184,9 @@ See also [Demo](https://github.com/pimcore/demo/blob/11.x/config/ecommerce/base- Following steps are necessary to add additional custom properties to cart items: -1) Extend `CartItem` implementation and extend it with your custom property fields and getters/setters. -2) Extend `Cart` implementation and make use your custom `CartItem` implementation is used. -3) Provide the custom properties as key-value pairs in `params` parameter in `addItem` and `updateItem` of `Cart` or `CartManager`. +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`