I have got a script for creating an order, with a guest customer (see my code below). This script works fine but it not decrease stock of products sets in the created order.

Any explication or solution ?

Thanks you

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;
}
有帮助吗?

解决方案

You should try adding

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

after

$order->addItem ( $orderItem );
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top