2

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.

0

You can use $quote->getEntityId() to get the quote_id

2
  • Yes, I have tried $quote->getEntityId() but not success. Mar 30 '17 at 4:18
  • Does the order and quote are saved on the database?
    – manumotate
    Mar 31 '17 at 14:08
0

As per my understanding with your code,you are creating order programatically by adding and loading products,customer hence quote id is not generating.Since quote id is generated when a product is added to cart thats why you are not getting quote id.

0
0
$quote = Mage::getModel('checkout/session')->getQuote();

$quote->getEntityId(); or $quote->getId();

 $quote = Mage::getModel('checkout/session')->getQuote();
                       $grandTotal = 0;
  foreach ($quote->getAllItems() as $item) {
 //get all data here
                      }

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.