1

I create shipments programatically and try to set a tracking number like this:

// ... snipped; code that prepares shipment data ...

// Create Shipment
$shipment = $order->prepareShipment($item_qtys);
$shipment->register();
$shipment->sendEmail(false)
         ->setEmailSent(false)
         ->save();
$transactionSave = Mage::getModel('core/resource_transaction')
         ->addObject($shipment)
         ->addObject($shipment->getOrder())
         ->save();
$order->save();

// Add Shipment Tracking Number If Available
if (!empty($order_info->TrackingNumber)) {

    var_dump($shipment->getId());
    var_dump($order->getShippingCarrier()->getCarrierCode());
    var_dump($order->getShippingCarrier()->getConfigData('title'));
    var_dump($order_info->TrackingNumber);
    echo "\r\n";

    Mage::getModel('sales/order_shipment_api')
        ->addTrack(
            $shipment->getId(),
            $order->getShippingCarrier()->getCarrierCode(),
            $order->getShippingCarrier()->getConfigData('title'),
            $order_info->TrackingNumber
        );
}

When the above code executes, I get the following output & exception:

string(7) "1598070"
string(13) "productmatrix"
string(15) "Shipping Option"
string(14) "TEST1234567890"

not_exists - Stack Trace: #0 /var/www/public_html/app/code/core/Mage/Sales/Model/Order/Shipment/Api.php(187): Mage_Api_Model_Resource_Abstract->_fault('not_exists')
#1 /var/www/public_html/app/code/community/Mycompanyname/Mymodulename/controllers/Mymodulename/ApiController.php(1266): Mage_Sales_Model_Order_Shipment_Api->addTrack('1598070', 'productmatrix', 'Shipping Option', 'TEST1234567890')
#2 /var/www/public_html/app/code/community/Mycompanyname/Mymodulename/controllers/Mymodulename/ApiController.php(82): Mycompanyname_Mymodulename_Mymodulename_ApiController->NewOrderShipment('{"MagentoOrderI...')
#3 /var/www/public_html/app/code/community/Mycompanyname/Mymodulename/controllers/Mymodulename/ApiController.php(66): Mycompanyname_Mymodulename_Mymodulename_ApiController->Handle_Request('NewOrderShipmen...', '{"MagentoOrderI...')
#4 /var/www/public_html/app/code/local/Mage/Core/Controller/Varien/Action.php(421): Mycompanyname_Mymodulename_Mymodulename_ApiController->v2Action()
#5 /var/www/public_html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(254): Mage_Core_Controller_Varien_Action->dispatch('v2')
#6 /var/www/public_html/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#7 /var/www/public_html/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#8 /var/www/public_html/app/Mage.php(684): Mage_Core_Model_App->run(Array)
#9 /var/www/public_html/index.php(103): Mage::run('default', 'store')
#10 {main}

Any idea what's causing the error? Is this the incorrect way to add tracking number?

Is it possible to set tracking number as you are creating the shipment?


I've also tried to achieve this using a different method like this:

// If order can be shipped
if ($order->canShip())
{
    // Create shipment
    $shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($item_qtys);
    $shipment = new Mage_Sales_Model_Order_Shipment_Api();
    $shipmentId = $shipment->create($orderId);

    // If tracking number is available
    if (!empty($order_info->TrackingNumber))
    {
        // Add shipment tracking
        $shipment->addTrack(
            $shipmentId,
            $order->getShippingCarrier()->getCarrierCode(),
            $order->getShippingCarrier()->getConfigData('title'),
            $order_info->TrackingNumber
        );
    }
}

This also gives me the following error:

order_not_exists - Stack Trace: #0 /var/www/public_html/app/code/core/Mage/Sales/Model/Order/Shipment/Api.php(138): Mage_Api_Model_Resource_Abstract->_fault('order_not_exist...')

2

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.

2

I needed to add tracking information to an existing shipment associated with an existing order.

Preferring to avoid directly writing to the database, I dug a little deeper into how to achieve this.

Below is the solution that I came to after digging through core source to see what was going on .

// Snipped from larger code, where `$shipment` is a shipment object
// that is already associated with the order.

// Test values:
$shippingCarrierCode = 'custom';
$shippingTitle       = 'Custom Carrier Title';
$trackingNumber      = 'asdf1234';

$track = Mage::getModel('sales/order_shipment_track')
    ->setNumber($trackingNumber)
    ->setCarrierCode($shippingCarrierCode)
    ->setTitle($shippingTitle);

$shipment->addTrack($track)
    ->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.