Skip to content

Commit

Permalink
Add possibility to set custom properties to cart item (#143)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Feb 5, 2024
1 parent 75fbf82 commit 7fd7dfc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
12 changes: 12 additions & 0 deletions doc/11_Cart_Manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
4 changes: 4 additions & 0 deletions src/CartManager/AbstractCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/CartManager/AbstractCartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}

0 comments on commit 7fd7dfc

Please sign in to comment.