0

I want to get the current category name in a .phtml file, having only the product ID. The product ID is fetched from reviews.

Note: I'm not running this on product page.

This is what I have now:

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$reviewData = $block->getAllRatingData();

foreach ($reviewData as $reviewKey => $reviewValue) {

$productId   = $reviewValue['entity_pk_value'];
$product     = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
$productName = $product->getName();
$productUrl  = $product->getProductUrl();

?>
<div>
    <span><?php echo $reviewValue['Grade']; ?></span><br>
    <span><a href="<?php echo($productUrl); ?>"><?php echo($productName); ?></a></span><br>
    <strong><?php echo $reviewValue['title']; ?></strong><br>
    <span><?php echo date_format(date_create($reviewValue['created_at']),"Y-m-d"); ?> 
    <?php echo $reviewValue['nickname']; ?></span><br>
    <em><?php echo $reviewValue['detail']; ?></em>
</div>
<br>
<?php
}
?>

Thanks,

4
  • on which page this code running? Aug 10 at 7:49
  • @Charmi Patel => It's running from a module, and is called from a CMS page.
    – Aknot
    Aug 20 at 12:50
  • 1
    You want to category id from the product id which is fetched by the review. I mean in which categories assigned this product right? Aug 21 at 4:24
  • Yes exactly, thats correct, and with the corresponding category name :-)
    – Aknot
    Aug 22 at 14:13

2 Answers 2

1

Try the below code. I update the code as per your requirement

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$reviewData = $block->getAllRatingData();

foreach ($reviewData as $reviewKey => $reviewValue) {
    $productId = $reviewValue['entity_pk_value'];
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
    $productName = $product->getName();
    $productUrl = $product->getProductUrl();
    
    // Get category IDs associated with the product
    $categoryIds = $product->getCategoryIds();
    
    // Initialize an empty array to store category names
    $categoryNames = [];
    
    // Load category names based on category IDs
    foreach ($categoryIds as $categoryId) {
        $category = $objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
        $categoryNames[] = $category->getName();
    }
    
    // Implode category names array into a string
    $categoryNamesString = implode(', ', $categoryNames);
    
    ?>
    <div>
        <span><?php echo $reviewValue['Grade']; ?></span><br>
        <span><a href="<?php echo($productUrl); ?>"><?php echo($productName); ?></a></span><br>
        <strong><?php echo $reviewValue['title']; ?></strong><br>
        <span><?php echo date_format(date_create($reviewValue['created_at']),"Y-m-d"); ?> 
        <?php echo $reviewValue['nickname']; ?></span><br>
        <em><?php echo $reviewValue['detail']; ?></em><br>
        <span>Categories: <?= $categoryNamesString; ?></span>
    </div>
    <br>
    <?php
}
?>

check this one and please tell me if you have any query

NOTE: replace objectmanager with corresponding dependencies

0
1

Get category name from product anywhere.

<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $categoryAllIds = $product->getCategoryIds();

    $names = [];    
    
    foreach ($categoryAllIds as $categoryId) {
        $category = $objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
        $names[]  = $category->getName();
    }            
    $allCategory = implode(', ', $names);
    
    echo $allCategory;
}
?>

Note : Handle $objectManager according you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.