getPurchasedEntity can return NULL and there can be error

Created on 4 July 2024, 7 months ago
Updated 10 September 2024, 4 months ago

Problem/Motivation

In some cases the order_item->getPurchasedEntity(); code can return NULL and produce the error:
Error: Call to a member function id() on null in Drupal\facebook_pixel_commerce\FacebookCommerce->getOrderItemData() (line 90 of /app/web/modules/contrib/facebook_pixel/modules/facebook_pixel_commerce/src/FacebookCommerce.php).

Proposed resolution

Add condition in FacebookCommerce::getOrderItemData() to check if the $entity = $order_item->getPurchasedEntity() is not NULL. Though I'm not sure if we should just return empty array if it's NULL or return all available data except those which are received from the $entity.

πŸ› Bug report
Status

Fixed

Version

2.0

Component

Code (Facebook Pixel Commerce)

Created by

πŸ‡ΊπŸ‡¦Ukraine khiminrm

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

Merge Requests

Comments & Activities

  • Issue created by @khiminrm
  • Status changed to Needs review 7 months ago
  • Status changed to Needs work 5 months ago
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    @khiminrm thanks, please turn this into a MR.

  • First commit to issue fork.
  • Status changed to Needs review 5 months ago
  • πŸ‡ΊπŸ‡ΈUnited States rhovland Oregon

    The actual problem is calling functions on $entity when it is null. Switching to using $order_item->getPurchasedEntityId() and $order_item->getTitle() handles the lack of a purchased entity gracefully and also works on non-product purchased entities, returning the name and entity id. getPurchasedEntityId() returns null instead of throwing an error if there is no purchased entity.

    I also moved the SKU code so it uses a null coalescing operator to determine if it should use the sku or the purchased entity id instead of overwriting values previously set.

  • πŸ‡ΊπŸ‡ΈUnited States rhovland Oregon

    I made a small change so it returns an empty string if the id is null

    It's a stacked null coalescing operator so if $sku is null then it uses $order_item->getPurchasedEntityId() and if that's also null it uses an empty string.

  • Pipeline finished with Success
    5 months ago
    Total: 140s
    #276201
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    @rhovland looking great!

    Just one last step to finish this: Is an empty string an accepted value for Facebook?
    'content_name' => $order_item->getTitle() will then still return the title, right? (Hopefully we can be sure there, it's not NULL :P)

  • πŸ‡ΊπŸ‡ΈUnited States rhovland Oregon

    According to the order item entity interface it will always return a string. Titles are required for order items in the UI, you cannot save one without it.

      /**
       * Gets the order item title.
       *
       * @return string
       *   The order item title
       */
      public function getTitle();
    
  • Status changed to RTBC 4 months ago
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    Thanks @rhovland, so let's merge this!

  • Pipeline finished with Skipped
    4 months ago
    #278899
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica
  • Status changed to Fixed 4 months ago
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024