0

How can I get the name of a product in the order history page? I copied the view order code in but it's not working.

$_order = $block->getOrder()
<?php $_items = $_order->getItemsCollection(); ?>
<?php foreach ($_items as $_item){ ?>
<?php echo $block->getItemHtml($_item) }?>

2 Answers 2

2

Magento save order items name at sales_order_item table's at name columns.So you have order item object then using getName() function for getting product name at any where.

<?php  $_order = $block->getOrder();
foreach ($_items as $_item){ ?>
<?php echo $_item->getName(); ?>
<?php echo $block->getItemHtml($_item) 
}?>
2
  • getting error call to a member function getItemsCollection() on null
    – User0434
    Oct 12, 2016 at 13:31
  • may be you did not add $_order = $block->getOrder()
    – Amit Bera
    Oct 12, 2016 at 13:36
1

You copied wrong code you need to copy rendered item data from /module-sales/view/adminhtml/templates/order/view/items/renderer/default.phtml

<?php $_item = $block->getItem()
 $block->setPriceDataObject($_item) ?><tr>

<?php $i = 0;
$columns = $block->getColumns();
$lastItemNumber = count($columns) ?>
<?php foreach ($columns as $columnName => $columnClass):?>
    <?php $i++; ?>
    <td class="<?php echo /* @noEscape */ $columnClass?><?php /* @noEscape */ echo ($i === $lastItemNumber ? ' last' : '')?>"><?php /* @escapeNotVerified */ echo $block->getColumnHtml($_item, $columnName) ?></td>
<?php endforeach; ?></tr>

get item with column name, will allow you to get product name.

3
  • its not working @Ronak Chauhan
    – User0434
    Oct 12, 2016 at 13:36
  • have you called this function : $_items = $block->getItemsCollection() Oct 12, 2016 at 13:42
  • /vendor/magento/module-sales/view/adminhtml/templates/order/view/items.phtml this file called rendered file so you need to pass that item data through this : $block->getItemHtml($_item) Oct 12, 2016 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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