2

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?

1
  • You haven't defined the $productId variable. It could be retrieved as $item->getData('product_id').
    – Dmytro
    Jan 21, 2015 at 9:17

5 Answers 5

7

$_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

2

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

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');

1

Things to note:

  1. $categoryIds = $product->getCategoryIds(); will give categories ids in comma separated system (1,2,3,4) so you need to explode this into individual values and then load category (ONLY IF A PRODUCT CAN BE IN MULTIPLE CATEGORIES).

  2. $categoryName = ''; This should be $categoryName = array();

1

Shopping.com doesn't say anything about possibility to put few categories for one item, so it means that somehow you need to choose one main category from array.

in my case i randomly choosing the second id from array if array is exist. Hope it will help to somebody because I was looking solution for this for a while.

<?php
 $items = $order->getAllItems();
foreach ($items as $item){

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

if (is_array($cats)){
    $_cat = $cats[2]; 
}
else $_cat = $cats;
$_category = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($_cat);
?>


<script type="text/javascript">
_roi.push(['_addItem', 
'<?php echo $item->getSku();?>', // Merchant sku
'<?php echo $item->getName();?>', // Product name
'<?php echo $_cat;?>', // Category id
'<?php echo $_category->getName();?>', // Category name
'<?php echo $item->getPrice();?>', // Unit price
'<?php echo $item->getQtyToShip();?>' // Item quantity
]);

</script>
<?php

}

?>

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.