0
votes

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

2 Answers

0
votes

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.

0
votes

The issue (as suggseted by sergio) is when you are trying to call: $shipmentId = $shipment->create($orderId);

Here, you're not actually sending through the order ID as your comment suggests, however, looking at Mage_Sales_Model_Order_Shipment_Api create method, you need to pass through the order increment_id rather than the entity_id as this is how it loads the order in the first line.

/**
 * Create new shipment for order
 *
 * @param string $orderIncrementId
 * @param array $itemsQty
 * @param string $comment
 * @param booleam $email
 * @param boolean $includeComment
 * @return string
 */
public function create($orderIncrementId, $itemsQty = array(), $comment = null, $email = false,
    $includeComment = false
) {
    $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);

If you update your code to: $shipmentId = $shipment->create($order->getIncrementId()); I'd expect it to find the right order.