Cart view event triggered on every single page

Created on 12 March 2024, 7 months ago
Updated 10 April 2024, 6 months ago

Problem/Motivation

Looking at the code, and by debugging, it seems that a cart view event is triggered in a KernelEvents::REQUEST event subscriber without any checks on whether the cart is actually viewed. The only check is that a cart exists for the current store. Is this intentional? This adds an event, and on many pages multiple events, on all pages on an application.

Proposed resolution

Add an event only on the cart page.

If applications want an event to be added when the cart block is rendered, that should be done in a different way. Maybe provide a block that does that? Many websites might not have the cart block on all pages, it may not be expandable to view the details of the cart, or in any case it might not be desirable to count that as a cart view. For most websites this would actually render the cart view events meaningless if an event is added on all page views.

🐛 Bug report
Status

Active

Version

2.0

Component

Miscellaneous

Created by

🇵🇪Peru krystalcode

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @krystalcode
  • 🇵🇪Peru krystalcode

    I suggest adding the following at the top of the CommerceEventsSubscriber::trackCartView() method, loosely taken from KernelEventsSubscriber::shouldTrackCheckout().

    if ($this->routeMatch->getRouteName() !== 'commerce_cart.page') {
      return;
    }
    
    // Bail if we are not dealing with a master request or GET method.
    if (!$event->isMasterRequest() || !$event->getRequest()->isMethod('GET')) {
      return;
    }
    
  • First commit to issue fork.
Production build 0.71.5 2024