Magento:更改现有订单的送货方式

时间:2015-09-24 17:45:59

标签: php magento shopping-cart shipping quote

我试图改变Magento现有订单的运费。这在管理员后端工作正常,即使这是一个非常重要的过程,因为我必须在发货地址对象上设置新的送货方法并重新计算报价总计后手动更新大量订单字段/属性。

我的问题是,当在前端运行相同的代码时,它根本不起作用,报价collectTotals将还原我在shippingAddress中所做的任何更改,而我不知道如何解决它或它为什么从后端工作。

它的外观如下:

    $shippingAddress = $quote->getShippingAddress();

    $shippingAddress->setShippingMethod('dynamicshipping_'.$shippingCode);
    $shippingAddress->setCollectShippingRates(true);
    $shippingAddress->collectShippingRates();

    $quote->setUseCustomerBalance(1)->setTotalsCollectedFlag(false)->collectTotals()->save();

    $order->setShippingHiddenTaxAmount($shippingAddress->getShippingHiddenTaxAmount());
    $order->setBaseShippingHiddenTaxAmount($shippingAddress->getBaseShippingHiddenTaxAmount());
    $order->setBaseShippingHiddenTaxAmnt($shippingAddress->getBaseShippingHiddenTaxAmnt());
    $order->setShippingInclTax($shippingAddress->getShippingInclTax());
    $order->setBaseShippingInclTax($shippingAddress->getBaseShippingInclTax());
    $order->setShippingTaxAmount($shippingAddress->getShippingTaxAmount());
    $order->setBaseShippingTaxAmount($shippingAddress->getBaseShippingTaxAmount());
    $order->setShippingAmount($shippingAddress->getShippingAmount());
    $order->setBaseShippingAmount($shippingAddress->getBaseShippingAmount());
    $order->setShippingDiscountAmount($shippingAddress->getShippingDiscountAmount());
    $order->setBaseShippingDiscountAmount($shippingAddress->getBaseShippingDiscountAmount());
    $order->setGrandTotal($shippingAddress->getGrandTotal());
    $order->setBaseGrandTotal($shippingAddress->getBaseGrandTotal());
    $order->setShippingMethod('dynamicshipping_'.$shippingCode);
    $order->setShippingDescription($shippingDescription);

    $order->setServicePoint($servicePoint);
    $order->save();

正如我所说,每次从后端开始工作都很好,但是从前端调用时却没有。

我尝试过各种变体,例如尝试消除旧运输方法的任何痕迹,没有运气。

    $quote->getShippingAddress()->removeAllShippingRates()
        ->setShippingMethod('dynamicshipping_'.$shippingCode)
        ->setShippingDescription($shippingDescription)
        //->setBaseShippingAmount(0)
        //->setBaseShippingTaxAmount(0)
        //->setShippingTaxAmount(0)
        //->setShippingInclTax(0)
        ->setCollectShippingRates(true)
        //->unsetData('cached_items_all')
        //->unsetData('cached_items_nominal')
        //->unsetData('cached_items_nonnominal')
        ->collectShippingRates()
        //->collectTotals()
        ->save();

在我调用collectTotals时,无论我做什么,它都会让我觉得报价是使用旧版/不同版本的送货地址。

有任何建议,或者可能会了解它是如何在后端而不是前端工作的?

修改

经过更多调试后,我可以看到运费在前端和后端都发生了变化。问题是,只有在通过后端运行此代码时才会更改费用。很奇怪。它只是拒绝更新运费。

1 个答案:

答案 0 :(得分:2)

看起来我在collectTotals的观察者身上遇到了一些问题,这就是它在事件没有被解雇的后端工作的原因。

完整的参考代码,我最近更改为使用更加自动防故障的方法将所有字段复制回订单。

    /* @var $order Mage_Sales_Model_Order */
    /* @var $quote Mage_Sales_Model_Quote */

    $shippingAddress = $quote->getShippingAddress();
    $shippingAddress->setShippingMethod('dynamicshipping_'.$shippingCode);
    $shippingAddress->setShippingDescription($shippingDescription);

    $shippingAddress->setCollectShippingRates(true)->collectShippingRates();
    $quote->collectTotals();

    if ($this->updateMagentoOrder($order, $quote)) {

        // here's where I check if we successfully updated the authorized
        // amount at the payment gateway, before saving anything
        // wrapping the payment update and save in a try-catch

        $quote->save();
        $order->save();
    }

使用此方法更新所有订单字段:

/**
 * Updates a Magento order based on quote changes
 * will not save anything, up to the caller.
 * deleting items not supported.
 *
 * @param  $order Mage_Sales_Model_Order
 * @param  $quote Mage_Sales_Model_Quote
 * @return bool
 */
public function updateMagentoOrder($order, $quote) {
    if (!$order instanceof Mage_Sales_Model_Order || !$quote instanceof Mage_Sales_Model_Quote) {
        return false;
    }

    try {
        $converter = Mage::getSingleton('sales/convert_quote');
        $converter->toOrder($quote, $order);

        foreach ($quote->getAllItems() as $quoteItem) {

            $orderItem     = $converter->itemToOrderItem($quoteItem);
            $quoteItemId   = $quoteItem->getId();
            $origOrderItem = empty($quoteItemId) ? null : $order->getItemByQuoteItemId($quoteItemId);

            if ($origOrderItem) {
                $origOrderItem->addData($orderItem->getData());
            } else {
                if ($quoteItem->getParentItem()) {
                    $orderItem->setParentItem(
                        $order->getItemByQuoteItemId($quoteItem->getParentItem()->getId())
                    );
                    $orderItem->setParentItemId($quoteItem->getParentItemId());
                }
                $order->addItem($orderItem);
            }
        }

        if ($shippingAddress = $quote->getShippingAddress()) {
            $converter->addressToOrder($shippingAddress, $order);
        }
    } catch (Exception $e) {
        Mage::logException($e);
        return false;
    }

    return true;
}

作为参考,上面的方法可以首先循环$order->getAllItems()并对它们$orderItem->cancel()->delete(); - 但我现在不支持删除项目。

删除前的cancel()部分是为了使CatalogInventory模块可以恢复库存。它正在聆听sales_order_item_cancel事件。