From 780f86e6542549a52ffae577af903240b0347d2b Mon Sep 17 00:00:00 2001 From: jacker Date: Mon, 1 Mar 2021 10:57:41 +0700 Subject: [PATCH] Improve get product from order item --- Helper/EmailMarketing.php | 17 ++++++++++++++--- composer.json | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Helper/EmailMarketing.php b/Helper/EmailMarketing.php index 23f5790..2134939 100644 --- a/Helper/EmailMarketing.php +++ b/Helper/EmailMarketing.php @@ -57,6 +57,7 @@ use Magento\Reports\Model\ResourceModel\Order\CollectionFactory as ReportOrderCollectionFactory; use Magento\Sales\Model\Order; use Magento\Sales\Model\Order\Creditmemo; +use Magento\Sales\Model\Order\Item as OrderItem; use Magento\Sales\Model\Order\Shipment; use Magento\Sales\Model\Order\Invoice; use Magento\Sales\Model\ResourceModel\Order\Collection as OrderCollection; @@ -720,7 +721,7 @@ public function getShipmentOrCreditmemoItems($object) $items = []; foreach ($object->getItems() as $item) { $orderItem = $item->getOrderItem(); - $product = $orderItem->getProduct(); + $product= $this->getProductFromItem($orderItem); if ($orderItem->getParentItemId() && isset($items[$orderItem->getParentItemId()]['bundle_items'])) { $items[$orderItem->getParentItemId()]['bundle_items'][] = [ 'title' => $item->getName(), @@ -826,6 +827,16 @@ public function getItemOptions($orderItem) return $this->formatOptions($orderItem, $result); } + /** + * @param Item|OrderItem $item + * + * @return DataObject|Product + */ + public function getProductFromItem($item) + { + return $item->getProduct() ?: new DataObject([]); + } + /** * @param Quote|Shipment|Creditmemo|Order $object * @@ -845,7 +856,7 @@ public function getCartItems($object) /** * @var Product $product */ - $product = $item->getProduct(); + $product = $this->getProductFromItem($item); $productType = $item->getData('product_type'); $bundleItems = []; @@ -879,7 +890,7 @@ public function getCartItems($object) if ($item->getHasChildren()) { $children = $isQuote ? $item->getChildren() : $item->getChildrenItems(); foreach ($children as $child) { - $product = $child->getProduct(); + $product = $this->getProductFromItem($child); if ($hasVariant) { $itemRequest['variant_title'] = $child->getName(); $itemRequest['variant_image'] = $this->getProductImage($product); diff --git a/composer.json b/composer.json index 7f3b296..33b5615 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "mageplaza/module-core": "^1.4.5" }, "type": "magento2-module", - "version": "1.7.1", + "version": "1.7.2", "license": "proprietary", "authors": [ {