创建装运不会更新物料的装运数量,并且订单不完整 [英] Creating shipment does not update items shipped quantity and order is not complete

查看:54
本文介绍了创建装运不会更新物料的装运数量,并且订单不完整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个程序,该程序必须以编程方式为已付款和已开具发票的订单创建货件.

I'm developing a procedure that has to programatically create shipment for orders that are already paid and invoiced.

问题在于,即使在创建装运后,订单状态仍保持为处理中"而不是完成". 如果我从后端手动发货,则不会发生这种情况.

The problem is that even after creating the shipment, the order status remains to 'processing' instead of going to 'complete'. This does not happen if I manually Ship from the backend.

我发现问题是订单商品的发货数量没有更新,但是在保存发货和订单后仍为0.

I found that the problem is that the quantity shipped for the order items is not updated, but remains 0 after saving the shipment and the order.

这是我正在使用的过程. 没有发出异常,并且正确创建了装运.

This is the procedure i'm using. No exception is issued, and shipment is correctly created.

    $orders = $this->_orderCollectionFactory->create()
        ->addAttributeToSelect('*')
        ->addFieldToFilter( 'entity_id' , array('in' => $ordersIdsArr) )
        ->setOrder('created_at', 'desc' );   

    foreach ($orders as $index => $order) {
        if ($order->canShip()) { 
           $shipment = $this->_convertOrder->toShipment($order);;
           $orderItems = $order->getItemsCollection()->addAttributeToSelect('*')->load();

           foreach ($orderItems as $orderItem) {
                if (! $orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
                    continue;
                }
                $qtyShipped = $orderItem->getQtyToShip();
                $shipmentItem = $this->_convertOrder->itemToShipmentItem($orderItem)->setQty($qtyShipped);
                $shipment->addItem($shipmentItem); 
            }
            $shipment->register();
            $shipment->getOrder()->setIsInProcess(true);

            try {

                $saveTransaction = $this->_transactionFactory->create();
                $saveTransaction->addObject($shipment)
                    ->addObject($shipment->getOrder());
                $saveTransaction->save();
            } catch (\Exception $e) {

            }
        }
    }

    /*..........*/

有任何线索吗?

推荐答案

经过2天的努力,试图了解问题所在,研究了Magento模块销售的核心类之后,我发现Magento社区中有人Magento API 类似问题,并开发了

After struggling for 2 days on this, trying to understand what the problem was, studying Magento core classes for module-sales, I found someone on Magento community who had similar problems with Magento API and developed a patch.

问题出在一年前,但在随后的Magento版本中似乎未解决,因此我决定采用与扩展程序相同的解决方案,因此迫使订购商品的发货数量相等到要运送的数量,然后再次保存订单.

The problem is from one year ago, but doesn't seem to have been addressed in subsequent versions of Magento, so I decided to adopt the same solution as the extension does, hence forcing the shipped quantity of order items to be equal to the quantity to ship, and then, save the order again.

好吧,如果这是一个普遍的问题,那只是一个补丁而已,但对我而言,这是使它工作并最终获得状态为完成"的唯一方法.

Well, it is just a patch and dunno if it is a general problem, but to me it has been the only way to make this work, and get finally the order in status of 'Complete'.

我加入此代码的第一后节省的顺序的:

I added this code after the first save of the order:

try {
        $saveTransaction = $this->_transactionFactory->create();
        $saveTransaction->addObject($shipment)
             ->addObject($shipment->getOrder());
        $saveTransaction->save();

        $itemsCheck = $order->getItemsCollection()->addAttributeToSelect('*')->load();
        foreach ($itemsCheck as $item) {
            if (! $item->getQtyToShip() || $item->getIsVirtual()) { 
                   continue;
            }
            $item->setQtyShipped($item->getQtyToShip());
            $item->save();
            $Norder = $shipment->getOrder()->load( $shipment->getOrder()->getId() );
            $Norder->save();
        }
    } 

希望对其他人有帮助.

Hope it can be of help for someone else.

这篇关于创建装运不会更新物料的装运数量,并且订单不完整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