public function WishlistManager::addEntity in Commerce Wishlist 8.3
Adds the given purchasable entity to the given wishlist entity.
Parameters
\Drupal\commerce_wishlist\Entity\WishlistInterface $wishlist: The wishlist entity.
\Drupal\commerce\PurchasableEntityInterface $entity: The purchasable entity.
int $quantity: The quantity.
bool $combine: Whether the wishlist item should be combined with an existing matching one.
bool $save_wishlist: Whether the wishlist should be saved after the operation.
Return value
\Drupal\commerce_wishlist\Entity\WishlistItemInterface The saved wishlist item.
Overrides WishlistManagerInterface::addEntity
File
- src/
WishlistManager.php, line 69
Class
- WishlistManager
- Default implementation of the wishlist manager.
Namespace
Drupal\commerce_wishlistCode
public function addEntity(WishlistInterface $wishlist, PurchasableEntityInterface $entity, $quantity = 1, $combine = TRUE, $save_wishlist = TRUE) {
$wishlist_item = $this->wishlistItemStorage
->createFromPurchasableEntity($entity, [
'quantity' => $quantity,
]);
$purchasable_entity = $wishlist_item
->getPurchasableEntity();
$quantity = $wishlist_item
->getQuantity();
$matching_wishlist_item = NULL;
if ($combine) {
$matching_wishlist_item = $this
->matchWishlistItem($wishlist_item, $wishlist
->getItems());
}
if ($matching_wishlist_item) {
$new_quantity = Calculator::add($matching_wishlist_item
->getQuantity(), $quantity);
$matching_wishlist_item
->setQuantity($new_quantity);
$matching_wishlist_item
->save();
$saved_wishlist_item = $matching_wishlist_item;
}
else {
$wishlist_item
->save();
$wishlist
->addItem($wishlist_item);
$saved_wishlist_item = $wishlist_item;
}
$event = new WishlistEntityAddEvent($wishlist, $purchasable_entity, $quantity, $wishlist_item);
$this->eventDispatcher
->dispatch(WishlistEvents::WISHLIST_ENTITY_ADD, $event);
if ($save_wishlist) {
$wishlist
->save();
}
return $saved_wishlist_item;
}