You are here

public function AddToWishlistController::action in Commerce Add To Cart Link 2.x

Same name and namespace in other branches
  1. 8 modules/wishlist/src/Controller/AddToWishlistController.php \Drupal\commerce_add_to_wishlist_link\Controller\AddToWishlistController::action()

Performs the add to wishlist action and redirects to wishlist.

Parameters

\Drupal\commerce_product\Entity\ProductInterface $commerce_product: The product entity.

\Drupal\commerce_product\Entity\ProductVariationInterface $commerce_product_variation: The product variation to add.

string $token: The CSRF token.

\Symfony\Component\HttpFoundation\Request $request: The request.

Return value

\Symfony\Component\HttpFoundation\RedirectResponse A redirect to the wishlist after adding the product.

Throws

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

1 string reference to 'AddToWishlistController::action'
commerce_add_to_wishlist_link.routing.yml in modules/wishlist/commerce_add_to_wishlist_link.routing.yml
modules/wishlist/commerce_add_to_wishlist_link.routing.yml

File

modules/wishlist/src/Controller/AddToWishlistController.php, line 102

Class

AddToWishlistController
Defines the add to wishlist controller.

Namespace

Drupal\commerce_add_to_wishlist_link\Controller

Code

public function action(ProductInterface $commerce_product, ProductVariationInterface $commerce_product_variation, $token, Request $request) {
  $quantity = $request->query
    ->get('quantity', 1);

  // Determine the wishlist type to use.
  $wishlist_type = $this
    ->config('commerce_wishlist.settings')
    ->get('default_type') ?: 'default';
  $wishlist = $this->wishlistProvider
    ->getWishlist($wishlist_type);
  if (!$wishlist) {
    $wishlist = $this->wishlistProvider
      ->createWishlist($wishlist_type);
  }
  $this->wishlistManager
    ->addEntity($wishlist, $commerce_product_variation, $quantity);
  if ($this
    ->config('commerce_add_to_cart_link.settings')
    ->get('redirect_back')) {
    $referer = $request->server
      ->get('HTTP_REFERER');
    $fake_request = Request::create($referer);
    $referer_url = $this->pathValidator
      ->getUrlIfValid($fake_request
      ->getRequestUri());
    if ($referer_url && $referer_url
      ->isRouted() && $referer_url
      ->getRouteName() !== 'user.login') {
      $referer_url
        ->setAbsolute();
      return new RedirectResponse($referer_url
        ->toString());
    }
  }
  return $this
    ->redirect('commerce_wishlist.page');
}