Provide a checkout pane for customer comments

Created on 14 August 2024, about 1 month ago
Updated 6 September 2024, 11 days ago

Describe your bug or feature request.

We currently have no checkout pane for allowing the customer to add comments. Work was done as part of Add customer-facing order comments Active but due to the amount of changes introduced, I prefer to work on a simpler version focusing on the checkout pane only for now.

The checkout pane should be disabled by default (we can use customer_comments as its ID, just like in the issue linked), we should leverage the commerce_log module, but only record a log on checkout completion (Or we can use the existing order subscriber from commerce_log, reacting to the place post transition). Then we can have an event subscriber in commerce_log checking that the order data key is not empty and log a message to the activity log.

Additionally, we need changes to the order receipt to include the comments so in short we need:

  1. A new log template
  2. A new order base field "customer_comments"
  3. A new checkout pane (ID: "customer_comments"), message should be stored in the order data array.
  4. An event subscriber in commerce_log (preferably reacting to checkout completion, since that happens prior to the order place post transition, and the place post transition is used for sending the receipt. We can either do that, or have the log subscriber run before the order receipt subscriber.
Feature request
Status

Fixed

Version

3.0

Component

Checkout

Created by

🇮🇱Israel jsacksick

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

Merge Requests

Comments & Activities

  • Issue created by @jsacksick
  • 🇺🇸United States rszrama

    Agreed re: reacting to checkout completion; makes more logical sense, too.

    Let's also remember to have the event subscriber delete the message out of the order data array when it's transferred... unless, would it be easier to keep it in the data array to also be able to define an order comments token / theme template variable to make it easier to include in emails and user interfaces?

  • 🇮🇱Israel jsacksick

    The token could use the generated logs, but wondering whether storing the comment in an actual separate field doesn't make this easier?
    That makes it less custom, and easier to print in Twig template without additional magic / logic required...

    But not 100% sure the field is the right solution either, it does simplify the code / logic for sure.

  • 🇮🇱Israel jsacksick

    I initially extracted code from Add customer-facing order comments Active , but I'm having second thoughts about the approach.
    With the current approach, there is no way to have customer comments in checkout without the commerce_log module enabled... I don't think we must require commerce_log for that?

    The checkout pane is provided by the commerce_log module. Then, if we want to include the comments in the order receipt, we need to have conditional logic based on whether the commerce_log module is installed...

    There was a method added to the OrderReceiptMail:

      /**
       * Get customer viewable order comments.
       *
       * @param \Drupal\commerce_order\Entity\OrderInterface $order
       *   The order entity.
       *
       * @return array
       *   The order comments.
       */
      public function getOrderComments(OrderInterface $order): array {
        $orderComments = [];
        if (\Drupal::moduleHandler()->moduleExists('commerce_log')) {
          $logStorage = \Drupal::entityTypeManager()->getStorage('commerce_log');
          /** @var \Drupal\commerce_log\Entity\LogInterface $logEntry */
          foreach (array_reverse($logStorage->loadMultipleByEntity($order)) as $logEntry) {
            if ($logEntry->getTemplateId() === 'commerce_order_to_customer_comment') {
              $orderComments[$logEntry->id()]['type'] = OrderCommentEvent::ORDER_TO_CUSTOMER_COMMENT;
              $orderComments[$logEntry->id()]['date'] = $logEntry->getCreatedTime();
              $orderComments[$logEntry->id()]['comment'] = $logEntry->getParams()['comment'];
            }<ol>
      <li></li>
    </ol>
            elseif ($logEntry->getTemplateId() === 'commerce_order_from_customer_comment') {
              $orderComments[$logEntry->id()]['type'] = OrderCommentEvent::ORDER_FROM_CUSTOMER_COMMENT;
              $orderComments[$logEntry->id()]['date'] = $logEntry->getCreatedTime();
              $orderComments[$logEntry->id()]['comment'] = $logEntry->getParams()['comment'];
            }
          }
        }
    
        return $orderComments;
      }

    I don't think we have to do it this way, we could implement a hook_preprocess_HOOK() from the commerce_log module.

    So I think we have several options:

    1. Perhaps we should move the checkout pane to the checkout module?
    2. The order receipt is looping on order comments, but if we have a single pane and ignore admin comments, I don't think this is necessary and we should just focus on the order comments entered in checkout?
    3. Maybe we should define an actual base field for the customer comments after all? My problem with the current approach is that the order receipt is updated only if the commerce_log module is enabled, so if I define the checkout pane from the checkout module, then there is an inconsistency, we may expose a pane and not do anything with the comment entered...
  • 🇮🇱Israel jsacksick

    Considering this is a checkout feature, I think the checkout module should be responsible for providing it, so I'm thinking of:

    • A new base field provided by the checkout module ("checkout_comments")
    • Commerce log could still record a log on checkout completion
    • The pane manipulates the field
    • The order receipt simply outputs the field value? Though the patch from Add customer-facing order comments Active also includes a date, which wouldn't be possible if we don't rely on the logs for the order receipt...

    Thoughts?

  • Pipeline finished with Canceled
    26 days ago
    #260320
  • Status changed to Needs review 26 days ago
  • 🇮🇱Israel jsacksick

    After discussing this with @rszrama on Slack, decided to implement the original approach.
    The MR is a simplified version of the changes from Add customer-facing order comments Active with several adjustments:

    1. No "to customer" log template
    2. The order receipt simply outputs the order_comments variable, the HTML is coming from a new template/theme function "commerce_log_order_receipt_customer_comments" provided by the commerce_log module
    3. I removed all the commerce_log code that was added to the OrderReceipt email, all that logic now lives in a preprocess within the commerce_log module (commerce_log_preprocess_commerce_order_receipt)
    4. There is no custom event, no custom permission.

    The MR focuses on the checkout pane, generating the log on checkout completion as well as outputting the customer comments in the order receipt (see the attached screenshot).

    In the event we're adding "to customer" comments, the current approach might not be flexible enough but I'm not sure (especially the template part).
    I removed the log date from the order receipt template, and I'm not looping on logs since there is supposed to be only one customer comment.

    At the same time, and maybe I'm missing something, I don't really understand in which context an admin would leave comments to a customer from the admin, PRIOR to the order being placed?

  • Pipeline finished with Success
    26 days ago
    Total: 553s
    #260322
  • 🇮🇱Israel jsacksick

    Crediting

  • Pipeline finished with Success
    26 days ago
    Total: 727s
    #260331
  • 🇮🇱Israel jsacksick

    Actually, after rediscussing this with @rszrama, it might make more sense to implement a solution that is more aligned with my comment #9.

  • Status changed to Needs work 26 days ago
  • 🇺🇸United States rszrama

    Yeah, really sorry for jerking you around on this. The maximum utility approach is actually to store the value in a field that can be more easily embedded / used in other contexts. I'd propose customer_comments provided by commerce_order, with the Checkout module providing a checkout pane for populating this field and an event subscriber for recording the contents of this field at checkout completion to the order log. This provides some auditability while still permitting the field to be used for other purposes.

    If a headless site then wants to populate this field through some other means, or if the field is made editable or whatever, then those use cases would be responsible for their own event subscribers that write the contents of the field to the log at their own consequential times.

    Additionally, since we're still copying this into the activity stream, we can still also use it in concert with some theoretical log message emailing system in the future, in a view of comment related logs, etc.

  • It looks like the same issue tries to fix 2 separate problems.

    1. The one that jsacksick talked about in this issue, to allow customer to leave a note while checking out. It's a one time thing.

    2. Another one is to use to communicate with customer via Log / Email. Let's say if I would like to notify customer that this item is out of stock, I can just send an email to customer from the order page. I also have a log to check if we already send something to customer. Also, it's the way for store admin to send to customer, not the other way around.

    Actually, I also prefer the way Ubercart works, it divides comments into 2 types order comment and admin comment. That way, we can know exactly which information we send to customer, which one is only note between store admin.

    I created an issue long time ago for the second problem here (To Compare what other E-Commerce has but not in Drupal Commerce) -> https://www.drupal.org/project/commerce/issues/3166853 💬 How to Add Order Comment and also Allow to notify customer via email? Active

  • 🇮🇱Israel jsacksick

    The second problem isn't addressed here, the point of this issue is to focus on customer comments, left during checkout.
    The goal was to split the changes into smaller chunks so this can more easily get in.

  • Thanks jsacksick, I forgot to read the title.. Just followed from the old issue and think that this tries to fix that old issue.

  • 🇮🇱Israel jsacksick

    Wanted to point out, after discussing this internally, that an alternative to this would be to use https://www.drupal.org/project/commerce_checkout_order_fields .

  • Pipeline finished with Canceled
    25 days ago
    Total: 202s
    #261377
  • Status changed to Needs review 25 days ago
  • 🇮🇱Israel jsacksick

    Ok, we now have a base "customer_comments" field, it is used in the order receipt. The log module is still logging the message in the activity log. The pane is provided by commerce_checkout, and the field can be added to the order view (it's currently output under "Other").

    I'm wondering if this should be 3.x only or whether I should backport this change to 2.x, this affects the update hook number ofc, so we have to make a decision.

  • Pipeline finished with Success
    25 days ago
    Total: 493s
    #261385
  • 🇮🇱Israel jsacksick

    @rszrama: Last thing... Should we use plural or singular? "customer_comments" might suggest the field cardinality is multiple...

    So the question is: "customer_comment" vs "customer_comments", for the field / checkout pane ID as well as the order receipt.

  • Pipeline finished with Skipped
    25 days ago
    #261682
  • Pipeline finished with Skipped
    25 days ago
    #261683
    • jsacksick committed 042e8f1d on 3.0.x
      Issue #3468080 by ayalon, khiminrm, Lukas von Blarer, vipin.j, Morbus...
    • jsacksick committed e459cb3c on 8.x-2.x
      Issue #3468080 by ayalon, khiminrm, Lukas von Blarer, vipin.j, Morbus...
  • Status changed to Fixed 25 days ago
  • 🇺🇸United States Morbus Iff

    @jsacksick: Does this contain the ability for an admin to communicate back to the customer via comments? (I've been, and still remain, on a holiday as I'm moving doing the college move-in thing for a kid, so haven't had a chance to follow along with this recent flurry of dev.)

  • 🇮🇱Israel jsacksick

    @morbu iff: Yes only customer comments in checkout, and the comment is logged in the activity log. I tried simplifying this as much as possible so we can get at least part of the functionality in. The other issue kind of derailed with events / templates as well as commerce_log code in the multiple modules (OrderReceipt fetching logs etc etc)... The other patch had multiple issues.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024