'无法创建空货物'同时尝试在magento中创建货物

时间:2016-05-22 07:23:06

标签: magento-1.9

我正在尝试在下订单后创建货件并添加跟踪号,但我收到的错误如无法创建空货件。当我通过谷歌搜索我得到的原因之一是项目数量为空,但我在下面使用的是返回订购产品的确切数量。

  

$ itemQty = $ order-> getItemsCollection() - > count();

我不确切地知道这只是出错的原因。我做错了什么?有谁知道请帮帮我。

 public function salesOrderInvoiceShipmentCreate($observer)
    {
        //  $order = $observer->getEvent()->getOrder();
        //$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
        $order_id = $observer->getData('order_ids');
        $order = Mage::getModel('sales/order')->load($order_id);

        $token = '3acb6561b04117c6cbe3552c90f1d6815507e257';
        $waybill_url = 'https://track.delhivery.com/waybill/api/fetch/json/?token='.$token.'&cl=GEECHOO';
        $waybill = file_get_contents($waybill_url);

        Mage::log($order, Zend_Log::INFO, 'order.log', true);

        if (!$order->getId()) {
            Mage::throwException('Order does not exist, for the Shipment process to complete');
        }

      if ($order->canShip()) {
          try {
              //    $shipment = Mage::getModel('sales/service_order', $order)
        //       ->prepareShipment($this->_getItemQtys($order));
              $itemQty = $order->getItemsCollection()->count();
              $shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty);

              $shipmentCarrierCode = '';
              $shipmentCarrierTitle = '';

              $arrTracking = array(
               'carrier_code' => isset($shipmentCarrierCode) ? $shipmentCarrierCode : $order->getShippingCarrier()->getCarrierCode(),
               'title' => isset($shipmentCarrierTitle) ? $shipmentCarrierTitle : $order->getShippingCarrier()->getConfigData('title'),
               'tracking_number' => $waybill,
           );

              $track = Mage::getModel('sales/order_shipment_track')->addData($arrTracking);
              $shipment->addTrack($track);

           // Register Shipment
           $shipment->register();

           // Save the Shipment
          $this->_saveShipment($shipment, $order);

           // Finally, Save the Order
           $this->_saveOrder($order);
          } catch (Exception $e) {
              throw $e;
          }
      }
    }

1 个答案:

答案 0 :(得分:0)

请尝试订购商品数量

$quote = Mage::helper('checkout/cart')->getCart()->getQuote();
$value = [];
foreach ($quote->getAllItems() as $item) {
$value[]= array (
'id' => $item->getSku(),
'quantity' => $item->getQty(),

}