0

一些以前的程序员使用 Magento SOAP API V1 编写了这个程序,以将已发货的订单标记为已发货。在以前的 Magento vs 1.5 平台上它运行良好,但现在在 vs 1.7 上,跟踪号本身没有被导入。正如你在中途看到的那样,我的名字被注释掉了 //Caitlin。上面的那一行是以前的程序员写的,之后的两行是我认为 Magento vs 1.7 的代码,但我上次尝试这个片段时,我停止了他们的操作。这对您来说是否正确?有任何想法吗?

   $comment = '<b><br>*** Order has shipped. ***</b><br/><br/>' . 
                           '<b>3PL order number:</b> ' .  $fields[1] . '<br/>' . 
                           '<b>Weight:</b> ' .  $fields[2] . '<br/>' . 
                           '<b>Shipped via:</b> ' .  $fields[3] . '<br/>' . 
                           '<b>Tracking number:</b> ' .  $fields[4] . '<br/>' . 
                           '<b>Ship date:</b> ' .  $fields[5] . '<br/>' . 
                           '<b>Postage:</b> ' .  $fields[6] . '<br/>' . 
                           '<b>Fulfillment:</b> ' .  $fields[7] . '<br/>' . 
                           '<b>Per packslip:</b> ' .  $fields[8];

            // Make shipment and add tracking number
            if ($fields[3] == 'UPS-RESIDENTIAL') { $shippedby = 'ups'; $shipname = 'UPS Ground'; }
            elseif ($fields[3] == 'UPS-2') { $shippedby = 'ups'; $shipname = 'UPS 2nd Day Air'; }
            elseif ($fields[3] == 'UPS-OVERNIGHT') { $shippedby = 'ups'; $shipname = 'UPS Next Day Air Saver'; }
            elseif ($fields[3] == 'USPS-PRI') { $shippedby = 'usps'; $shipname = 'USPS Priority'; }
            elseif ($fields[3] == 'CANADA') { $shippedby = 'custom'; $shipname = 'MSI Canada (Standard) '; }
            elseif ($fields[3] == 'MSITRACK') { $shippedby = 'custom'; $shipname = 'MSI Canada (Express)'; }
            else { $shippedby = 'custom'; }
            // Attempt to create the order, notify on failure
            try { 
            $newShipmentId = $client->call($sess_id, 'sales_order_shipment.create', array($ShippedOrderId, array(), $comment, true, false, $shippedby, $shipname, $fields[4]));

            //Caitlin
            //$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]));
            }
            catch (Exception $e) { echo 'Shipment creation failed on order '. $ShippedOrderId . ': ', $e->getMessage(); }


            // Add comment to order with all the info
             $client->call($sess_id, 'sales_order.addComment', array($ShippedOrderId, 'complete',  $comment,  false));
            $mail_content .= $line . "\n";
            $importcount++;
         }
      //}
   }

编辑 2/25/13


使用下面的实现。运行此脚本时出错。我无法测试它,因为我必须在 cron 在凌晨 5 点运行时进行测试。

