Maybe you run into the same issue i had. I got emails, not containing the correct updated informations.
i figured out (Magento 1.7.2) that the Email for tracking is generated even before the tracking information was saved. Normally all works fine in default, but sometimes it happened, that the email sending is generated before the tracking is generated or updated.
What i were doing is to add an additional $this->save()
in app/code/core/Mage/Sales/Model/Order/Shipment.php
at the function addTrack
right before the return
statement.
For sure i didn't do this in the core file itself, i've cloned it into app/code/local/Mage/Sales/Model/Order/Shipment.php
My addTrack looks like this now:
public function addTrack(Mage_Sales_Model_Order_Shipment_Track $track)
{
$track->setShipment($this)
->setParentId($this->getId())
->setOrderId($this->getOrderId())
->setStoreId($this->getStoreId());
if (!$track->getId()) {
$this->getTracksCollection()->addItem($track);
}
/**
* Track saving is implemented in _afterSave()
* This enforces Mage_Core_Model_Abstract::save() not to skip _afterSave()
*/
$this->_hasDataChanges = true;
/**
* Added additional $this->save(); to have the new data for shippment
* tracking already in the Mage_Sales_Model_Order_Shipment object.
* If this would not happen, the tracking mails generated may have old
* or wrong data regarding carrier title.
*/
$this->save();
return $this;
}
EDIT 3nd May 2016:
Hi Scot,
in app/code/local/Mage/Sales/Model/Order/Shipment/Api/V2.php
and app/code/local/Mage/Sales/Model/Order/Shipment/Api.php
i've REMOVED $shipment->sendEmail($email, ($includeComment ? $comment : ''));
in the create() function at the try/catch block right after the ->save();
public function create($orderIncrementId, $itemsQty = array(), $comment = null, $email = false,
$includeComment = false
) {
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
/**
* Check order existing
*/
if (!$order->getId()) {
$this->_fault('order_not_exists');
}
/**
* Check shipment create availability
*/
if (!$order->canShip()) {
$this->_fault('data_invalid', Mage::helper('sales')->__('Cannot do shipment for order.'));
}
/* @var $shipment Mage_Sales_Model_Order_Shipment */
$shipment = $order->prepareShipment($itemsQty);
if ($shipment) {
$shipment->register();
$shipment->addComment($comment, $email && $includeComment);
if ($email) {
$shipment->setEmailSent(true);
}
$shipment->getOrder()->setIsInProcess(true);
try {
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($shipment)
->addObject($shipment->getOrder())
->save();
} catch (Mage_Core_Exception $e) {
$this->_fault('data_invalid', $e->getMessage());
}
return $shipment->getIncrementId();
}
return null;
}
But i've added the email send in addTrack()
public function addTrack($shipmentIncrementId, $carrier, $title, $trackNumber)
{
$shipment = Mage::getModel('sales/order_shipment')->loadByIncrementId($shipmentIncrementId);
/* @var $shipment Mage_Sales_Model_Order_Shipment */
if (!$shipment->getId()) {
$this->_fault('not_exists');
}
$carriers = $this->_getCarriers($shipment);
if (!isset($carriers[$carrier])) {
$this->_fault('data_invalid', Mage::helper('sales')->__('Invalid carrier specified.'));
}
$track = Mage::getModel('sales/order_shipment_track')
->setNumber($trackNumber)
->setCarrierCode($carrier)
->setTitle($title);
$shipment->addTrack($track);
try {
$shipment->save();
$track->save();
if(!$shipment->getEmailSent()) {
$shipment->sendEmail(true, $shipment->getCommentsCollection());
$shipment->setEmailSent(true);
$shipment->save();
}
} catch (Mage_Core_Exception $e) {
$this->_fault('data_invalid', $e->getMessage());
}
return $track->getId();
}
Hope i didn't mix up something again.
I've been trying to solve this via models with so many variation and none of them are working. I am quite tired with this approach, so I have solved it using direct SQL like this:
// Add Shipment Tracking Number If Available
if (!empty($order_info->TrackingNumber))
{
// Get db resources
$resource = Mage::getSingleton('core/resource');
$writeConnection = $resource->getConnection('core_write');
$shipmentTrackTable = $resource->getTableName('sales_flat_shipment_track');
// Insert tracking manually
$writeConnection->query(
"INSERT INTO `$shipmentTrackTable` (`parent_id`, `order_id`, `track_number`, `title`, `carrier_code`, `created_at`, `updated_at`)
VALUES (:parent_id, :order_id, :track_number, :title, 'custom', :created_at, :updated_at)",
array(
'parent_id' => $shipment->getId(),
'order_id' => $order->getId(),
'track_number' => $order_info->TrackingNumber,
'title' => 'My Module Name',
'created_at' => now(),
'updated_at' => now(),
));
}
This works.
Best Answer
The problem is that this is PHP code:
You can't execute PHP from inside of an email template - instead, look to a tutorial to implement:
http://forum.azmagento.com/how-to/how-do-i-add-tracking-link-to-shipment-transactional-email-63-nbsp--82195.html
Or an extension:
http://ecommerce.aheadworks.com/magento-extensions/mobile-order-tracking.html
This plugin has the following feature which sounds handy to insert into your transactional shipment email: "Ability to insert order tracking link via widget and CMS"