以编程方式创建订单时库存不会减少

时间:2013-03-25 14:55:56

标签: php magento stock

我有一个用来创建订单的脚本,与客户客户(请参阅下面的代码)。此脚本工作正常但不会减少创建顺序中的产品集库存。

任何解释或解决方案?

谢谢你

private $_storeId = '1';
public function create($customerInfos, $orderInfos, $itemsInfos) {
    $quote = Mage::getModel ( 'sales/quote' );

    // adding guest customer
    $quote->setIsMultiShipping ( false );
    $quote->setCheckoutMethod ( 'guest' );
    $quote->setCustomerId ( null );
    $quote->setCustomerEmail ( $customerInfos ['email'] );
    $quote->setCustomerIsGuest ( true );
    $quote->setCustomerGroupId ( Mage_Customer_Model_Group::NOT_LOGGED_IN_ID );
    $quote->setStoreId ( $this->_storeId );

    // adding addresses
    $quote->getBillingAddress ()->addData ( $customerInfos ['address'] );
    $quote->getBillingAddress ()->implodeStreetAddress ();
    $quote->getShippingAddress ()->addData ( $customerInfos ['address'] );
    $quote->getShippingAddress ()->implodeStreetAddress ();

    $shippingCost = 0;
    foreach ( $itemsInfos as $item ) {
        $shippingCost += $item ['shippingAmount'];
    }

    $quote->getShippingAddress ()->setShippingMethod ( $orderInfos ['shippingMethod'] );
    $quote->getShippingAddress ()->collectShippingRates ();
    $quote->getShippingAddress ()->removeAllShippingRates ();

    $rate = $this->createShippingRate($orderInfos ['shippingMethod']) ->setMethodTitle($orderInfos['marketPlace'])->setPrice($shippingCost);    

    $quote->getShippingAddress ()->addShippingRate($rate)
    ->setShippingMethod($orderInfos ['shippingMethod'])
    ->setShippingDescription($orderInfos['marketPlace']);
    $quote->reserveOrderId ();

    // adding products
    $productModel = Mage::getModel ( 'catalog/product' );
    foreach ( $itemsInfos as $item ) {
        $productId = $productModel->getIdBySku ( $item ['sku'] );
        $product = Mage::getModel ( 'catalog/product' )->load ( $productId );
        // changing price of my product
        $unitPrice = $item ['priceAmount'] / $item ['qtyOrdered'];
        $product->setData ( 'price', $unitPrice );

        $buyInfo = array ('qty' => $item ['qtyOrdered'] );
        $quote->addProduct ( $product, new Varien_Object ( $buyInfo ) );
    }

    $quote->collectTotals ();
    $quote->save ();

    //adding payment infos
    $quotePayment = $quote->getPayment ();
    $quotePayment->setMethod ( $orderInfos ['paymentMethod'] );
    $quote->setPayment ( $quotePayment );

    $convertQuote = Mage::getSingleton ( 'sales/convert_quote' );
    $order = $convertQuote->addressToOrder ( $quote->getShippingAddress () );
    $order->setStoreId ( $this->_storeId );
    $orderPayment = $convertQuote->paymentToOrderPayment ( $quotePayment );

    $order->setBillingAddress ( $convertQuote->addressToOrderAddress ( $quote->getBillingAddress () ) );
    $order->setShippingAddress ( $convertQuote->addressToOrderAddress ( $quote->getShippingAddress () ) );
    $order->setPayment ( $convertQuote->paymentToOrderPayment ( $quote->getPayment () ) );

    foreach ( $quote->getAllItems () as $item ) {
        $orderItem = $convertQuote->itemToOrderItem ( $item );
        if ($item->getParentItem ()) {
            $orderItem->setParentItem ( $order->getItemByQuoteItemId ( $item->getParentItem ()->getId () ) );
        }
        $order->addItem ( $orderItem );
    }

    $order->setCanShipPartiallyItem ( false );
    $order->setAtMkOrderName($orderInfos['marketPlace']);
    $order->setAtMkOrderId($orderInfos['marketPlaceOrderId']);

    $totalDue = $order->getTotalDue ();
    $order->place ();
    $order->save ();
    $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save();
    $orderId = $order->getId ();

    return $orderId;
}

1 个答案:

答案 0 :(得分:6)

您应该尝试添加

Mage::getSingleton('cataloginventory/stock')->registerItemSale($orderItem)

之后

$order->addItem ( $orderItem );