Skip to content

Error when saving/cancelling an order containing a deleted product #69

Description

@kdonarski-ip

When cancelling an order that contains a product which has been removed from the catalog, the following error occurs:
Error: Call to a member function getId() on null in /var/www/html/vendor/getresponse/magento2/Api/OrderFactory.php:66

The code in OrderFactory, which is executed whenever an order is saved, assumes that the product associated with each order item still exists in the catalog.

However, if the product has been deleted, $item->getProduct() returns null, causing the call to getId() to fail.

The original product ID is still available in the product_id field of sales_order_item, so using getProductId() directly avoids this issue.

The following change fixes the problem:

--- a/Api/OrderFactory.php
+++ b/Api/OrderFactory.php
@@ -63,7 +63,7 @@
                 $childItem = reset($childrenItems);
                 $variantId = $childItem->getProductId();
             } else {
-                $variantId = $item->getProduct()->getId();
+                $variantId = $item->getProductId();
             }
 
             $lines[] = new Line(

I originally encountered this issue in version 21.0.0 and confirmed that it is still present in version 21.3.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions