I have create a programmatically order. But when submit order sales_quote_collect_totals_after events fire before save collect totals.
Here is my code :
$storeId = Mage::app()->getStore()->getStoreId();
try {
$customer_id = $this->getRequest()->getParam('customer_id');
$selected_product_details = $this->getRequest()->getParam('selected_product_details');
$firstname = $this->getRequest()->getParam('firstname');
$lastname = $this->getRequest()->getParam('lastname');
$email = $this->getRequest()->getParam('email');
$street = $this->getRequest()->getParam('street');
$mobile = $this->getRequest()->getParam('mobile');
if ($customer_id == '') {
$store = Mage::app()->getStore();
$customer = Mage::getModel("customer/customer");
$customer->setWebsiteId($websiteId)
->setStore($store)
->setFirstname($firstname)
->setLastname($lastname)
->setEmail($email)
->setPassword('123456');
$customer->save();
$customer_id = $customer->getCustomerId();
} else {
$customer = Mage::getModel('customer/customer')->load($customer_id);
}
$product_details = json_decode($selected_product_details, true);
$websiteId = Mage::app()->getWebsite()->getId();
// Start New Sales Order Quote
$quote = Mage::getModel('sales/quote')
->setStoreId($storeId);
// Set Sales Order Quote Currency
$quote->setCurrency($order->AdjustmentAmount->currencyID);
// Assign Customer To Sales Order Quote
$quote->assignCustomer($customer);
// Configure Notification
$quote->setSendCconfirmation(1);
foreach ($product_details as $_products) {
$productId = $_products['productId'];
$qty = $_products['qty'];
$product = Mage::getModel('catalog/product')->load($productId);
$quote->addProduct($product, new Varien_Object(array('qty' => $qty)));
}
// Set Sales Order Billing Address
$billingAddress = $quote->getBillingAddress()->addData(array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => $firstname,
'middlename' => '',
'lastname' => $lastname,
'suffix' => '',
'company' => '',
'street' => $street,
'telephone' => $mobile,
'vat_id' => '',
'save_in_address_book' => 1
));
// Set Sales Order Shipping Address
$shippingAddress = $quote->getShippingAddress()->addData(array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => $firstname,
'middlename' => '',
'lastname' => $lastname,
'suffix' => '',
'company' => '',
'street' => $street,
'telephone' => $mobile,
'vat_id' => '',
'save_in_address_book' => 1
));
if ($shippingPrice == 0) {
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('freeshipping_freeshipping')
->setPaymentMethod('cashondelivery');
} else {
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('flatrate_flatrate')
->setPaymentMethod('cashondelivery');
}
//Fire event sales_quote_collect_totals_after Before ->collectTotals->save();
$quote->getPayment()->importData(array('method' => 'cashondelivery'));
$quote->collectTotals->save();
// Create Order From Quote
$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();
$orderId = $service->getOrder()->getRealOrderId();
// Resource Clean-Up
$quote = $customer = $service = null;
$this->createOrderInvoice($orderId);
$message = $this->__('Ordered Created Successfully');
$success = 1;
//send mail when placing order
$order_mail = new Mage_Sales_Model_Order();
$order_mail->loadByIncrementId($orderId);
$order_mail->sendNewOrderEmail();
$result = array("success" => $success, "message" => $message, "order_id" => $orderId);
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
} catch (Exception $ex) {
$message = $this->__('Something went wrong. Please try again.');
$success = 0;
$result = array("success" => $success, "message" => $message);
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
return false;
}
Here is config.xml :
<modules>
<Assel_StoreOwners>
<version>0.1.0</version>
</Assel_StoreOwners>
</modules>
<global>
<blocks>
<storeowners>
<class>Assel_StoreOwners_Block</class>
</storeowners>
</blocks>
<helpers>
<storeowners>
<class>Assel_StoreOwners_Helper</class>
</storeowners>
</helpers>
<events>
<sales_quote_collect_totals_after>
<observers>
<set_custom_discount>
<type>singleton</type>
<class>Assel_StoreOwners_Model_Observer</class>
<method>setDiscount</method>
</set_custom_discount>
</observers>
</sales_quote_collect_totals_after>
</events>
</global>
I have create a setDiscount function in observer.php But When fire this events i have didn't get quote_id.
Here is observer.php code :
function setDiscount($observer) {
$quote=$observer->getEvent()->getQuote();
$quoteid=$quote->getId();
$customer_id = $quote->getCustomerId();
}
when call this observer I have didn't get quote_id. But I have getting customer_id.
Please anyone help me.