以编程方式为具有跟踪功能的magento订单创建货件 - 收到错误:Mage_Api_Model_Resource_Ab


Programatically create shipment for magento order with tracking - Getting error: Mage_Api_Model_Resource_Abstract->_fault('not_exists')

>我以编程方式创建货件并尝试设置如下所示的跟踪号:

// ... 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
        );
}

当上面的代码执行时,我得到以下输出和异常:

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}

知道是什么导致了错误吗?这是添加跟踪号的错误方法吗?

是否可以

在创建货件时设置跟踪号?


我也尝试使用不同的方法来实现这一点,如下所示:

// 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
        );
    }
}

这也给了我以下错误:

order_not_exists - 堆栈跟踪:#0 /var/www/public_html/app/code/core/Mage/Sales/Model/Order/Shipping/Api.php(138): Mage_Api_Model_Resource_Abstract->_fault("order_not_exist...")

我一直

在尝试通过具有如此多变化的模型来解决这个问题,但没有一个有效。我对这种方法很厌倦,所以我使用这样的直接 SQL 解决了它:

// 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(),
        ));
}

这行得通。

问题(正如塞尔吉奥所建议的那样)是当您尝试调用时: $shipmentId = $shipment->create($orderId);

在这里,您实际上并没有像注释所暗示的那样通过订单 ID 发送,但是,查看Mage_Sales_Model_Order_Shipment_Api create方法,您需要传递订单increment_id而不是the entity_id因为这是它在第一行加载订单的方式。

/**
 * 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);

如果将代码更新为: $shipmentId = $shipment->create($order->getIncrementId());我希望它能找到正确的顺序。