Magento SOAP API: Sales Order Shipment Creation and Tracking Number Addition
Introduction
The Magento SOAP API provides powerful functionality for managing sales orders programmatically. This includes creating shipments and adding tracking numbers to them. In this post, we’ll address a common issue faced by developers working with the Magento 1.7 SOAP API V1 regarding tracking number import failures.
The Problem: Tracking Numbers Not Being Imported
A developer encountered an issue where tracking numbers were not being imported successfully using the Magento SOAP API on version 1.7. This was despite the code working correctly on a previous version (Magento vs 1.5). Let’s dive into the provided code snippets to understand the problem.
Analyzing the Code Snippets
Original Code
The original code attempt uses the sales_order_shipment.create service call to create a shipment and adds tracking information directly in the same method:
$newShipmentId = $client->call($sess_id, 'sales_order_shipment.create', array($ShippedOrderId, array(), $comment, true, false, $shippedby, $shipname, $fields[4]));
This approach seems to work on the older version of Magento but fails when migrated to version 1.7.
Attempted Solution
The developer attempted a different approach by first creating the shipment without adding tracking information and then using sales_order_shipment.addTrack to append the tracking details:
$newShipmentId = $client->call($sess_id, 'sales_order_shipment.create', array($ShippedOrderId, array(), $comment, true, false));
$newTrackId = $proxy->call($sessionId, 'sales_order_shipment.addTrack', array($newShipmentId, $shippedby, $shipname, $fields[4]));
However, this attempt also resulted in a halt to the operations when executed.
A Better Approach: Using Magento’s Order and Shipment Models
To resolve this issue successfully, we should leverage Magento’s built-in models for working with orders and shipments. This approach ensures compatibility across different versions of Magento.
Step 1: Load the Existing Order
First, load the order by its increment ID using the sales/order model:
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
Step 2: Check for Existing Shipments
Retrieve any existing shipments associated with the order and filter them by the order’s ID:
$shipment_collection = Mage::getResourceModel('sales/order_shipment_collection');
$shipment_collection->addAttributeToFilter('order_id', $orderId);
$shipment_collection->load();
Step 3: Create a New Shipment or Add Tracking to an Existing One
Case 1: No existing shipments found
If no shipments are found for the order, create a new shipment using the prepareShipment method and register it:
$orderShip = $order->prepareShipment();
$orderShip->register();
$orderShip->sendEmail();
Case 2: Existing shipments found
If one or more shipments already exist for the order, retrieve the first shipment and add a new track to it:
$shipment = Mage::getModel('sales/order_shipment')->load($firstItem->getId());
if ($shipment->getId() != '') {
$track = Mage::getModel('sales/order_shipment_track')
->setShipment($shipment)
->setData('title', $shipname)
->setData('number', $importData['Tracking Number'])
->setData('carrier_code', $shippedby)
->setData('order_id', $shipment->getData('order_id'));
$track->save();
}
Step 4: Save the Updated Order Status
After successfully creating or updating the shipment, save the order’s status as “complete”:
$order->setData('state', 'complete');
$order->setStatus('complete');
$history = $order->addStatusHistoryComment('Order marked as complete by shipment code.', false);
$history->setIsCustomerNotified(false);
$order->save();
Conclusion
By using Magento’s built-in order and shipment models, we can reliably create new shipments or add tracking numbers to existing ones across various versions of the platform. The key is to follow the correct approach of first checking for existing shipments and then either creating a new one or adding a track to an existing shipment.
Remember that this approach requires proper access to Magento’s internal models, which may necessitate additional permissions depending on your deployment environment.
For more advanced Magento development needs, consider exploring the extensive resources available at LaravelCompany, including in-depth tutorials and expert guidance.