将magento admin quote转换为多地址顺序

时间:2014-06-18 18:15:08

标签: magento

假设我从管理创建订单中引用了已输入的所有产品,我怎么能写一些代码来添加多个送货地址并将项目分配到地址。

我有一些代码工作,基于前端多芯片模型。我遇到的问题是添加多个地址并将项目分配给报价。如果我有标准报价,我该如何添加多个地址?

我已经尝试过了:

 $address = Mage::getModel('customer/address')->load($addressID);
 $quote->addShippingAddress($address);

这似乎只包含一个地址

整个功能:

 public function createMultiOrders() {
    // print_r($_POST);
    $items = $_POST['item'];
    $itemsByAddress = array();
    $billingInfo = array();
    $session = Mage::getSingleton('adminhtml/session_quote');
        $quote = $session->getQuote();

        //first we are reorganizing the data provided by javascript into an array based on address ID's
        foreach ($items as $key => $item) {
            $addID = $item['addressID'];
            $itemID = $key;
            $qty = $item['qty'];
            $itemsByAddress[$addID][] = array('itemID' => $itemID, 'qty' => $qty);
        }
        // print_r($itemsByAddress);
        //now that we have an array of items, seperated by which addressid they goto, we can create the order for each address

        foreach ($itemsByAddress as $addressID => $items) {
            $address = Mage::getModel('customer/address')->load($addressID); 
            $quote->addShippingAddress($address);
            echo"<br/>address id is $addressID, items are: <br/>"; 
            foreach ($items as $item) {
                // $address->addItem($_item);
                ///add items to order
                //  echo"id before is $item<br/>";
                $itemID = explode('-_', $item['itemID']);
                $itemID = $itemID[0];
                $qty = $item['qty'];
                echo "itemid is $itemID, qty is " . $qty . " <br/><br/><br/><br/>";
            }
        }
        //print_r(get_class_methods($quote));
        $shippingAddresses = $quote->getAllShippingAddresses();
        try {
            foreach ($shippingAddresses as $address) { 
                $order = $this->_prepareOrder($address);

                $orders[] = $order;
                Mage::dispatchEvent(
                        'checkout_type_multishipping_create_orders_single', array('order' => $order, 'address' => $address)
                );
            }

            foreach ($orders as $order) {
                $order->place();
                $order->save();
                if ($order->getCanSendNewEmailFlag()) {
                    $order->sendNewOrderEmail();
                }
                $orderIds[$order->getId()] = $order->getIncrementId();
            }

            Mage::getSingleton('core/session')->setOrderIds($orderIds);
            Mage::getSingleton('checkout/session')->setLastQuoteId($quote->getId());

            $quote->setIsActive(false)->save();

            Mage::dispatchEvent('checkout_submit_all_after', array('orders' => $orders, 'quote' => $quote));

            return $this;
        } catch (Exception $e) {
            Mage::dispatchEvent('checkout_multishipping_refund_all', array('orders' => $orders));
            throw $e;
        }

1 个答案:

答案 0 :(得分:2)

好的,我解决了这个问题,并成功地编写了一个脚本,将普通的管理订单转换为多地址订单。一直与我混淆的错误之一就是我加载管理员引用的方式:

我正在加载这样的会话

$session = Mage::getSingleton('adminhtml/session_quote');

它应该是

$session = Mage::getModel('adminhtml/session_quote');

现在,只要将每个报价项目分配到一个地址,最终的代码就像:

$address = Mage::getModel('customer/address')->load($addressID);
echo"<br/>address id is $addressID<br/>";

echo"quote address not set, adding shipping address <br/>";
$quoteAddress = Mage::getModel('sales/quote_address')->importCustomerAddress($address);
$quote->addShippingAddress($quoteAddress);
$quoteAddressItem = $quoteAddress->getItemByQuoteItemId($quoteItem->getId());
if ($quoteAddressItem) {
  echo"increasing item quantitiy for quote address<br/>";
  // $quoteAddressItem->setQty((int) ($quoteAddressItem->getQty() + $qty));
} else {
echo"setting customer id for quote item<br/>";
$quoteItem->setCustomerAddressId($quoteAddress->getCustomerAddressId());
echo"adding item to quote address<br/>";
$quoteAddress->addItem($quoteItem)->setQuote($quote);
echo"adding item to quote collection<br/>";


if (!$quoteAddress->getItemsCollection()->getItemById($quoteItem->getId())) {
    $quoteAddress->getItemsCollection()->addItem($quoteItem)->save();
}
$quoteAddress->setCollectShippingRates(TRUE);
$quoteAddress->collectShippingRates();
$quoteAddress->save();
$quoteItem->save();
$quote->save();

}