I have a cronJob written in PHP which reads Tracking numbers from a CSV and updates in Magento.
My code looks like this
if ($order->getShipmentsCollection()->getData()) {
foreach ($order->getShipmentsCollection() as $shipment) {
$shipmentid = $shipment->getId();
$shipment = Mage::getModel('sales/order_shipment')->load($shipmentid);
$trackdata = array();
$trackdata['carrier_code'] = $data[22]; //get CARRIER_NAME from csv
$trackdata['title'] = $data[22]; //get title from csv
$trackdata['number'] = $data[20]; //get CONSIGNMENT_NO from csv
$track = Mage::getModel('sales/order_shipment_track')->addData($trackdata);
$shipment->addTrack($track);
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($shipment)
->addObject($shipment->getOrder())
->save();
$shipment->sendEmail($email, ($includeComment ? $comment : ''));
}
} else {
$convertor = Mage::getModel('sales/convert_order');
$shipment = $convertor->toShipment($order);
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);
}
$trackdata = array();
$trackdata['carrier_code'] = $data[22]; //get carrier_code from csv
$trackdata['title'] = $data[22]; //get title from csv
$trackdata['number'] = $data[20]; //get track number from csv
$track = Mage::getModel('sales/order_shipment_track')->addData($trackdata);
$shipment->addTrack($track);
$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 : ''));
}
It works well but only thing is that does not do what "Send Tracking Information" button, this button
I want to replicate its behaviour.
Actually when "Send Tracking Information" button is clicked email is sent via PackPin API to customer.
But with my code it doesn't send.