You are here

public function MoveToCart::viewsFormSubmit in Commerce Wishlist 8.3

Submit handler for the views form.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Throws

\Exception When the call to self::selectStore() throws an exception because the entity can't be purchased from the current store.

File

src/Plugin/views/field/MoveToCart.php, line 209

Class

MoveToCart
Defines a form element for moving or copying the wishlist item to the cart.

Namespace

Drupal\commerce_wishlist\Plugin\views\field

Code

public function viewsFormSubmit(array &$form, FormStateInterface $form_state) {
  $triggering_element = $form_state
    ->getTriggeringElement();
  if (!empty($triggering_element['#move_wishlist_item'])) {
    $row_index = $triggering_element['#row_index'];

    /** @var \Drupal\commerce_wishlist\Entity\WishlistItemInterface $wishlist_item */
    $wishlist_item = $this
      ->getEntity($this->view->result[$row_index]);
    $purchased_entity = $wishlist_item
      ->getPurchasableEntity();
    $order_item = $this->cartManager
      ->createOrderItem($purchased_entity, $wishlist_item
      ->getQuantity());
    $order_type = $this->orderTypeResolver
      ->resolve($order_item);
    $store = $this
      ->selectStore($purchased_entity);
    $cart = $this->cartProvider
      ->getCart($order_type, $store);
    if (!$cart) {
      $cart = $this->cartProvider
        ->createCart($order_type, $store);
    }
    $this->cartManager
      ->addOrderItem($cart, $order_item, $this->options['combine']);
    if (!$this->options['keep_item']) {
      $wishlist = $wishlist_item
        ->getWishlist();
      $wishlist
        ->removeItem($wishlist_item);
      $wishlist
        ->save();
      $wishlist_item
        ->delete();
    }
  }
}