1.create a csv file . In it put following fields OrderNumber,Email,TrackingNumber,Carrier . & csv's name is orders_tracking_import.csv
2 Create ordertracking.php file in your magento's root directory
<?php
require_once("app/Mage.php");
Mage::app();
if (isset($_POST['MAX_FILE_SIZE'])) {
$email = true;
$target_path = basename( $_FILES['uploadedfile']['name']);
$_FILES['uploadedfile']['tmp_name'];
$target_path = basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
?>
The file <?php echo basename( $_FILES['uploadedfile']['name']); ?> has been uploaded succesfully!
<?php
ini_set("auto_detect_line_endings", 1);
$current_row = 1;
$handle = fopen($target_path, "r");
$csvData = array();
while ( ($data = fgetcsv($handle, 10000, ",") ) !== FALSE )
{
$number_of_fields = count($data);
//print_r($number_of_fields);exit;
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) {
$includeComment = false;
$comment = NULL;
$order = Mage::getModel('sales/order')->loadByIncrementId($rec['OrderNumber']);
//This converts the order to "Completed".
if ($order->canShip())
{
$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;
// FOR GUIDANCE ON THIS SECTION LOOK AT MY FIRST POST... IT HAS BETTER COMMENTS
if ($rec['Carrier'] == 'ups') {
$carrierTitle = 'United Parcel Service';
}
if ($rec['Carrier'] == 'dhl') {
$carrierTitle = 'DHL (Deprecaied)';
}
if ($rec['Carrier'] == 'fedex') {
$carrierTitle = 'Federal Express';
}
if ($rec['Carrier'] == 'dhlint') {
$carrierTitle = 'DHL';
}
if ($rec['Carrier'] == 'usps') {
$carrierTitle = 'United States Postal Service';
}
if ($rec['Carrier'] == 'custom') {
$carrierTitle = 'Custom Value';
}
$data = array();
$data['carrier_code'] = $rec['Carrier'];
$data['title'] = $carrierTitle;
$data['number'] = $rec['TrackingNumber'];
$track =Mage::getModel('sales/order_shipment_track')->addData($data);
$shipment->addTrack($track);
if ($order->canShip()){
$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($rec['Email'], ($includeComment ? $comment : ''));
$shipment->save();
}
else {
$track->save();
$shipment->sendEmail($rec['Email'], ($includeComment ? $comment : ''));
$order->setStatus('complete');
$order->addStatusToHistory('complete', '', false);
$order->save();
}
}
} //end if statment for file upload check
} else { // end if statement for post check for upload
?>
<form enctype="multipart/form-data" action="" method="POST">
<p>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000000" />
CSV File:
<input name="uploadedfile" type="file" />
</p>
<p>
<input type="submit" name="upload" id="upload" value="Submit" />
</p>
</form>
<?php
}
?>
2 Create ordertracking.php file in your magento's root directory
<?php
require_once("app/Mage.php");
Mage::app();
if (isset($_POST['MAX_FILE_SIZE'])) {
$email = true;
$target_path = basename( $_FILES['uploadedfile']['name']);
$_FILES['uploadedfile']['tmp_name'];
$target_path = basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
?>
The file <?php echo basename( $_FILES['uploadedfile']['name']); ?> has been uploaded succesfully!
<?php
ini_set("auto_detect_line_endings", 1);
$current_row = 1;
$handle = fopen($target_path, "r");
$csvData = array();
while ( ($data = fgetcsv($handle, 10000, ",") ) !== FALSE )
{
$number_of_fields = count($data);
//print_r($number_of_fields);exit;
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) {
$includeComment = false;
$comment = NULL;
$order = Mage::getModel('sales/order')->loadByIncrementId($rec['OrderNumber']);
//This converts the order to "Completed".
if ($order->canShip())
{
$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;
// FOR GUIDANCE ON THIS SECTION LOOK AT MY FIRST POST... IT HAS BETTER COMMENTS
if ($rec['Carrier'] == 'ups') {
$carrierTitle = 'United Parcel Service';
}
if ($rec['Carrier'] == 'dhl') {
$carrierTitle = 'DHL (Deprecaied)';
}
if ($rec['Carrier'] == 'fedex') {
$carrierTitle = 'Federal Express';
}
if ($rec['Carrier'] == 'dhlint') {
$carrierTitle = 'DHL';
}
if ($rec['Carrier'] == 'usps') {
$carrierTitle = 'United States Postal Service';
}
if ($rec['Carrier'] == 'custom') {
$carrierTitle = 'Custom Value';
}
$data = array();
$data['carrier_code'] = $rec['Carrier'];
$data['title'] = $carrierTitle;
$data['number'] = $rec['TrackingNumber'];
$track =Mage::getModel('sales/order_shipment_track')->addData($data);
$shipment->addTrack($track);
if ($order->canShip()){
$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($rec['Email'], ($includeComment ? $comment : ''));
$shipment->save();
}
else {
$track->save();
$shipment->sendEmail($rec['Email'], ($includeComment ? $comment : ''));
$order->setStatus('complete');
$order->addStatusToHistory('complete', '', false);
$order->save();
}
}
} //end if statment for file upload check
} else { // end if statement for post check for upload
?>
<form enctype="multipart/form-data" action="" method="POST">
<p>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000000" />
CSV File:
<input name="uploadedfile" type="file" />
</p>
<p>
<input type="submit" name="upload" id="upload" value="Submit" />
</p>
</form>
<?php
}
?>
No comments:
Post a Comment