cancel
Showing results for 
Search instead for 
Did you mean: 

My script count grand total twice, what is wrong here

My script count grand total twice, what is wrong here

magento 1.9.3.7

 

Below is my code, I'm trying to follow the example here

but somehow my grand total get calculated twice. Been reading about it and I think it is because magento add 1 grand total for delivery address and 1 for shippppigg address which make no sense, see here

any suggestions on what to change in the code would be great. I'm making this script for importing.

 

My main goal is to import orders from cubecart.

 

function create_quote() {
	$customer = Mage::getModel('customer/customer')->load(410);
	$store_id = $customer->getStoreId();
	$quote = Mage::getModel('sales/quote')->assignCustomer($customer);
	$store = $quote->getStore()->load($store_id);
	$quote->setStore($store);
	try {
		
		//for produktet 168789 er antall = 0 og stock availability er out of stock, disabled og not visible individually
		$productModel = Mage::getModel('catalog/product');
 		$product = $productModel->setStore($store_id)->setStoreId($store_id)->load(168789);		
  	$product->setSkipCheckRequiredOption(true);
		$order_item = $quote->addProduct($product);
		$order_item->setQty(2);
		$order_item->setQuote($quote);
		$order_item->setFreeShipping(true);
		$quote->addItem($order_item);  	
		$product->unsSkipCheckRequiredOption();
    $order_item->checkData();  	
  	
     
    $quoteShippingAddress = new Mage_Sales_Model_Quote_Address();    
    $quoteShippingAddress->setData($shippingAddress);
    $quoteBillingAddress = new Mage_Sales_Model_Quote_Address();
    $quoteBillingAddress->setData($billingAddress);

    $quote->setShippingAddress($quoteShippingAddress);
    $quote->setBillingAddress($quoteBillingAddress);

        $quote->getShippingAddress()->setShippingMethod('amtable');

        //$quote->getShippingAddress()->setCollectShippingRates(true);
        $quote->getShippingAddress()->setCollectShippingRates(false);
        $quote->getShippingAddress()->collectShippingRates();


        $quote->collectTotals(); // calls $address->collectTotals();
        $quote->setIsActive(0);
        $quote->save();    
        
        return $quote->getId() . "\n";
         
	}	 catch (Exception $e) {
		echo $e->getMessage();
	}
	catch (Mage_Core_Exception $e) {
    echo $e->getMessage();
	}  		
}

function create_order($quote) {
$quote = Mage::getModel('sales/quote')->load($quote); // Mage_Sales_Model_Quote
$items = $quote->getAllItems();
$quote->reserveOrderId();

$quote_Payment = $quote->getPayment(); // Mage_Sales_Model_Quote_Payment
$quote_Payment->setMethod("paypal_express");
$quote->setPayment($quote_Payment);

// convert quote to order
$convert_quote = Mage::getSingleton('sales/convert_quote');
$order = $convert_quote->addressToOrder($quote->getShippingAddress());
$order_Payment = $convert_quote->paymentToOrderPayment($quote_Payment);

// convert quote addresses
$order->setBillingAddress($convert_quote->addressToOrderAddress($quote->getBillingAddress()));
$order->setShippingAddress($convert_quote->addressToOrderAddress($quote->getShippingAddress()));

// set payment options
$order->setPayment($convert_quote->paymentToOrderPayment($quote->getPayment()));

// convert quote items
foreach ($items as $item) {
// @var $item Mage_Sales_Model_Quote_Item
$orderItem = $convert_quote->itemToOrderItem($item);


$order->addItem($orderItem);
}

$order->setCanShipPartiallyItem(false);
$order->setData("state", "complete");
$order->setStatus("delivered");
$history = $order->addStatusHistoryComment('importert fra de gamle nettsidene', false);
$history->setIsCustomerNotified(false);
try {
$order->place();
} catch (Exception $e){
Mage::log($e->getMessage());
Mage::log($e->getTraceAsString());
}

$order->save();
//$order->sendNewOrderEmail();
return $order->getId();
}

 

8 REPLIES 8

Re: My script cound grand total twice, what is wrong here

function create_order($quote) {
$quote = Mage::getModel('sales/quote')->load($quote); // Mage_Sales_Model_Quote
$items = $quote->getAllItems();
$quote->reserveOrderId();

$quote_Payment = $quote->getPayment(); // Mage_Sales_Model_Quote_Payment
$quote_Payment->setMethod("paypal_express");
$quote->setPayment($quote_Payment);

// convert quote to order
$convert_quote = Mage::getSingleton('sales/convert_quote');
$order = $convert_quote->addressToOrder($quote->getShippingAddress());
$order_Payment = $convert_quote->paymentToOrderPayment($quote_Payment);

// convert quote addresses
$order->setBillingAddress($convert_quote->addressToOrderAddress($quote->getBillingAddress()));
$order->setShippingAddress($convert_quote->addressToOrderAddress($quote->getShippingAddress()));

// set payment options
$order->setPayment($convert_quote->paymentToOrderPayment($quote->getPayment()));

// convert quote items
foreach ($items as $item) {
// @var $item Mage_Sales_Model_Quote_Item
$orderItem = $convert_quote->itemToOrderItem($item);


$order->addItem($orderItem);
}

$order->setCanShipPartiallyItem(false);
$order->setData("state", "complete");
$order->setStatus("delivered");
$history = $order->addStatusHistoryComment('importert fra de gamle nettsidene', false);
$history->setIsCustomerNotified(false);
try {
$order->place();
} catch (Exception $e){
Mage::log($e->getMessage());
Mage::log($e->getTraceAsString());
}

$order->save();
//$order->sendNewOrderEmail();
return $order->getId();
}

Re: My script cound grand total twice, what is wrong here

Here is where I call the code. I know this code is a bit simple but I try to create it simple and then improve it when I know it works

 

So anyone knows why grand total get counted twice?

 

	$quote = create_quote();
	echo "quote_id = " . $quote . "<br>";
	
	$order = create_order($quote);
	
	echo "order_id = " . $order . "<br>";

Re: My script cound grand total twice, what is wrong here

Hi @Loginname,

You are using following file in create_quote function:

$order_item->setQty(2);

In this line you are setting quantity two for the item, is it correct?

If, incorrect then set it to 1.

Re: My script cound grand total twice, what is wrong here

No that won't help. I set it to 2 as a test.... Ok, if I set it to 1 and product price is 49,-... then grand total become 98.

 

I set it to 2 so I could test my theory, when qty = 2, grand total become 196,- (49*2*2).. So there is somehting else wrong here

Re: My script cound grand total twice, what is wrong here

@Loginname,

Try once by commenting following line:

// $quote->collectTotals();

I hope it will work for you.

Re: My script cound grand total twice, what is wrong here

If I remove that line, it mean that grand total and o\the other numbers are 0. see picture blow where I have just tested it

issue1.jpg

Re: My script cound grand total twice, what is wrong here

Please try to change code accordingly.

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');
$QuoteId = $Quote->getId();


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

For more info you can refer following line:

https://magento.stackexchange.com/questions/223220/creating-order-programatically-showing-wrong-gran...

Re: My script cound grand total twice, what is wrong here

Hi @Loginname 

Did it work for you?