You are here

function addToCart in UC Wish List 8

Submit callback handler for wishlistviewform add to cart action.

1 string reference to 'addToCart'
WishlistViewForm::buildForm in src/Form/WishlistViewForm.php
Form constructor.

File

./uc_wishlist.module, line 409
Allows users to create public shopping/wish lists.

Code

function addToCart(array &$form, FormState $form_state) {

  // $form_state->get();
  $submitButton = $form_state
    ->getTriggeringElement()['#name'];
  $buttonName = $submitButton;

  // The add to cart button was pressed on a product in the wish list
  // explode the button name to get the product id.
  $names = explode('-', $buttonName);
  $pid = $names[1];
  $values = $form_state
    ->getValues();
  $nid = $values['items'][$pid]['nid'];
  $qty = $values['items'][$pid]['qty'];
  $data = [
    'nid' => $nid,
    'qty' => $qty,
  ];
  $data = \Drupal::moduleHandler()
    ->invokeAll('uc_add_to_cart_data', [
    $data,
  ]);
  $msg = TRUE;
  $cart = \Drupal::service('uc_cart.manager')
    ->get();
  $redirect = $cart
    ->addItem($nid, $qty, $data, $msg);
  $form_state
    ->set('variant', uc_product_load_variant($qty, $data));
  if (isset($redirect)) {
    $form_state
      ->setRedirectUrl($redirect);
  }
}