You are here

public function AddToWishlistController::action in Commerce Add To Cart Link 8

Same name and namespace in other branches
  1. 2.x 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 89

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);
  return $this
    ->redirect('commerce_wishlist.page');
}