0

I've successfully created an order programmatically using custom prices for items. When I view the order, the item is correct, the subtotal is correct, but the Grand Total is double.

Here is my code...

    global $StoreId;

    $CustomerId = $_POST['CustomerId'];

    $Quote = Mage::getModel('sales/quote')->setStoreId($StoreId);

    $Customer = Mage::getModel('customer/customer')->load($CustomerId); // insert customer ID

    // Assign Customer To Sales Order Quote
    $Quote->assignCustomer($Customer);

    $address =  $Customer->getDefaultBillingAddress();

    $Quote->getBillingAddress()->importCustomerAddress($address);
    $Quote->getShippingAddress()->importCustomerAddress($address);

    $Quote->setSendCconfirmation(0);

    // Loop to add order. $Item contains the Sku, Qty, Price
    AddItemToQuote($Item, $_product, $Quote);

    try 
    {
        $Quote->collectTotals()->save();
        $Quote->getShippingAddress()->collectTotals();
        // Collect Rates and Set Shipping & Payment Method
        $Quote->getShippingAddress()
            ->setCollectShippingRates(true)
            ->collectShippingRates()
            ->setShippingMethod('freeshipping_freeshipping');
        $Quote->getPayment()->setMethod('checkmo');

        // Create Order From Quote
         $service = Mage::getModel('sales/service_quote', $Quote);
         $service->submitAll();
         $increment_id = $service->getOrder()->getRealOrderId();
    }
    catch (Exception $e) 
    {
        echo '<blockquote>Caught exception: ',  $e->getMessage(), "</blockquote>";
    }


function AddItemToQuote($Item, $_product, $Quote)
{
    global $StoreId;

    try 
    {
        $Quote->addProduct($_product, new Varien_Object(array('qty'   => $Item["Qty"])));
        $Quote->save();
        $QuoteItem = $Quote->getItemByProduct($_product);
        $QuoteItem->setOriginalCustomPrice($Item["Price"]);
        $QuoteItem->setCustomPrice($Item["Price"]);
        $QuoteItem->setIsSuperMode(true);
        $QuoteItem->save();
    }
    catch (Exception $e) 
    {
        echo 'Caught exception: ',  $e->getMessage(), "<br>";
        print "Product ID: " . $_product->getId() . "<br>\n";
        print "Product Sku: " . $_product->getSku() . " - " . $_product->getName() . "<hr>\n";
    }
}
0

Haven't figured out enough of whats going on behind the scenes to know why, but I did solve the problem by commenting out the following line...

    // $Quote->getShippingAddress()->collectTotals();

.

try 
{
    set_time_limit(300);

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

    // $Quote->getShippingAddress()->collectTotals();

    // Collect Rates and Set Shipping & Payment Method
    $Quote->getShippingAddress()
        ->setCollectShippingRates(true)
        ->collectShippingRates()
        ->setShippingMethod('freeshipping_freeshipping');
    $Quote->getPayment()->setMethod('checkmo');
    $QuoteId = $Quote->getId();


    // Create Order From Quote
    $service = Mage::getModel('sales/service_quote', $Quote);
    $service->submitAll();
    $increment_id = $service->getOrder()->getRealOrderId();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.