Magento 2 - Product category id on success
-
10-12-2020 - |
Question
How can I get the category id of the products ordered on the success.phtml
page. I have managed to get other bits of data but cant find the category id.
I need to check the category so i can post to a third parties API only if a product from a specific category has been purchased.
My OOP knowledge isn't the best. So please be patient lol
La solution
<?php
$orderId = $this->getOrderId();
echo "Order ID:".$orderId."<br/>";
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($orderId);
$items = $order->getAllVisibleItems();
foreach($items as $i):
$_product = $objectManager->create('Magento\Catalog\Model\Product')->load($i->getProductId());
$categoryIds = $_product->getCategoryIds();
endforeach;
?>
You will get all category IDs for that particular product in $categoryIds
. But again I won't recommend to do this using Object Manager directly. Instead of doing this please create your block and pass detail from there and you can retrieve in this file.
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange