Skip to content
This repository was archived by the owner on Apr 20, 2026. It is now read-only.

Commit 7fd7dfc

Browse files
Daniel Blaichingerblanksefashxp
authored
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 <sebastian.bl@gmx.de> * Remove method declaration from CartItemInterface * adding some docs * Added more details to 11_Cart_Manager.md --------- Co-authored-by: Sebastian Blank <sebastian.bl@gmx.de> Co-authored-by: Christian Fasching <fashxp@users.noreply.github.com>
1 parent 75fbf82 commit 7fd7dfc

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

doc/11_Cart_Manager.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,15 @@ Once set, the cart manager uses all specific settings of the currently active ch
178178
in the configuration (identified by tenant name).
179179

180180
See also [Demo](https://github.com/pimcore/demo/blob/11.x/config/ecommerce/base-ecommerce.yaml#L197) for some examples.
181+
182+
183+
## Adding Custom Properties to Cart Items
184+
185+
Following steps are necessary to add additional custom properties to cart items:
186+
187+
1) Extend `Pimcore\Bundle\EcommerceFrameworkBundle\CartManager\CartItem` implementation and add your custom properties including getters/setters.
188+
2) Extend `Cart::getCartItemClassName` implementation and make sure your custom `CartItem` implementation gets returned.
189+
3) Provide the custom properties as key-value pairs in `$params` parameter in the following methods:
190+
1) `Pimcore\Bundle\EcommerceFrameworkBundle\CartManager\AbstractCart::addItem`
191+
2) `Pimcore\Bundle\EcommerceFrameworkBundle\CartManager\AbstractCart::updateItem`
192+
3) `Pimcore\Bundle\EcommerceFrameworkBundle\CartManager\CartManagerInterface::addToCart`

src/CartManager/AbstractCart.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ public function updateItem(string $itemKey, CheckoutableInterface $product, int
158158
$item->setSubItems($subItems);
159159
}
160160

161+
if (method_exists($item, 'setCustomProperties')) {
162+
$item->setCustomProperties($params);
163+
}
164+
161165
$this->items[$itemKey] = $item;
162166

163167
// trigger cart has been modified

src/CartManager/AbstractCartItem.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,20 @@ public function setIsLoading(bool $isLoading): void
298298
{
299299
$this->isLoading = $isLoading;
300300
}
301+
302+
/**
303+
* Sets custom properties to CartItem when provided in AbstractCart::addItem
304+
*
305+
* @param array $params
306+
* @return void
307+
*/
308+
public function setCustomProperties(array $params): void
309+
{
310+
foreach ($params as $key => $value) {
311+
$method = 'set' . ucfirst($key);
312+
if (method_exists($this, $method)) {
313+
$this->{$method}($value);
314+
}
315+
}
316+
}
301317
}

0 commit comments

Comments
 (0)