How to get Product Category name and Category ID on checkout success page?

I am looking for a way to get Category name and Category ID one the checkout success page. So far I have added the product name, sku etc but I am unable to get the category name and id

Here's my code to product attributes and category names:

<?php //
    $products = array();
    $lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
    $_order = Mage::getSingleton('sales/order')->load($lastOrderId);

/* @var $item Mage_Sales_Model_Order */
foreach ($_order->getAllVisibleItems()  as $item) {
    $info['sku'] = trim($item->getSku());
    $info['qty'] = $item->getQtyOrdered();
    $info['price'] = $item->getPrice();
    $info['name'] = $item->getName();

    $products[] = $info;
}

    $product = Mage::getModel('catalog/product')->load($productId);
    $categoryIds = $product->getCategoryIds();
    $categoryName = '';
if (isset($categoryIds[0])){
    $category = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($categoryIds[0]);
    $categoryName['category'] = $category->getName();
}

$config['productInfos'] = $products;
$config['cartTotal'] = $_order->getGrandTotal();
$config['currency'] = Mage::app()->getBaseCurrencyCode();
?> 

Here we echo out the values:

_roi.push(['_addItem',
    '<?php echo $product['sku']; ?>', // Merchant SKU
    '<?php echo $product['name']; ?>', // Product Name
    '<?php echo $categoryIds['categoryId']; ?>', // Category ID
    '<?php echo $categoryName['category']; ?>', // Category Name
    '<?php echo $product['price']; ?>', // Unit Price
    '<?php echo $product['qty']; ?>' // Item Quantity
]);

The issue is the CategoryIds and CategoryName are not showing and I'm stuck!

Anyone able to help please do?

Solutions

When you loaded order, it contains ordered items, not products. To get products from items, you should get item_id for each product, and load products by that Id, and only then get categories data for this products.

When you do $product = Mage::getModel('catalog/product')->load($productId), it receives no product ID, as $productId is not defined. You can assign it to $item->getData('product_id');

$_item['product_id'] is a product id . We will find out categoryid associate with product.

$product = Mage::getModel('catalog/product')->load($_item['product_id']);
$cats = $product->getCategoryIds();

Now we have category ID(s). One product can associate with more then one categorys. This is Magento default nature.

 foreach ($cats as $category_id) {
    $_cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($category_id);
        echo $_cat->getName();             
    }

$_cat->getName(); is your product category name. This is beneficial if product is associate with a single category. if product is associate with multiple category then we need category ID to get product category name.

i hope this will little helpful for you

Use product ID :

$product = Mage::getModel('catalog/product')->load($productId);
$categoryIds = $product->getCategoryIds();
$categoryName = '';
if (isset($categoryIds[0])){
$category = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($categoryIds[0]);
$categoryName = $category->getName();
}

Similar questions

Can we turn the checkout success page into a checkout step in Magento 2?
How can we make the checkout success page part of the regular Magento2 checkout steps? I like the idea of having a simplified layout with minimal content during the checkout steps. But the confirmation/success page has that same, main layout used by most of the website. I know that we can build additional checkout steps, i.e: http://devdocs.magento...
How to get all product SKUs in foreach loop on checkout success page?
I am trying to add some tracking and conversion codes which require all skus from an order to be listed in the HTML tag on the checkout success page. So far I have been able to get the QTY, Order Amount and SKU but I need to show ALL SKUs from the order not just one so need to loop but I'm doing it wrong. Here is what I have: One I have the order i...
how to send payment success email after braintree payment gateway return success?
how to send payment success email when braintree payment gateway return success? I know this is a bit odd, but my magento is missing order number even the payment was success. Any suggestion to get alert email or notice by another way? Thanks
Magento 2 - How to retrieve the name and quantity for each product ordered in success.phtml file
I need to add an iframe for tracking the orders, and the code needs some values like Subtotal, order id, name of every product in the order and also the quantity for each product. I found a solution for order id and subtotal value. From getData() function you can retrieve informations like, subtotal, customer email, order id, status of the order, s...
Showing product size and color in magento checkout success page
Actually just want to show product attribute on magento checkout success page Currently just found it, and need for the size and color, any suggestion .. thanks
Magento 2.3.0 - Create Product Page H1 title with product name and attribute name
I have created two phtml files that can be called from within a CMS Block on a product page to echo the product name and an attribute name. As follows: Product Name {{block class="Vendor\ProductName\Block\ProductName" template="Vendor_ProductName::productname.phtml" }} Attribute Name (Artist) {{block class="Magento\Catalog\Block\Product\View" name=...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.