通过跟踪以编程方式为magento订单创建货件 - 获取错误:Mage_Api_Model_Resource_Abstract-> _fault(' not_exists')

时间:2016-03-04 11:40:45

标签: php magento magento-1.9

我以编程方式创建货件并尝试设置跟踪号码:

// ... 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/Shipment/Api.php(138):   Mage_Api_Model_Resource_Abstract-> _fault(' order_not_exist ...&#39)

2 个答案:

答案 0 :(得分:0)

我一直试图通过具有如此多变化的模型来解决这个问题,而且它们都没有工作。我对这种方法感到很累,所以我用这样的直接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(),
        ));
}

这很有效。

答案 1 :(得分:0)

问题(如塞尔吉奥所建议的)是你打电话的时候:$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());我希望它找到正确的顺序。