1

Magento 2 how to get Ordered product category id

My problem is i have same product with multiple category so i want know which category product i place order based on order id

<?php 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')
->loadByIncrementId('000000004');
$orderItems = $order->getAllItems();
foreach ($orderItems as $item) {
  echo "<pre>";
  echo $itemQty = $item->getProductId();

?>

2 Answers 2

1

In your case:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId('000000002');

$orderItems = $order->getAllItems();

foreach ($orderItems as $item) {

$product = $objectManager->get('Magento\Catalog\Model\Product')->load($item->getProductId());

$categoryIds = $product->getCategoryIds();

}
2
  • No I already got this answer my problem is i have same product with multiple category so i want know which category product i added based on order id
    – Magento 2
    Commented Dec 19, 2017 at 4:42
  • This is actual in magento 2: magento.stackexchange.com/questions/114300/… You must save this info manually. Try to use observer, for example before order create.
    – Anny
    Commented Dec 19, 2017 at 7:54
0

Please find below answer to get category ids.

$product_id=20;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);

$cats = $product->getCategoryIds();
print_r($cats);exit;

Thanks

1
  • No I already got this answer my problem is i have same product with multiple category so i want know which category product i added based on order id
    – Magento 2
    Commented Dec 19, 2017 at 4:42

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.