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

  • so you want category IDs for each product of that particular order on success.phtml page? – Abhishek Panchal Aug 16 '17 at 15:36
  • yes or at least be able to check for "X" id exists, if true then post to API – webbie1985 Aug 16 '17 at 15:42
$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();


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.

  • This didnt seem to work. The array inside $items is always empty here is what i am trying to do to show what is inside the array. $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->create('Magento\Sales\Model\Order')->load($orderId); $items = $order->getAllItems(); echo "<pre>"; print_r($items); echo "</pre>"; – webbie1985 Aug 17 '17 at 12:02
  • I edit my answer. Please print $_product object and check for categopry_ids. Thanks – Abhishek Panchal Aug 17 '17 at 13:29
  • Printing $_product does not work either. If i try to print $items the array is empty so i think that is why printing $_product does not work – webbie1985 Aug 17 '17 at 14:57
  • edit my code again... $items = $order->getAllVisibleItems();.. it should work – Abhishek Panchal Aug 17 '17 at 15:01
  • That gives me a empty $items varible too – webbie1985 Aug 17 '17 at 15:25

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.