// Make shipment and add tracking number
            if ($fields[3] == 'UPS-RESIDENTIAL') { $shippedby = 'ups'; $shipname = 'UPS Ground'; }
            elseif ($fields[3] == 'UPS-2') { $shippedby = 'ups'; $shipname = 'UPS 2nd Day Air'; }
            elseif ($fields[3] == 'UPS-OVERNIGHT') { $shippedby = 'ups'; $shipname = 'UPS Next Day Air Saver'; }
            elseif ($fields[3] == 'USPS-PRI') { $shippedby = 'usps'; $shipname = 'USPS Priority'; }
            elseif ($fields[3] == 'CANADA') { $shippedby = 'custom'; $shipname = 'MSI Canada (Standard) '; }
            elseif ($fields[3] == 'MSITRACK') { $shippedby = 'custom'; $shipname = 'MSI Canada (Express)'; }
            else { $shippedby = 'custom'; }


        /////////////////////////////////////////////       
        $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);

        $shipment_collection = Mage::getResourceModel('sales/order_shipment_collection');
        $shipment_collection->addAttributeToFilter('order_id', $orderId);
        $shipment_collection->load();

        $firstItem = $shipment_collection->getFirstItem();

        if(count($shipment_collection) > 1)
        {

            $track_no = $fields[4]; // insert tracking # string here

                $shipment = Mage::getModel('sales/order_shipment');
                $shipment->load($firstItem->getId());
                if($shipment->getId() != '')
                {
                    $track = Mage::getModel('sales/order_shipment_track')
                        ->setShipment($shipment)
                        ->setData('title', $shipname) // User syntax correct name here
                        ->setData('number', $track_no)
                        ->setData('carrier_code', $shippedby) // use code that matches DB code for ship method here
                        ->setData('order_id', $shipment->getData('order_id'));

                    $track->save();
                }

            return true;

        } else {

            $orderShip = $order->prepareShipment(); // can take sku => qty array
            $orderShip->register();
            $orderShip->sendEmail();

            $tracker = Mage::getModel( 'sales/order_shipment_track' );
            $tracker->setShipment( $orderShip );
            $tracker->setData( 'title', $shipname );
            $tracker->setData( 'number', $importData['Tracking Number'] );
            $tracker->setData( 'carrier_code', $shippedby );
            $tracker->setData( 'order_id', $orderId );

            $orderShip->addTrack($tracker);
            $orderShip->save();

            $order->setData('state', "complete");
            $order->setStatus("complete");
                $history = $order->addStatusHistoryComment('Order marked as complete by shipment code.', false);
                $history->setIsCustomerNotified(false);
            $order->save(); 
            /////////////////////////////////////////////////








            // Add comment to order with all the info
             $client->call($sess_id, 'sales_order.addComment', array($ShippedOrderId, 'complete',  $comment,  false));
            $mail_content .= $line . "\n";
            $importcount++;
         }
      //}
   }
4

2 回答 2

3

上面的实现几乎对我有用,但我无法添加跟踪号码。我最终回到测试 Magento soap API 的代码。以下还添加了跟踪号:

 try { 
            // Create new shipment
            $newShipmentId = $client->call($sess_id, 'sales_order_shipment.create', array($ShippedOrderId, array(), 'Shipment Created', true, true));
            $newTrackId = $client->call($sess_id, 'sales_order_shipment.addTrack', array($newShipmentId, $shippedby, $shipname, $fields[4]));
            }
            catch (Exception $e) { echo 'Shipment creation failed on order '. $ShippedOrderId . ': ', $e->getMessage(); }

不敢相信我花了这么多时间,因为我以前试过这个,我想我只是搞砸了一个变量。乐于帮助任何可能需要额外帮助的人。

于 2013-03-13T00:16:20.663 回答
0

我会一起去掉 API 的使用。

试试这个:

$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);

        $shipment_collection = Mage::getResourceModel('sales/order_shipment_collection');
        $shipment_collection->addAttributeToFilter('order_id', $orderId);
        $shipment_collection->load();

        $firstItem = $shipment_collection->getFirstItem();

        if(count($shipment_collection) > 1)
        {

            $track_no = "FEDEX9879879"; // insert tracking # string here

                $shipment = Mage::getModel('sales/order_shipment');
                $shipment->load($firstItem->getId());
                if($shipment->getId() != '')
                {
                    $track = Mage::getModel('sales/order_shipment_track')
                        ->setShipment($shipment)
                        ->setData('title', 'United Parcel Service') // User syntax correct name here
                        ->setData('number', $track_no)
                        ->setData('carrier_code', 'ups') // use code that matches DB code for ship method here
                        ->setData('order_id', $shipment->getData('order_id'));

                    $track->save();
                }

            return true;

        } else {

            $orderShip = $order->prepareShipment(); // can take sku => qty array
            $orderShip->register();
            $orderShip->sendEmail();

            $tracker = Mage::getModel( 'sales/order_shipment_track' );
            $tracker->setShipment( $orderShip );
            $tracker->setData( 'title', 'United Parcel Service' );
            $tracker->setData( 'number', $importData['Tracking Number'] );
            $tracker->setData( 'carrier_code', 'ups' );
            $tracker->setData( 'order_id', $orderId );

            $orderShip->addTrack($tracker);
            $orderShip->save();

            $order->setData('state', "complete");
            $order->setStatus("complete");
                $history = $order->addStatusHistoryComment('Order marked as complete by shipment code.', false);
                $history->setIsCustomerNotified(false);
            $order->save();

请注意,orderShip 的保存会自动保存跟踪器,并且您不能单独保存跟踪器对象,因为它会失败外键约束。

于 2013-02-21T06:26:32.437 回答