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();
}