Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Import Tracking Code and Complete Order With Cron and a CSV [Solved]
 
ramk
Jr. Member
 
Total Posts:  14
Joined:  2011-02-15
 

Hi Elfling,

Thanks for your reply.

I will look into this and will check it.

Thanks again,
Ram.

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  901
Joined:  2008-10-21
 

just a heads up.

You will want to add this to line 22.

$shipment->setIncrementId($orderId);

so that it is

$shipment $convertor->toShipment($order);
    
$shipment->setIncrementId($orderId);

This will fix all problems with multiple tracking codes and remove the need for fooman sameinvoice.

 
Magento Community Magento Community
Magento Community
Magento Community
 
TEmelie
Jr. Member
 
Total Posts:  12
Joined:  2010-09-08
 

Hello,

Congrats for your work so far.
I tried your module, installed Manual Upload.zip but I cannot access the module from my admin interface. Can’t see it :(
Some hours of work were spent on this module, I guess a dummie’s guide will be more than welcome smile
I’m using Magento 1.4x

Thanks,
Adrian

 
Magento Community Magento Community
Magento Community
Magento Community
 
RuggeroSB
Jr. Member
 
Total Posts:  13
Joined:  2011-04-19
 
TEmelie - 17 April 2011 01:16 PM

Hello,

Congrats for your work so far.
I tried your module, installed Manual Upload.zip but I cannot access the module from my admin interface. Can’t see it :(
Some hours of work were spent on this module, I guess a dummie’s guide will be more than welcome smile
I’m using Magento 1.4x

Thanks,
Adrian

+1 on not being able to get it to work. I’m running Magento 1.5 though, and I’m trying to use the cron version… I know Elfling said it’s designed for 1.3/1.4 - I don’t know if there’s a change required for 1.5?

I’ve mailed Elfling; hopefully it’s an easy fix ‘cuz this is basically exactly what I need…

 
Magento Community Magento Community
Magento Community
Magento Community
 
RuggeroSB
Jr. Member
 
Total Posts:  13
Joined:  2011-04-19
 

Okay so really ridiculous Magento nuance for those of you who didn’t know - uploading the module is not sufficient to ‘activate’ it. You have to go into System -> Configuration -> Advanced -> Advanced and click on the “Save Config” button. You should see the module’s name appear in the list now (in this case, Nick_Trackingimport - at the bottom).

Do this whenever uploading/deleting modules.

Now to fix an actual code problem with the module - getting this error:

Fatal error: Cannot redeclare updateorder() (previously declared in /home/staging/public_html/store/app/code/community/Nick/Trackingimport/Model/Observer.php:12) in /home/staging/public_html/store/app/code/community/Nick/Trackingimport/Model/Observer.php on line 12

Corrected, complete code looks like this:

<?php

class Nick_Trackingimport_Model_Observer {
  
    
public function Fileimport() {
  
          
if( !function_exists"updateOrder" ) ) {
            
function updateOrder($orderId$trackingNum{
                $includeComment 
false;
                
$comment NULL;
                
                
$order Mage::getModel('sales/order')->loadByIncrementId($orderId);
                
$email $order->getCustomerEmail();
                            
                
$orderStatus $order->getStatus();
                
                
//This converts the order to "Completed".
                
if( $orderStatus == "processing" {
                    $convertor 
Mage::getModel('sales/convert_order');
                    
$shipment $convertor->toShipment($order);
                    
$shipment->setIncrementId($orderId);
                    
                    foreach (
$order->getAllItems() as $orderItem{
                    
                        
if (!$orderItem->getQtyToShip()) {
                            
continue;
                        
}
                        
if ($orderItem->getIsVirtual()) {
                            
continue;
                        
}
                    
                        $item 
$convertor->itemToShipmentItem($orderItem);
                    
                        
$qty $orderItem->getQtyToShip();
                    
                        
$item->setQty($qty);
                        
$shipment->addItem($item);
                    
}
                    
                 } 
else {
                
                    
foreach ($order->getShipmentsCollection() as $shipment{
                        $shipmentId 
$shipment->getIncrementId();
                    
}
                        $shipment 
Mage::getModel('sales/order_shipment')->loadByIncrementId($shipmentId);
                
}
                
                $carrierTitle 
NULL;
                
$carrier 'custom';
                
                
// READ MY COMMENTS REGARDING THIS SECTION ABOVE.
                
                
if ($carrier == 'ups'{
                    $carrierTitle 
'United Parcel Service';
                
}
                
                
if ($carrier == 'custom'{
                    $carrierTitle 
'Parcel Force';
                
}
                
                $data 
= array();
                
$data['carrier_code'$carrier;
                
$data['title'$carrierTitle;
                
$data['number'$trackingNum;
                
                
$track Mage::getModel('sales/order_shipment_track')->addData($data);
                
$shipment->addTrack($track);
                
                
                if( 
$orderStatus == "processing" {
                    $shipment
->register();
                    
$shipment->addComment($comment$email && $includeComment);
                    
$shipment->setEmailSent(true);
                    
$shipment->getOrder()->setIsInProcess(true);
                    
                    
$transactionSave Mage::getModel('core/resource_transaction')
                        ->
addObject($shipment)
                        ->
addObject($shipment->getOrder())
                        ->
save();
                    
                    
$shipment->sendEmail($email, ($includeComment $comment ''));
                    
                    
$shipment->save();
                
else {
                    $track
->save();
                    
$shipment->sendEmail($email, ($includeComment $comment ''));
                    
$order->setStatus('complete'); 
                    
$order->addStatusToHistory('complete'''false);
                    
$order->save();
                
}
                
return;
            
}
        }
                
        $fileName 
'updateorders.csv';
        
$target_path Mage::getBaseDir().'/var/import/'.$fileName;
        
        
ini_set("auto_detect_line_endings"1);
        
$current_row 1;
        
$handle fopen($target_path"r");
        
$csvData = array();
        
        while ( (
$data fgetcsv($handle10000",") ) !== FALSE {
            $number_of_fields 
count($data);
            if (
$current_row == 1{    //Header line
                
for ($c=0$c $number_of_fields$c++) {
                    $header_array[$c] 
$data[$c];
                
}
            } 
else {    //Data line
                
for ($c=0$c $number_of_fields$c++) {
                    $data_array[$header_array[$c]] 
$data[$c];
                
}
                $csvData[] 
$data_array;
            
}
            $current_row
++;
        
}
        
        fclose
($handle);
        
        foreach(
$csvData as $rec
            updateOrder
($rec['OrderNumber']$rec['TrackingNumber']); 
        
}
    }
}

So the above returns no errors, but it also doesn’t appear to do what it’s designed to do: change order status and add tracking number…

I’m investigating in the mean time, but since I’m not familiar with Magento module development as a rule; any ideas, Elfling?

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  901
Joined:  2008-10-21
 

Hi guys,

I’ll sort out some help with this shortly. Currently working on a few other things, so finding it hard to get time to break away. Will try and get this resolved for you over the weekend.

Regards,

Nick

 
Magento Community Magento Community
Magento Community
Magento Community
 
RuggeroSB
Jr. Member
 
Total Posts:  13
Joined:  2011-04-19
 

OK so here’s the bug and the solution:

When an order’s status is set to “pending”, then this part of the code:

if( $orderStatus == "processing" {
    $shipment
->register();
    
$shipment->addComment($comment$email && $includeComment);
    
$shipment->setEmailSent(true);
    
$shipment->getOrder()->setIsInProcess(true);
    
    
$transactionSave Mage::getModel('core/resource_transaction')
        ->
addObject($shipment)
        ->
addObject($shipment->getOrder())
        ->
save();
    
    
$shipment->sendEmail($email, ($includeComment $comment ''));
    
    
$shipment->save();
 
else {
    $track
->save();
    
$shipment->sendEmail($email, ($includeComment $comment ''));
    
$order->setStatus('complete'); 
    
$order->addStatusToHistory('complete'''false);
    
$order->save();
}
at the “else”, will break the loop. You can’t add a tracking number to a pending order (it hasn’t been paid yet, so you wouldn’t have shipped it - one hopes!). So, if your CSV has a “pending” shipment in it, or any other order status that doesn’t support adding tracking numbers, it’ll stop at that item and not process the rest of the CSV.

Simple solution is to change the ‘else’ to ‘elseif’, such as this:

elseif($orderStatus == "complete")  {
    $track
->save();
    
$shipment->sendEmail($email, ($includeComment $comment ''));
//    $order->setStatus('complete'); **you probably don't need this line anymore**
    
$order->addStatusToHistory('complete'''false);
    
$order->save();
}

I’m not sure if there are any other order statuses in which you can add tracking numbers...? If there are, you can just add them to the elseif status inside the round brackets, like this: || $orderStatus == “holded” || $orderStatus == “fraud” etc

Example:

elseif($orderStatus == "complete" || $orderStatus == "holded" )   {
    $track
->save();
    
$shipment->sendEmail($email, ($includeComment $comment ''));
    
$order->addStatusToHistory('complete'''false);
    
$order->save();
}
The || mean OR in php. Does anybody know which statuses you can add tracking numbers to apart from processing and complete?

I’ve also added a file deletion instruction to delete the CSV after processing it. Personally I’d prefer that the file be gone so that I know that the routine won’t run again, thus duplicating shipments and such. I also added a control to ensure that the system doesn’t try to open the file if it doesn’t exist.

 
Magento Community Magento Community
Magento Community
Magento Community
 
RuggeroSB
Jr. Member
 
Total Posts:  13
Joined:  2011-04-19
 

Here’s the working code which I corrected / altered as it stands now:

<?php

class Nick_Trackingimport_Model_Observer {
  
    
public function Fileimport() {
  
          
if( !function_exists"updateOrder" ) ) {
            
function updateOrder($orderId$trackingNum{
                $includeComment 
false;
                
$comment NULL;
                
                
$order Mage::getModel('sales/order')->loadByIncrementId($orderId);
                
$email $order->getCustomerEmail();
                                            
                
$orderStatus $order->getStatus();
                
//    This converts the order to "Completed".
                
if( $orderStatus == "processing" {
                    $convertor 
Mage::getModel('sales/convert_order');
                    
$shipment $convertor->toShipment($order);
                    
$shipment->setIncrementId($orderId);
                    
                    foreach (
$order->getAllItems() as $orderItem{
                    
                        
if (!$orderItem->getQtyToShip()) {
                            
continue;
                        
}
                        
if ($orderItem->getIsVirtual()) {
                            
continue;
                        
}
                    
                        $item 
$convertor->itemToShipmentItem($orderItem);
                    
                        
$qty $orderItem->getQtyToShip();
                    
                        
$item->setQty($qty);
                        
$shipment->addItem($item);
                    
}
                 } 
else {
                    
foreach ($order->getShipmentsCollection() as $shipment{
                        $shipmentId 
$shipment->getIncrementId();
                    
}
                        $shipment 
Mage::getModel('sales/order_shipment')->loadByIncrementId($shipmentId);
                
}
                
                $carrierTitle 
NULL;
                
$carrier 'custom';
                
                
// READ MY COMMENTS REGARDING THIS SECTION ABOVE.
                
                
if ($carrier == 'ups'{
                    $carrierTitle 
'United Parcel Service';
                
}
                
                
if ($carrier == 'custom'{
                    $carrierTitle 
'Parcel Force';
                
}
                
                $data 
= array();
                
$data['carrier_code'$carrier;
                
$data['title'$carrierTitle;
                
$data['number'$trackingNum;
                
                
$track Mage::getModel('sales/order_shipment_track')->addData($data);
                
$shipment->addTrack($track);
                
                if( 
$orderStatus == "processing" {
                    $shipment
->register();
                    
$shipment->addComment($comment$email && $includeComment);
                    
$shipment->setEmailSent(true);
                    
$shipment->getOrder()->setIsInProcess(true);
                    
                    
$transactionSave Mage::getModel('core/resource_transaction')
                        ->
addObject($shipment)
                        ->
addObject($shipment->getOrder())
                        ->
save();
                    
                    
$shipment->sendEmail($email, ($includeComment $comment ''));
                    
                    
$shipment->save();
                 
elseif( $orderStatus == "complete" {
                    $track
->save();
                    
$shipment->sendEmail($email, ($includeComment $comment ''));
                    
$order->addStatusToHistory('complete'''false);
                    
$order->save();
                
}
                
return;
            
}
        }
                
        $fileName 
'updateorders.csv';
        
$target_path Mage::getBaseDir().'/var/import/'.$fileName;
        
        
ini_set("auto_detect_line_endings"1);
        
$current_row 1;
        if( 
file_exists$target_path ) ) {
            $handle 
fopen($target_path"r");
            
$csvData = array();
            
            while ( (
$data fgetcsv($handle10000",") ) !== FALSE {
                $number_of_fields 
count($data);
                if (
$current_row == 1{    //Header line
                    
for ($c=0$c $number_of_fields$c++) {
                        $header_array[$c] 
$data[$c];
                    
}
                } 
else {    //Data line
                    
for ($c=0$c $number_of_fields$c++) {
                        $data_array[$header_array[$c]] 
$data[$c];
                    
}
                    $csvData[] 
$data_array;
                
}
                $current_row
++;
            
}
            
            fclose
($handle);
            
unlink( $target_path );
            foreach(
$csvData as $rec
                updateOrder
($rec['OrderNumber']$rec['TrackingNumber']); 
            
}
        } 
else {
        }
    }
}
?>

Works flawlessly for me now! smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  901
Joined:  2008-10-21
 

Nicely done, glad you got it working.

Please add

if($order['increment_id'== "")
        
{
                
return ;
        
}

underneath

$order Mage::getModel('sales/order')->loadByIncrementId($orderId);

That way if the orderId is incorrect, it will just skip the row and carry on importing.

 
Magento Community Magento Community
Magento Community
Magento Community
 
RuggeroSB
Jr. Member
 
Total Posts:  13
Joined:  2011-04-19
 

awesome! smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
xstompsquadx
Jr. Member
 
Total Posts:  6
Joined:  2011-03-14
 

Hey I was trying to get your import tracking code working but just couldn\’t get it to upload once everything was added.  When I browse for file and click submit it goes to a blank white page and nothing happens.  Maybe you could help me out with this.  Has anyone else had this issue?  Any help would be greatly appreciated.  Also do you have a sample csv with order#, carrier, and tracking # you guys are using to import?

Thanks Guys

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  901
Joined:  2008-10-21
 

checked your php error_log ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
xstompsquadx
Jr. Member
 
Total Posts:  6
Joined:  2011-03-14
 

This is my entire error log.

2011-07-18 15:28:07.126 [INFO] [24.23.246.70:51252-0#APVH_173.248.188.67_mysite.com] File not found [/home/username/public_html/500.shtml]

2011-07-18 15:25:58.653 [INFO] [173.258.189.67:46469-0#APVH_mysite.com] File not found [/home/usernamen/public_html/403.shtml]

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  901
Joined:  2008-10-21
 

Ok for those who are interested Cron and a Manual tracking import module, I have finally created something that is accessible through a menu in the admin.

Its still very much in Beta at present and would be doing the following.

Current Features:
- Import tracking placed directly onto the server
- Manual upload of csv for instant import (Accessible through a new admin menu)
- Shipment created if none exists
- Tracking number and shipper name imported

Future Features:
- Option to disable tracking cron import
- Set your own CSV headers
- Manual import for large amounts of tracking numbers to be done via the cron import
- Add new status Dispatched (rather than complete)

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  901
Joined:  2008-10-21
 

Latest Current Features:
- Import tracking placed directly onto the server
- Manual upload of csv for instant import (Accessible through a new admin menu)
- Shipment created if none exists
- Tracking number and shipper name imported
- Option to disable tracking cron import
- Set your own CSV headers

Bonus Features
- Create custom tracking codes
- Customise carrier name
- Customise tracking url

Works on version 1.3x, 1.4x and 1.5x

elfling - 21 July 2011 11:27 AM

Ok for those who are interested Cron and a Manual tracking import module, I have finally created something that is accessible through a menu in the admin.

Its still very much in Beta at present and would be doing the following.

Current Features:
- Import tracking placed directly onto the server
- Manual upload of csv for instant import (Accessible through a new admin menu)
- Shipment created if none exists
- Tracking number and shipper name imported

Future Features:
- Option to disable tracking cron import
- Set your own CSV headers
- Manual import for large amounts of tracking numbers to be done via the cron import
- Add new status Dispatched (rather than complete)

Image Attachments
Working.JPGConfig.JPGUpload.JPG
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top