首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Magento sales_order_shipment创建和addTrack问题

Magento sales_order_shipment创建和addTrack问题
EN

Stack Overflow用户
提问于 2013-02-21 04:54:46
回答 2查看 4.2K关注 0票数 0

一些前程序员使用Magento V1编写了这个程序,以标记已发送的订单。在以前的MagentoVS1.5平台上,它工作得很好,但是现在在vs 1.7上,跟踪号码本身并没有被导入。正如你所看到的,我的名字被注释掉了//Caitlin。上面的一行是前程序员放的,后面的两行是我认为Magento vs 1.7的代码,但我上次尝试这个片段时,他们的操作停止了。你觉得这是对的吗?有什么想法吗?

代码语言:javascript
复制
   $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

使用下面的实现。我运行这个脚本时出错了。不过,我一直无法测试它,因为我不得不在凌晨5点运行cron。

代码语言:javascript
复制
// 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++;
         }
      //}
   }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-21 06:26:32

我会把API的使用全部去掉。

试试这个:

代码语言:javascript
复制
$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的保存会自动保存跟踪器,并且您无法单独保存跟踪器对象,因为它将失败外键约束。

票数 0
EN

Stack Overflow用户

发布于 2013-03-13 00:16:20

上面的实现几乎适用于我,但我无法添加跟踪编号。我最终回到了测试Magento中的代码。以下内容还添加了跟踪号码:

代码语言:javascript
复制
 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(); }

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14994809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档