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.
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:66The 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()returnsnull, causing the call togetId()to fail.The original product ID is still available in the
product_idfield ofsales_order_item, so usinggetProductId()directly avoids this issue.The following change fixes the problem:
I originally encountered this issue in version 21.0.0 and confirmed that it is still present in version 21.3.0.