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.
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?
$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:
Anyone knows how to do the same in code?