Recently after updating to 1.9.4 my custom order creation function began to fail. If I run my OAcreateOrder() function from a test PHP file the order is created as expected:
testOrder.php
require_once $_SERVER['DOCUMENT_ROOT']."/services/oa-functions.php";
require __DIR__ . '/app/Mage.php';
Mage::app();
$order_info = [
"ord_bill_fname" => "Andy",
"ord_bill_lname" => "Boehm",
"ord_bill_address1" => "123 Acme St",
"ord_bill_address2" => "",
"ord_bill_city" => "Pittsburgh",
"ord_bill_postalcode" => "15211",
"ord_bill_phone" =>"3125443535",
"ord_bill_cou_code" => "US",
"ord_bill_sta_code" => "PA",
"ord_ship_fname" => "Andy",
"ord_ship_lname" => "Boehm",
"ord_ship_address1" => "123 Acme St",
"ord_ship_address2" => "",
"ord_ship_city" => "Pittsburgh",
"ord_ship_postalcode" => "15211",
"ord_ship_phone" =>"3125443535",
"ord_ship_cou_code" => "US",
"ord_ship_sta_code" => "PA",
"ord_gift_sku" => "none",
"ord_plan_sku" => "yoga-monthly",
"ord_total" => "1149.95",
"subtotal" => "1149.95"
];
//create a magento user/customer
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
$customer = Mage::getModel("customer/customer");
$customer->setWebsiteId(1);
$customer->loadByEmail("[email protected]");
try{
$customer->save();
OAcreateOrder($customer, $order_info);
echo "Completed Order";
//login($_POST['email'], $_POST['password']);
}
catch (Exception $e) {
Zend_Debug::dump($e->getMessage());
}
However, if I run my OAcreateOrder() function from within a custom controller the order fails to be created. No error is given.
I've tracked down the issue to here app/code/core/Mage/Sales/Model/Service/Quote.php
$transaction->save();
fails to run. No error/exception is thrown and I'm not seeing anything in the logs.
Can anyone point me in the right direction? I can provide the full OAcreateOrder() function but since this works outside of my controller I don't think it is the issue.
Any insight would be greatly appreciated.
EDIT: Here's the controller and OAcreateOrder() function.
public function indexAction()
{
$order_info = [
"ord_bill_fname" => "Andy",
"ord_bill_lname" => "Boehm",
"ord_bill_address1" => "123 Acme St",
"ord_bill_address2" => "",
"ord_bill_city" => "Pittsburgh",
"ord_bill_postalcode" => "15211",
"ord_bill_phone" =>"3125443535",
"ord_bill_cou_code" => "US",
"ord_bill_sta_code" => "PA",
"ord_ship_fname" => "Andy",
"ord_ship_lname" => "Boehm",
"ord_ship_address1" => "123 Acme St",
"ord_ship_address2" => "",
"ord_ship_city" => "Pittsburgh",
"ord_ship_postalcode" => "15211",
"ord_ship_phone" =>"3125443535",
"ord_ship_cou_code" => "US",
"ord_ship_sta_code" => "PA",
"ord_gift_sku" => "none",
"ord_plan_sku" => "yoga-monthly",
"ord_total" => "1149.95",
"subtotal" => "1149.95"
];
//create a magento user/customer
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
$customer = Mage::getModel("customer/customer");
$customer->setWebsiteId(1);
$customer->loadByEmail("[email protected]");
// ->setRecurlyaccount($uniqid);
try{
$customer->save();
OAcreateOrder($customer, $order_info);
echo "Completed Order";
//login($_POST['email'], $_POST['password']);
}
catch (Exception $e) {
Zend_Debug::dump($e->getMessage());
}
}
OAcreateOrder() function:
function OAcreateOrder($customer, $order_info)
{
Mage::log('ORDER CREATION ATTEMPT', null, 'subscription-order.log');
//Shipping / Billing information gather
//$firstName = $customer->getFirstname(); //get customers first name
//$lastName = $customer->getLastname(); //get customers last name
//$customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling(); //get default billing address from session
$ShipfirstName = $order_info['ord_ship_fname']; //get customers first name
$ShiplastName = $order_info['ord_ship_lname']; //get customers last name
$BillfirstName = $order_info['ord_bill_fname']; //get customers first name
$BilllastName = $order_info['ord_bill_lname']; //get customers last name
$Billaddress = 'No address';
$Billstreet = 'No street';
$Billcity = 'No City';
$Billpostcode = $order_info['ord_bill_postalcode'];
$BillphoneNumber = $order_info['ord_bill_phone'];
$BillcountryId = 'No country';
$BillregionId = 'No region';
if ($order_info['ord_ship_postalcode']) {
$Shipstreet = trim($order_info['ord_ship_address1']." ". $order_info['ord_ship_address2']);
$Shipcity = $order_info['ord_ship_city'];
$Shippostcode = $order_info['ord_ship_postalcode'];
$ShipphoneNumber = $order_info['ord_ship_phone'];
$ShipcountryId = $order_info['ord_ship_cou_code'];
$ShipregionId = 'No region';
if($order_info['ord_ship_cou_code'] == "US") {
$ShipregionModel = Mage::getModel('directory/region')->loadByCode($order_info['ord_ship_sta_code'], $order_info['ord_ship_cou_code']);
$ShipregionId = $ShipregionModel->getId();
Mage::log('Region ID:'.$ShipregionId, null, 'subscription-order.log');
}
}else{
$Shipaddress = 'No address';
$Shipstreet = 'No street';
$Shipcity = 'No City';
$Shippostcode = 'No post code';
$ShipphoneNumber = 'No phone';
$ShipcountryId = 'No country';
$ShipregionId = 'No region';
}
//Start a new order quote and assign current customer to it.
Mage::log('store id: ' . Mage::app('default')->getStore('default')->getId(), null, 'subscription-order.log');
$quote = Mage::getModel('sales/quote')->setStoreId(Mage::app('default')->getStore('default')->getId());
$quote->assignCustomer($customer);
Mage::log('Quote Assign Customer: '.$customer->getEmail(), null, 'subscription-order.log');
//Low lets setup a shipping / billing array of current customer's session
$ShipAddressData = array(
'firstname' => $ShipfirstName,
'lastname' => $ShiplastName,
'street' => $Shipstreet,
'city' => $Shipcity,
'postcode'=>$Shippostcode,
'telephone' => $ShipphoneNumber,
'country_id' => $ShipcountryId,
'region_id' => $ShipregionId
);
$BillAddressData = array(
'firstname' => $BillfirstName,
'lastname' => $BilllastName,
//'street' => $Billstreet,
//'city' => $Billcity,
'postcode'=>$Billpostcode,
'telephone' => $BillphoneNumber,
//'country_id' => $BillcountryId,
//'region_id' => $BillregionId
);
//Add address array to both billing AND shipping address objects.
$billingAddress = $quote->getBillingAddress()->addData($BillAddressData);
$shippingAddress = $quote->getShippingAddress()->addData($ShipAddressData);
//Set shipping objects rates to true to then gather any accrued shipping method costs a product main contain
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('freeshipping_freeshipping')->setPaymentMethod('checkmo');
if(isset($order_info['ord_gift_sku']) && $order_info['ord_gift_sku'] != "none") {
Mage::log('add free gift: ' . $order_info['ord_gift_sku'], null, 'subscription-order.log');
//add the free gift
$product = Mage::getModel("catalog/product")->loadByAttribute('sku', $order_info['ord_gift_sku']);
if($product && $product->getId()) {
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($product)->setQty(1);
$quote->addItem($quoteItem);
}
} else if(isset($order_info['ord_plan_sku'])) {
Mage::log('add plan by sku' . $order_info['ord_plan_sku'], null, 'subscription-order.log');
$product = Mage::getModel("catalog/product")->loadByAttribute('sku', $order_info['ord_plan_sku']);
if($product && $product->getId()) {
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($product)->setQty(1);
$quote->addItem($quoteItem);
}
}
//add the subscription plan
// $product2 = Mage::getModel("catalog/product")->loadByAttribute('sku', $order_info['ord_plan_sku']);
// if($product2 && $product2->getId()){
// $quoteItem2 = Mage::getModel('sales/quote_item')
// ->setProduct($product2)
// ->setQty(1)
// ->setPrice($product2->getPrice())
// ->setSubtotal($product2->getPrice())
// ->setRowTotal($product2->getPrice());
// $quote->addItem($quoteItem2);
// }
Mage::log('Quote Add Item', null, 'subscription-order.log');
//Set quote object's payment method to check / money order to allow progromatic entries of orders
//(kind of hard to programmatically guess and enter a customer's credit/debit cart so only money orders are allowed to be entered via api)
if(!empty($order_info['roku_transaction_id'])){
$quote->getPayment()->importData(
array(
'method' => 'custompaymentmethod',
'roku_transaction_id' => $order_info['roku_transaction_id']
)
);
} else {
$quote->getPayment()->importData(
array(
'method' => 'checkmo',
)
);
}
//$quote->setDiscountAmount($product->getPrice() - $order_info['subtotal']);
//Save collected totals to quote object
$quote->collectTotals()->save();
Mage::log("collectTotals", null, 'subscription-order.log');
$totals = $quote->getGrandTotal();
Mage::log("getGrandTotal", null, 'subscription-order.log');
//Feed quote object into sales model
$service = Mage::getModel('sales/service_quote', $quote);
Mage::log("getModel", null, 'subscription-order.log');
//submit all orders to MAGE
$service->submitAll();
Mage::log("service->submitAll", null, 'subscription-order.log');
//empty the cart for the user
$quote->delete();
Mage::log("quote->delete", null, 'subscription-order.log');
//Setup order object and gather newly entered order
$order = $service->getOrder();
Mage::log("service->getOrder", null, 'subscription-order.log');
if($order) {
$order->setSubtotal($order_info['subtotal'])->setBaseSubtotal($order_info['subtotal']);
$order
->setGrandTotal($order_info['subtotal'])
->setBaseGrandTotal($order_info['subtotal']);
//$order->setTotalPaid($product->getPrice());
//setBaseToGlobalRate this so the orders show up in the reports
$order->setBaseToGlobalRate(1);
$order->setSalesFlatOrder(1);
Mage::log('set suborder total:'.$order_info['subtotal'], null, 'subscription-order.log');
$order->setCreatedAt(Varien_Date::now());
// if($order_info["ord_error"]){
// $history = Mage::getModel('sales/order_status_history')
// ->setStatus($order->getStatus())
// ->setComment($order_info["ord_error"])
// ->setEntityName(Mage_Sales_Model_Order::HISTORY_ENTITY_NAME)
// ->setIsCustomerNotified(false)
// ->setCreatedAt($order_info["ord_osb_due_date"]);
// $order->addStatusHistory($history);
// }
if($order_info['ord_gift_sku'] == "none") {
$order->setStatus('complete');
//add the invoice and mark paid so it shows up in reports
//thanks Inchoo: http://inchoo.net/magento/how-to-create-magento-invoice-from-order/
try {
if(!$order->canInvoice()) {
Mage::throwException(Mage::helper('core')->__('Cannot create an invoice.'));
}
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
if (!$invoice->getTotalQty()) {
Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
}
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();
}
catch (Mage_Core_Exception $e) {
}
} else {
$order->setStatus('pending');
}
// $order->save();
$order->getResource()->save($order);
Mage::log('Save Order', null, 'subscription-order.log');
}
return $order;
}