You are here

protected function WishlistUserForm::addItemToCart in Commerce Wishlist 8.3

Adds a wishlist item to the cart.

Parameters

\Drupal\commerce_wishlist\Entity\WishlistItemInterface $wishlist_item: The wishlist item to move to the cart.

2 calls to WishlistUserForm::addItemToCart()
WishlistUserForm::addToCartSubmit in src/Form/WishlistUserForm.php
Submit callback for the "Add to cart" button.
WishlistUserForm::submitForm in src/Form/WishlistUserForm.php
This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state…

File

src/Form/WishlistUserForm.php, line 385

Class

WishlistUserForm
Provides the wishlist user form.

Namespace

Drupal\commerce_wishlist\Form

Code

protected function addItemToCart(WishlistItemInterface $wishlist_item) {
  $purchasable_entity = $wishlist_item
    ->getPurchasableEntity();

  /** @var \Drupal\commerce_order\OrderItemStorageInterface $order_item_storage */
  $order_item_storage = $this->entityTypeManager
    ->getStorage('commerce_order_item');
  $values = [
    'quantity' => $wishlist_item
      ->getQuantity(),
  ];
  $order_item = $order_item_storage
    ->createFromPurchasableEntity($purchasable_entity, $values);
  $order_type_id = $this->orderTypeResolver
    ->resolve($order_item);
  $store = $this
    ->selectStore($purchasable_entity);
  $cart = $this->cartProvider
    ->getCart($order_type_id, $store);
  if (!$order_item
    ->isUnitPriceOverridden()) {
    $context = new Context($this->currentUser, $store);
    $resolved_price = $this->chainPriceResolver
      ->resolve($purchasable_entity, $order_item
      ->getQuantity(), $context);
    $order_item
      ->setUnitPrice($resolved_price);
  }
  if (!$cart) {
    $cart = $this->cartProvider
      ->createCart($order_type_id, $store);
  }
  $this->cartManager
    ->addOrderItem($cart, $order_item, TRUE);
}