3

From the Magento checkout success page I need to get the following data to add to a tracking pixels script.

$orderObj = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());  // get the last order object
$customerType;
$productCategories;
$orderId = $orderObj->getIncrementId();
$orderRevenue = number_format($orderObj->getBaseGrandTotal(),2);

I have successfully gotten $orderId & $orderRevenue but how can I get $customerType & $productCategories?

I thought I could use something like $orderObj->_data['customer_group_id'] to help me get the $customerType.

UPDATE

I was able to get $customerType with:

$customerId = $orderObj->getCustomerGroupId();
$customerType = Mage::getSingleton('customer/group')->load($ross)->getData('customer_group_code');

Now just to figure out how to get$productCategories; ?

3 Answers 3

3

You can get the customer group id like this :

$customerId = $order->getCustomerId();
$customer = Mage::getModel('customer/customer')->load($customerId);
$groupId =  $customer->getGroupId(); 

you can get category ids like this :

   $orderIncrementId= $orderObj->getIncrementId();
   $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
   $items = $order->getAllVisibleItems();
   foreach($items as $i):
      $product = $i->getProductId();
      $parentCategoryId =$product->getCategory()->getParentCategory();
   endforeach;
3
  • $orderObj->getCustomerId(); returns null
    – Holly
    Commented Feb 5, 2015 at 15:21
  • Did you tried my answer for category ids?? Commented Feb 5, 2015 at 15:27
  • @Grish SH, I tried the your answer for category ids but I got an error evaluating the code from xdebug. I got it working in the end, see my answer. Thanks for your help.
    – Holly
    Commented Feb 6, 2015 at 11:35
2

@Creen,You cannnot get product category directly because of Magento did nots save product all details and it category save at Sales Order object.

So,you need get product id from order item

$items = $order->getAllVisibleItems();
   foreach($items as $i):
      $product = $i->getProductId();

   endforeach;

then need to get product object using Mage::getModel('catalog/product')->load();

$Pro=Mage::getModel('catalog/product')->load($i->getProductId());
If($Pro->getId()){
// 

}
`

then from Product object $Pro you can get Category id and it parent category id

$cats = $Pro->getCategoryIds();
foreach ($cats as $category_id) {
    $category = Mage::getModel('catalog/category')->load($category_id);
    //each category has a path attribute
    $path = $category->getPath(); //should look like 1/3/14/23/55.
    //split the path by slash
    $pathParts = explode('/', $path);
    if (count($pathParts) == 3) {
        //it means the category is already a top level category
        echo $category->getName();
    }
    elseif (isset($pathParts[2])) {
        $topCategory = Mage::getModel('catalog/category')->load($pathParts[2]);
        echo $topCategory->getName();
    }

See at How to i get the base parent category name?

2

So in the end the below code got what I needed. The fourth one was the most difficult. Getting the list of product categories from the order. I had to iterate through each product to get each category it could have belonged to.

 $orderObj = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());  // get the last order object

    // 1.) get order id
    $orderId = $orderObj->getIncrementId();

    // 2.) get total order revenus
    $orderRevenue = number_format($orderObj->getBaseGrandTotal(),2);

    // 3.) get customer type
    $customerId = $orderObj->getCustomerGroupId(); // first  get customer id from order to find customer type from the customer/group model 
    $customerType = Mage::getSingleton('customer/group')->load($customerId)->getData('customer_group_code');

    // 4.) get list of categories which product belongs to.
    $orderItems = $orderObj->getAllVisibleItems();  // get list of line items from the order
    foreach($orderItems as $i):  // iterate over line items
        $productId = $i->getProductId(); // get product id from each line item
        $product = Mage::getModel('catalog/product')->load($productId);  // load the product object from the product id
        $categoryIds = $product->getCategoryIds(); // get the list of categories which the product belongs to.
        foreach($categoryIds as $i): // iterate through list of categories
            $category = Mage::getModel('catalog/category')->load($i);  // get the id of the category from the category model
            $productCategories[] = $category->getName();  // get the name of the category and put it into an array for use later
        endforeach;
    endforeach;
    $productCategories = array_unique($productCategories);  // remove duplicate catgoires from array created in above foreach loops.
0

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.