You are here

class CommerceCartRedirectionSubscriber in Commerce Cart Redirection 8.2

Same name and namespace in other branches
  1. 3.0.x src/EventSubscriber/CommerceCartRedirectionSubscriber.php \Drupal\commerce_cart_redirection\EventSubscriber\CommerceCartRedirectionSubscriber

Hierarchy

Expanded class hierarchy of CommerceCartRedirectionSubscriber

1 string reference to 'CommerceCartRedirectionSubscriber'
commerce_cart_redirection.services.yml in ./commerce_cart_redirection.services.yml
commerce_cart_redirection.services.yml
1 service uses CommerceCartRedirectionSubscriber
commerce_cart_redirection.cart_event_subscriber in ./commerce_cart_redirection.services.yml
Drupal\commerce_cart_redirection\EventSubscriber\CommerceCartRedirectionSubscriber

File

src/EventSubscriber/CommerceCartRedirectionSubscriber.php, line 15

Namespace

Drupal\commerce_cart_redirection\EventSubscriber
View source
class CommerceCartRedirectionSubscriber implements EventSubscriberInterface {

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * CartEventSubscriber constructor.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   */
  public function __construct(RequestStack $request_stack) {
    $this->requestStack = $request_stack;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [
      CartEvents::CART_ENTITY_ADD => 'tryRedirectToCheckout',
      KernelEvents::RESPONSE => [
        'checkRedirectIssued',
        -10,
      ],
    ];
    return $events;
  }

  /**
   * Conditionally skip cart and send user to checkout.
   *
   * If the added product meets criteria set in the config form then redirect.
   *
   * @param \Drupal\commerce_cart\Event\CartEntityAddEvent $event
   *   The add to cart event.
   */
  public function tryRedirectToCheckout(CartEntityAddEvent $event) {
    $redirect = FALSE;
    $purchased_entity = $event
      ->getEntity();

    /** @var \Drupal\Core\Config\Config $config */
    $config = \Drupal::config('commerce_cart_redirection.settings');
    $active_bundles = $config
      ->get('product_bundles');
    $negate = $config
      ->get('negate_product_bundles');
    $purchased_bundle = $purchased_entity
      ->bundle();
    if (isset($active_bundles[$purchased_bundle]) && $active_bundles[$purchased_bundle] !== 0) {
      if (!$negate) {
        $redirect = TRUE;
      }
    }
    else {
      if ($negate) {
        $redirect = TRUE;
      }
    }
    if ($redirect) {

      // Set a default so we always end up somewhere useful.
      $redirection_url = Url::fromRoute('commerce_checkout.form', [
        'commerce_order' => $event
          ->getCart()
          ->id(),
      ])
        ->toString();
      switch ($config
        ->get('redirection_route_path')) {
        case 'other':

          // @TODO add some sort of sanity checking.
          if (!empty($config
            ->get('redirection_route_path_other')) && UrlHelper::isValid($config
            ->get('redirection_route_path_other'))) {
            $redirection_url = $config
              ->get('redirection_route_path_other');
          }
          break;
        case 'cart':
          $redirection_url = Url::fromRoute('commerce_cart.page')
            ->toString();
          break;
        case 'checkout':
        default:
      }
      $this->requestStack
        ->getCurrentRequest()->attributes
        ->set('commerce_cart_redirection_url', $redirection_url);
    }
  }

  /**
   * Checks if a redirect url has been set.
   *
   * Redirects to the provided url if there is one.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The response event.
   */
  public function checkRedirectIssued(FilterResponseEvent $event) {
    $request = $event
      ->getRequest();
    $redirect_url = $request->attributes
      ->get('commerce_cart_redirection_url');
    if (isset($redirect_url)) {
      $event
        ->setResponse(new RedirectResponse($redirect_url));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommerceCartRedirectionSubscriber::$requestStack protected property The request stack.
CommerceCartRedirectionSubscriber::checkRedirectIssued public function Checks if a redirect url has been set.
CommerceCartRedirectionSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
CommerceCartRedirectionSubscriber::tryRedirectToCheckout public function Conditionally skip cart and send user to checkout.
CommerceCartRedirectionSubscriber::__construct public function CartEventSubscriber constructor.