0

It's a really strange issue I am facing it in Magento 2.

I have created a bulk import functionality of shipping information update by CSV Import.

Example: I have created grn_number field of shipping attribute and it associated with shipping_id.

enter image description here

For grn numbers, bulk uploading, below screen shows you a better understanding. enter image description here

Import was sucessfully completing with no erros.

But the problem is where we are facing that after grn number update we want to send shipment email along with the updated grn number.

So first record sending email is with all details but after the second record getting an error like Fatal error: Uncaught Error: Call to a member function getParentItem() on null templates/email/shipment/items.phtml on line 35 Image Items Qty

Code: templates/email/shipment/items.phtml on line 35:

<?php foreach ($_shipment->getAllItems() as $_item): ?>
            <?php $logger->info("Class Name --> ". get_class($_item->getOrderItem())); ?>
            <?php //if ($_item->getOrderItem()) : ?>
                <?php if (!$_item->getOrderItem()->getParentItem()) : ?>
                    <tbody>
                        <?= $block->getItemHtml($_item) ?>
                    </tbody>
                <?php endif; ?>
            <?php //endif; ?>
<?php endforeach; ?>

I printed class name of $_item->getOrderItem() so the result like :

1st record : Magento\Sales\Model\Order\Item\Interceptor

2nd record : Magento\Framework\View\TemplateEngine\Php

enter image description here

I also checked with the second record replaced with the first record in CSV than also the first record send the shipment email and second time it gives an error.

Waiting for a solution || Thanks in advance.

2

First, I have the exact same error, but I have this is two places - Customer account, view order->view invoice / -> view shipping (for that order)

In both those, I get the error:

Fatal error: Uncaught Error: Call to a member function getParentItem() on null in /srv/public_html/vendor/magento/module-shipping/view/frontend/templates/items.phtml:70

From your code snippet, I can see the code involved is the same:

I have:

        <?php $_items = $_shipment->getAllItems(); ?>
        <?php foreach ($_items as $_item): ?>
            <?php if (!$_item->getOrderItem()->getParentItem()) : ?>
                <tbody>
                    <?= $block->getItemHtml($_item) ?>
                </tbody>
            <?php endif; ?>
        <?php endforeach; ?>

which is essentially the same issue, involving the same classes. In both cases, items are being iterated, and the call to $_item->getOrderItem()->getParentItem() bails in a call of ->getParentItem() on a NULL

So, the question in both instances, is why is the call to $_item->getOrderItem() resulting in a NULL

Just bypassing the issue, is not a fix, as there is clearly an underlying data issue here. In my case, I suspect it is due to migration, and in yours due to your import. I highly suspect, if you view your orders in a customers Account section, and try view the Shipment, you will get the same issue, as there is an issue with the underlying data.

Using a debugger, placing a breakpoint on the call, you can step into the method, which results to this code:

/**
     * Retrieve order item instance
     *
     * @return \Magento\Sales\Model\Order\Item
     */
    public function getOrderItem()
    {
        if (null === $this->_orderItem) {
            if ($this->getShipment()) {
                $this->_orderItem = $this->getShipment()->getOrder()->getItemById($this->getOrderItemId());
            } else {
                $this->_orderItem = $this->_orderItemFactory->create()->load($this->getOrderItemId());
            }
        }
        return $this->_orderItem;
    }

located in Magento\Sales\Model\Order\Shipment\Item

(even though you have issue in email, you will end up in that exact same class and method, since you using the Item Object as fetch using $_shipment->getAllItems())

In my case, the $_item class is Magento\Sales\Model\Order\Shipment\Item (and when doing the same error in in invoice, I have Magento\Sales\Model\Order\Invoice\Item, with the same code in that class.

Ok, so digging further, I can see (via stepping on with debug), that the following code runs:

$this->_orderItem = $this->getShipment()->getOrder()->getItemById($this->getOrderItemId());

enter image description here

The value of $this->getOrderItemId() IS populated in the object, so that is not the issue.

enter image description here

SO, why is that line of code, producing a null. Lets step in further, and I find that all parts of the code, each method works fine, all the way up to:

 /**
     * Gets order item by given ID.
     *
     * @param int $itemId
     * @return \Magento\Framework\DataObject|null
     */
    public function getItemById($itemId)
    {
        $items = $this->getItems();

        if (isset($items[$itemId])) {
            return $items[$itemId];
        }

        return null;
    }

located in Magento\Sales\Model\Order

and THIS is where the underlying issue is. It is here that I get back a null.

enter image description here

For some reason the $items array is 0, which then results in no such prodict with given ID being found, which then results in NULL as the return.

Here is the underlying issue, and would be an issue in multiple places in the site.

Just masking the issue, like @AmitBera suggested, is not a good fix. You will definitely find other parts broken, by the same reason.

Although this is not 100% the same place in the framework you get your issue, it is surely related, and you can debug your specific issue the same way, to get a better solution, and fix.

I hope that helps.

Some additional info:

Drilling further down the method calls ->getItems(), I get to this:

enter image description here

So, a deeper issue here (might not be the same issue you have) is that the system is trying to load an order by a given ID, but finds none.

As I originally thought, this is an issue with data migration from m1 to m2, and I am now going to dig around in the db (oh what fun)

Clearly the order IS there, in the system, as I could list the order initially to view the invoice/shipping, but it is not found by the code.

Potentially (he speculates) is that entity ids got changed by the migration.

My issue, for this error, is specifically that the migration process had dropped out a lot of sales order items, located in sales_order_item table

SELECT * FROM lucas_m2.sales_order_item where order_id = 126672

results in no order items in m2

but results in items in source m1 table

SELECT * FROM sales_flat_order_item where order_id = 126672

so 100% a data migration issue

  • Good explain ProxiBlue – Amit Bera Sep 24 at 6:00
  • Very deep explanation thank you @ProxiBlue – Praveen Chelumalla Sep 25 at 7:35
  • Np. Had to debug issue for myself, so good place to record. Today I reran my M1 to M2 migration from scratch, and the missing data came in. Happy days – ProxiBlue Sep 25 at 11:03
-1

Changes

<?php if (!$_item->getOrderItem()->getParentItem()) : ?>

to

<?php if(!$item->getOrderItem() instanceof \Magento\Sales\Model\Order\Item): ?>
    <?php continue;?>
<?php endif;?>
 <?php if (!$_item->getOrderItem()->getParentItem()) : ?>
  • Thank you @Amit It very uses full condition and now the problem I am getting like In shipping email template product grid information not coming for remaining the first shipment. – Praveen Chelumalla Aug 29 at 8:53
  • This 'fix' just masks the problem, which is why is there no OrderItem object associated with the item. It not really a good way to deal with this. – ProxiBlue Sep 24 at 3:43
  • @ProxiBlue, if you have any Good solution then please post on this question – Amit Bera Sep 24 at 4:13
  • I will, after I investigate, the underlying cause of the issue. I have the exact same error, but caused by data migration (i suspect), but the issue is the exact same code... – ProxiBlue Sep 24 at 4:20
  • @AmitBera Answer placed. – ProxiBlue Sep 24 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.