In mailchimp_ecommerce/modules/mailchimp_ecommerce_commerce/src/EventSubscriber/CartEventSubscriber.php, we have:

  public function cartAdd(CartEntityAddEvent $event) {
    /** @var \Drupal\commerce_order\Entity\Order $order */
    $order = $event->getCart();

    $customer['email_address'] = $order->getEmail();

    if (empty($customer['email_address'])) {
      // Cannot create or add an item to a cart with no customer email address.
      return;
    }

Which checks if there is a customer email before doing any cart / order handling. However, there is no check in cartItemUpdate() nor cartItemRemove(). This results in an error when an anonymous shopper views the cart and updates a quantity or removes an item.

Adding:

    $customer['email_address'] = $order->getEmail();

    if (empty($customer['email_address'])) {
      // Cannot create or add an item to a cart with no customer email address.
      return;
    }

To each function after $order = $event->getCart(); eliminates the error. I'll see if I can create a quick patch.

Comments

scottsawyer created an issue. See original summary.

scottsawyer’s picture

quick patch, let's see how this does.

scottsawyer’s picture

Status: Active » Needs review
dench0’s picture

I having the same issue, but I'm thinking better to check if cart exist, then existence of the customer e-mail. Because theoretically possible situation when customer e-mail exist, but cart not exist at mailchimp. Attached new patch.

scottsawyer’s picture

@dench0, seems reasonable to me.

samuel.mortenson’s picture

The solution in #4 will now make two API calls when updating or deleting cart items instead of one - are there performance considerations here?

dench0’s picture

@samuel.mortenson yes, but otherwise it impossible to be sure that cart exist. Need to use #4, or #2 but inside try/catch block. Now it just WSOD on any cart updates, and theoretically for #2 also possible this situation.

travis-bradbury’s picture

For performance, it may be better to try updating and handle the special case of the cart not existing only if necessary ("Easier to ask forgiveness than it is to get permission.").

Additionally - and this is probably out of scope of this particular issue - these things should happen after pages have been delivered to the end user so customers aren't waiting on API calls to Mailchimp. See https://glamanate.com/blog/flush-and-run-using-kernelterminate-improve-p..., and https://api.drupal.org/api/drupal/core!lib!Drupal!Core!DestructableInter..., which is what you should use instead of the Kernel::TERMINATE event. DestructableInterface is designed for this. I'll open a separate issue for this request.

xenophyle’s picture

Version: 8.x-1.x-dev » 2.x-dev
Status: Needs review » Closed (duplicate)