0

I am trying to print the name of an item inside an order in Magento 2.3.3. The item is a variation, and for some reason $item->getName() doesn't return the full name of the item. Instead it returns some kind of a general name, like the parent of the variation or something.

This is the data from mysql from this item. The correct name is #1, but the getName() returns #2.

enter image description here

This is the code:

$order = $this->_objectManager->get('Magento\Sales\Api\Data\OrderInterface')->loadByIncrementId($OrderNumber);
$items = $order->getAllItems();
foreach($items as $item){
    echo "item:".$item->getName()."\n";
}

I tried some other ways to get the correct name,such as getData('name'). Also $item->getProductOptions()['simple_name'] both return the wrong name. Another thing that I tried is getting the name of the parent of the item:

$product = $this->_objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable')->getParentIdsByChild($productId);
if(isset($product[0])){
    $product = $this->_objectManager->create('Magento\Catalog\Model\Product')->load($product[0]);
    $name = $product->getName();
}

UPDATE

The item is a variation item ('configuable'). The main item is ID:1557, SKU:1109 One of the variations is ID:197, SKU:11096

My issue that even when I get product id 197, I am still getting the name of the parent item. How can I get the name of the item itself?

enter image description here

$product_t = $this->_objectManager->create('Magento\Catalog\Model\Product')->load(197);
echo $product_t->getSku()."\n";   //===> 11096 (correct)
echo $product_t->getName()."\n";  //===> תיק רחצה נתלה (wrong, name of parent variation)

UPDATE 2

I have found a mysql query that returns the real name of the item:

select value from catalog_product_entity_varchar left join eav_attribute on
eav_attribute.attribute_id = catalog_product_entity_varchar.attribute_id
where
eav_attribute.attribute_code='name' and
catalog_product_entity_varchar.entity_id=197

result[0] is the name of the variation that I am looking for:

enter image description here

Anyone knows how to do the same in code?

1
  • I have no idea but you can look into store part too. Commented Dec 23, 2020 at 12:04

3 Answers 3

0

Although using object manager is not a good practice but just to guide you I'm using it. Change it into dependency method if it works. Add below code :

  $order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId);
  $orderItems = $order->getAllItems();
foreach ($orderItems as $item) {
    $item->getItemId();
    $item->getOrderId();
    $item->getStoreId();
    $item->getProductId();

    print_r($item->getProductOptions());

    $item->getSku();
    $item->getName();
    $item->getQtyOrdered();
    $item->getPrice();

For details visit this link. hope it helps.

6
  • This is exactly what I am doing and the getName() returns the wrong name
    – justadev
    Commented Dec 23, 2020 at 11:39
  • Did you try with \Magento\Sales\Model\OrderRepository? Commented Dec 23, 2020 at 11:40
  • Yes, I tried it now. Same issue. This looks like an issue with the item data, not the order data.
    – justadev
    Commented Dec 23, 2020 at 11:47
  • Though the above solution should work but if it's not then perhaps you can try getting productId from $item and then get corresponding name from product catalog? Commented Dec 23, 2020 at 12:33
  • I updated the question with some additional info. Maybe it helps?
    – justadev
    Commented Dec 23, 2020 at 15:34
0

I think problem is your store view level data. Here is an example of getting product:


$order = $objectManager->get('\Magento\Sales\Model\OrderRepository')->get($orderId);
$productObj = $objectManager->get('Magento\Catalog\Model\ProductRepository');
$product = $productObj->getById($productId, false, $order->getStoreId());

Not tested code but you can modify it in your way.

2
  • Unfortunately no, I tried it right now, and it is still returning the wrong title.
    – justadev
    Commented Dec 23, 2020 at 14:59
  • I updated the question with some additional info. Maybe it helps?
    – justadev
    Commented Dec 23, 2020 at 15:34
0

This code worked:

foreach($items as $item){
    $product_obj = $item->getProduct();
    $productId = $item->getProductId();
    $name = $product_obj->getResource()->getAttributeRawValue($productId,'name',0);
    echo "name:$name\n";
}
1
  • I'm trying to get the Simple Product name in Shipment emails instead of the Configurable name. Same issue as you had - ID & Sku are correct but name is not. This code doesn't work for me. Did anyone else have a solution? Commented Dec 15, 2023 at 15:03

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.