magento 创建发货不会更新项目发货数量,订单未完成

yqkkidmi  于 2023-01-17  发布在  其他
关注(0)|答案(3)|浏览(160)

我正在开发一个程序,必须有计划地创建订单已经支付和发票装运。
问题是,即使在创建发货后,订单状态仍为“处理中”,而不是“完成”。如果我从后端手动发货,则不会发生这种情况。
我发现问题是订单项目的发货数量没有更新,而是在保存发货和订单后仍然为0。
这是我正在使用的程序。没有发出任何例外,并且正确创建了装运。

$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) {
               
            }
        }
    }

    /*..........*/
pb3skfrl

pb3skfrl1#

经过2天的挣扎,试图了解问题是什么,研究Magento核心类模块销售,我发现有人在Magento社区谁有similar problems与Magento API和开发了patch .
这个问题是从一年前,但似乎并没有在Magento的后续版本中得到解决,所以我决定采用与扩展相同的解决方案,因此强制订单项目的发货数量等于发货数量,然后,再次保存订单。
好吧,这只是一个补丁,不知道这是否是一个普遍的问题,但对我来说,这是唯一的办法,使这项工作,并最终获得订单的状态'完成'。
我在第一次保存订单后添加了以下代码:

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

希望能对其他人有所帮助。

rt4zxlrg

rt4zxlrg2#

如果您查看一下用于创建发货的SOAP API( Mage _Sales_Model_Order_Shipment_Api::create(...)),您会发现当您在同一事务中保存订单时,这是自动完成的。

/* @var $shipment Mage_Sales_Model_Order_Shipment */
    $shipment = $order->prepareShipment($itemsQty);
    if ($shipment) {
        $shipment->register();
        $shipment->addComment($comment, $email && $includeComment);
        if ($email) {
            $shipment->setEmailSent(true);
        }
        $shipment->getOrder()->setIsInProcess(true);
        try {
            $transactionSave = Mage::getModel('core/resource_transaction')
                ->addObject($shipment)
                ->addObject($shipment->getOrder())
                ->save();
            $shipment->sendEmail($email, ($includeComment ? $comment : ''));
        } catch (Mage_Core_Exception $e) {
            $this->_fault('data_invalid', $e->getMessage());
        }
        return $shipment->getIncrementId();
    }

我还注意到,我可以创建跟踪( Mage _Sales_Model_Order_Shipment_Track)并在保存之前将其添加到货件中,还可以将跟踪添加到事务中:

$track = Mage::getModel('sales/order_shipment_track')
        ->setNumber($trackingNumber)
        ->setCarrierCode($carrier)
        ->setTitle($serviceTitle);

    $shipment->addTrack($track);
    $transactionSave = Mage::getModel('core/resource_transaction')
        ->addObject($shipment)
        ->addObject($shipment->getOrder())
        ->addObject($track)
        ->save();

所以,没必要自己动手。

3wabscal

3wabscal3#

我遇到了同样的问题,但在我的案例中,我发现在发货登记后,销售订单再次保存关系,使qty_shipped变为0。
所以我的解决方案是添加一个插件到Magento\Sales\Model\Order\Shipment::register(),如下所示:

public function aroundRegister(
    \Magento\Sales\Model\Order\Shipment $subject,
    \Closure $proceed
) {
    if ($subject->getId()) {
        throw new \Magento\Framework\Exception\LocalizedException(
            __('We cannot register an existing shipment')
        );
    }

    $totalQty = 0;

    $orderItems = [];
    /** @var \Magento\Sales\Model\Order\Shipment\Item $item */
    foreach ($subject->getAllItems() as $item) {
        if ($item->getQty() > 0) {
            $item->register();
            $orderItems[] = $item->getOrderItem();
            if (!$item->getOrderItem()->isDummy(true)) {
                $totalQty += $item->getQty();
            }
        }
    }

    //make sure the updated Item be set in order so the save order relation will be correct
    $subject->getOrder()->setItems($orderItems);

    $subject->setTotalQty($totalQty);

    return $subject;
}

相关问题