Fix issue: Error: Call to a member function hasItem() on null in Drupal\commerce_simple_stock\EventSubscriber\OrderEventSubscriber->onOrderUpdate()

Created on 18 November 2022, about 2 years ago
Updated 13 November 2023, about 1 year ago

Problem/Motivation

Fix the error: "Call to a member function hasItem() on null in Drupal\commerce_simple_stock\EventSubscriber\OrderEventSubscriber->onOrderUpdate()" when a new order gets updated.

Steps to reproduce

  1. Create a hook presave that sets the order's data, (e.g. $order->setData('sent_api_request', FALSE);) via \Drupal\commerce_order\Entity\Order::setData()
  2. Create an event subscriber for event: "commerce_order.place.post_transition" and update order when it's completed and paid in full using the function \Drupal\commerce_order\Entity\Order::setData(). (e.g. $order->setData('sent_api_request', TRUE); $order->save())
  3. Proceed to create and save an order via api/admin/page ui

Proposed resolution

There should be a logic that uses:
1. the order entity for new orders
2. the order->original entity for updated orders
on the \Drupal\commerce_simple_stock\EventSubscriber\OrderEventSubscriber::onOrderUpdate().

🐛 Bug report
Status

Needs review

Version

1.1

Component

Code

Created by

🇵🇭Philippines erom

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇪🇸Spain interdruper

    Patch #2 fixes the issue for me. Thanks.

  • Status changed to Needs work about 1 year ago
  • 🇺🇸United States loze Los Angeles

    This part is wrong.

    -
    +    if ($item->getOriginal == NULL || $item == NULL) {
    +      return;
    +    }
         if ($order && !in_array($order->getState()->value, ['draft', 'canceled'])) {
    

    $item->getOriginal is always going to return null. Im not sure what its trying to accomplish here.

  • 🇪🇸Spain interdruper

    @loze is right, that snippet always will be true, and the return will be executed.

    $item cannot be null (since it triggers the event), so the check just can be:

        if ($item->original) {
          return;
        }
        if ($order && !in_array($order->getState()->value, ['draft', 'canceled'])) {
    
          $diff = $item->original->getQuantity() - $item->getQuantity();
    

    for avoiding the warning if $item->Original is NULL.

    The code that truly fixes the issue error is the other snippet contained in the patch:

    @@ -158,7 +160,7 @@ class OrderEventSubscriber implements EventSubscriberInterface {
        */
       public function onOrderUpdate(OrderEvent $event) {
         $order = $event->getOrder();
    -    $original_order = $order->original;
    +    $original_order = empty($order->original) ? $order : $order->original;
         $allow_negative = \Drupal::config('commerce_simple_stock.settings')->get('allow_backorder');
     
         if ($original_order) {
    
  • Status changed to Needs review about 1 year ago
  • 🇮🇳India roshni27

    I have implemented the modifications as per issue #6. please review the patch.

Production build 0.71.5 2024