Skip to content

Commit

Permalink
Added array $customProperties to AbstractCartItem and renamed outdate…
Browse files Browse the repository at this point in the history
…d $params variable
  • Loading branch information
dblaichinger committed Feb 2, 2024
1 parent 681170f commit 42407d9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 24 deletions.
12 changes: 6 additions & 6 deletions src/CartManager/AbstractCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -100,21 +100,21 @@ 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);
}

/**
* @param string $itemKey
* @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();
Expand Down Expand Up @@ -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;

Expand Down
21 changes: 13 additions & 8 deletions src/CartManager/AbstractCartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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;
}
}
8 changes: 4 additions & 4 deletions src/CartManager/CartInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/CartManager/CartItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions src/CartManager/CartManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/CartManager/MultiCartManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit 42407d9

Please sign in to comment.