2016-03-04 33 views
0

我編程方式創建出貨量和嘗試設置這樣一個跟蹤號碼:編程方式創建用於跟蹤Magento的訂單出貨 - 收到錯誤:Mage_Api_Model_Resource_Abstract - > _故障(「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 - 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...')

回答

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

This works。

0

(如suggseted由塞爾吉奧)的問題是,當你試圖撥打:$shipmentId = $shipment->create($orderId);

在這裏,你無法真正通過訂單ID發送作爲您的意見建議,但是,看着Mage_Sales_Model_Order_Shipment_Apicreate方法,您需要通過訂單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());我希望它找到正確的順序。