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 681170fad0eefe8c5d054d9d09889d58e6bfe5eb Mon Sep 17 00:00:00 2001 From: Daniel Blaichinger Date: Sat, 16 Dec 2023 11:31:12 +0100 Subject: [PATCH 5/6] Add setCustomerProperties declaration back to 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 17c3de49c..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 (method_exists($item, 'setCustomProperties')) { - $item->setCustomProperties($params); - } + $item->setCustomProperties($params); $this->items[$itemKey] = $item; 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 42407d9a74787f9d2446516994447574ab71d8e0 Mon Sep 17 00:00:00 2001 From: Daniel Blaichinger Date: Fri, 2 Feb 2024 16:07:50 +0100 Subject: [PATCH 6/6] Added array $customProperties to AbstractCartItem and renamed outdated $params variable --- src/CartManager/AbstractCart.php | 12 ++++++------ src/CartManager/AbstractCartItem.php | 21 +++++++++++++-------- src/CartManager/CartInterface.php | 8 ++++---- src/CartManager/CartItemInterface.php | 7 ++++++- src/CartManager/CartManagerInterface.php | 4 ++-- src/CartManager/MultiCartManager.php | 6 +++--- 6 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/CartManager/AbstractCart.php b/src/CartManager/AbstractCart.php index f3ba0f526..87bd9cf0f 100644 --- a/src/CartManager/AbstractCart.php +++ b/src/CartManager/AbstractCart.php @@ -84,13 +84,13 @@ abstract protected function getCartCheckoutDataClassName(): string; * @param int $count * @param string|null $itemKey * @param bool $replace - * @param array $params + * @param array $customProperties * @param AbstractSetProductEntry[] $subProducts * @param string|null $comment * * @return string */ - public function addItem(CheckoutableInterface $product, int $count, string $itemKey = null, bool $replace = false, array $params = [], array $subProducts = [], string $comment = null): string + public function addItem(CheckoutableInterface $product, int $count, string $itemKey = null, bool $replace = false, array $customProperties = [], array $subProducts = [], string $comment = null): string { if (empty($itemKey)) { $itemKey = (string) $product->getId(); @@ -100,7 +100,7 @@ public function addItem(CheckoutableInterface $product, int $count, string $item } } - return $this->updateItem($itemKey, $product, $count, $replace, $params, $subProducts, $comment); + return $this->updateItem($itemKey, $product, $count, $replace, $customProperties, $subProducts, $comment); } /** @@ -108,13 +108,13 @@ public function addItem(CheckoutableInterface $product, int $count, string $item * @param CheckoutableInterface&Concrete $product * @param int $count * @param bool $replace - * @param array $params + * @param array $customProperties * @param AbstractSetProductEntry[] $subProducts * @param string|null $comment * * @return string */ - public function updateItem(string $itemKey, CheckoutableInterface $product, int $count, bool $replace = false, array $params = [], array $subProducts = [], string $comment = null): string + public function updateItem(string $itemKey, CheckoutableInterface $product, int $count, bool $replace = false, array $customProperties = [], array $subProducts = [], string $comment = null): string { //load items first in order to lazyload items (if they are lazy loaded) $this->getItems(); @@ -158,7 +158,7 @@ public function updateItem(string $itemKey, CheckoutableInterface $product, int $item->setSubItems($subItems); } - $item->setCustomProperties($params); + $item->setCustomProperties($customProperties); $this->items[$itemKey] = $item; diff --git a/src/CartManager/AbstractCartItem.php b/src/CartManager/AbstractCartItem.php index 884ea1d38..d0d7f2c15 100644 --- a/src/CartManager/AbstractCartItem.php +++ b/src/CartManager/AbstractCartItem.php @@ -57,6 +57,8 @@ abstract class AbstractCartItem extends \Pimcore\Model\AbstractModel implements */ protected ?int $addedDateTimestamp = null; + protected array $customProperties = []; + public function __construct() { $this->setAddedDate(new \DateTime()); @@ -302,16 +304,19 @@ public function setIsLoading(bool $isLoading): void /** * Sets custom properties to CartItem when provided in AbstractCart::addItem * - * @param array $params + * @param array $customProperties * @return void */ - public function setCustomProperties(array $params): void + public function setCustomProperties(array $customProperties): void { - foreach ($params as $key => $value) { - $method = 'set' . ucfirst($key); - if (method_exists($this, $method)) { - $this->{$method}($value); - } - } + $this->customProperties = $customProperties; + } + + /** + * @return array + */ + public function getCustomProperties(): array + { + return $this->customProperties; } } diff --git a/src/CartManager/CartInterface.php b/src/CartManager/CartInterface.php index 9bb17f1bb..5c5027976 100644 --- a/src/CartManager/CartInterface.php +++ b/src/CartManager/CartInterface.php @@ -70,26 +70,26 @@ public function getGiftItem(string $itemKey): ?CartItemInterface; * @param int $count * @param string|null $itemKey * @param bool $replace replace if item with same key exists - * @param array $params optional additional item information + * @param array $customProperties optional additional item information * @param AbstractSetProductEntry[] $subProducts * @param string|null $comment * * @return string $itemKey */ - public function addItem(CheckoutableInterface $product, int $count, string $itemKey = null, bool $replace = false, array $params = [], array $subProducts = [], string $comment = null): string; + public function addItem(CheckoutableInterface $product, int $count, string $itemKey = null, bool $replace = false, array $customProperties = [], array $subProducts = [], string $comment = null): string; /** * @param string $itemKey * @param CheckoutableInterface $product * @param int $count * @param bool $replace replace if item with same key exists - * @param array $params optional additional item information + * @param array $customProperties optional additional item information * @param array $subProducts * @param string|null $comment * * @return string $itemKey */ - public function updateItem(string $itemKey, CheckoutableInterface $product, int $count, bool $replace = false, array $params = [], array $subProducts = [], string $comment = null): string; + public function updateItem(string $itemKey, CheckoutableInterface $product, int $count, bool $replace = false, array $customProperties = [], array $subProducts = [], string $comment = null): string; /** * updates count of specific cart item diff --git a/src/CartManager/CartItemInterface.php b/src/CartManager/CartItemInterface.php index d352ba825..8232a07c6 100644 --- a/src/CartManager/CartItemInterface.php +++ b/src/CartManager/CartItemInterface.php @@ -113,5 +113,10 @@ public function setAddedDateTimestamp(int $time): void; */ public function getName(): string; - public function setCustomProperties(array $params): void; + /** + * @return array + */ + public function getCustomProperties(): array; + + public function setCustomProperties(array $customProperties): void; } diff --git a/src/CartManager/CartManagerInterface.php b/src/CartManager/CartManagerInterface.php index 97bf4dadd..cb984c40f 100644 --- a/src/CartManager/CartManagerInterface.php +++ b/src/CartManager/CartManagerInterface.php @@ -40,7 +40,7 @@ public function getCartClassName(): string; * @param string|null $key - optional key of cart where the item should be added to * @param string|null $itemKey - optional item key * @param bool $replace - replace item if same key already exists - * @param array $params - optional addtional item information + * @param array $customProperties - optional additional item information * @param AbstractSetProductEntry[] $subProducts * @param string|null $comment * @@ -52,7 +52,7 @@ public function addToCart( string $key = null, string $itemKey = null, bool $replace = false, - array $params = [], + array $customProperties = [], array $subProducts = [], string $comment = null ): string; diff --git a/src/CartManager/MultiCartManager.php b/src/CartManager/MultiCartManager.php index bab0c63ee..f426da11c 100644 --- a/src/CartManager/MultiCartManager.php +++ b/src/CartManager/MultiCartManager.php @@ -116,7 +116,7 @@ protected function getAllCartsForCurrentUser(): ?array * @param string|null $key * @param string|null $itemKey * @param bool $replace - * @param array $params + * @param array $customProperties * @param array $subProducts * @param string|null $comment * @@ -130,7 +130,7 @@ public function addToCart( string $key = null, string $itemKey = null, bool $replace = false, - array $params = [], + array $customProperties = [], array $subProducts = [], string $comment = null ): string { @@ -142,7 +142,7 @@ public function addToCart( $cart = $this->carts[$key]; - $itemKey = $cart->addItem($product, $count, $itemKey, $replace, $params, $subProducts, $comment); + $itemKey = $cart->addItem($product, $count, $itemKey, $replace, $customProperties, $subProducts, $comment); $this->save(); return $itemKey;