0

I have added one Custom attribute custom_attribute to quote & order table I am sending a response to my controller I want to save the response in my custom_attribute to sales_flat_quote & sales_flat_order table can someone please help me how to achieve this

  $response = $this->getRequest()->getParam('data'); // i am getting response here in my controller 

      $quoteModelInstance = Mage::getSingleton('checkout/session')->getQuote();
        $customerModelInstance = Mage::getSingleton('customer/session');

        $order = Mage::getModel('sales/order');
        $order->setQuote($quoteModelInstance);
        $order->setCustomAttribute($response);
        try {
            $order->save();

        } catch (Exception $e) {

            Mage::logException($e);
        }
8
  • can you please add your code for addition clarification?
    – Vino
    Jul 20 '17 at 5:30
  • @vino Updated s Jul 20 '17 at 5:34
  • you missed $ for response variable.
    – Vino
    Jul 20 '17 at 5:37
  • Did you try $order->setData( 'custom_attribute',$response );
    – Vino
    Jul 20 '17 at 5:42
  • Please confirm the table names sales_flat_quote (or) sales_flat_quote_item ?
    – Nagaraju K
    Jul 20 '17 at 6:02
2

I found another very easy solution for this please follow the steps below:

Step 1: in your controller action please save the custom attribute value as shown below

Note:- You should create a column for saving the value in sales_flat_quote table Ex: custom_attribute is the name of the column i have created

public function customAction(){

  $productPayDetails=$this->getRequest()->getPost();//get post values

  $myValue = 2; 

  Mage::getSingleton("checkout/cart")->getQuote()->setCustomAttribute($myValue)->save();

   (or)

  Mage::getSingleton("checkout/session")->getQuote()->setCustomAttribute($myValue)->save();

}

Step 2: Create a column in the name of custom_attribute in sales_flat_order table

Step 3: now keep the below code in between global tag from config.xml from your custom module

<global> 
<fieldsets>
        <sales_convert_quote>
          <custom_attribute>
            <to_order>*</to_order>
           </custom_attribute>
        </sales_convert_quote>
        <sales_convert_order>
            <custom_attribute>
                <to_quote>*</to_quote>
            </custom_attribute>            
        </sales_convert_order>
    </fieldsets>
  </global>

clear cache and session and create an order and check the database.

1

Here I am showing you how to save the values in the sales_flat_order table using checkout_submit_all_after event and observer concept.

Step-1: From your custom controller set the value into session as shown below

/app/code/local/Namespace/ModuleName/controllers/IndexController.php

public function customAction(){

  $productPayDetails=$this->getRequest()->getPost();//get post values
  //write your code here
  .
  .


    $myValue = 2;
    Mage::getSingleton("checkout/cart")->getQuote()->setCustomAttribute($myValue)->save();

    (or)    

  Mage::getSingleton("checkout/session")->getQuote()->setCustomAttribute($myValue)->save();
  .
  .
  $testValue = 10; //Ex: Your attribute value
  Mage::getSingleton('core/session')->setCustomerPaymentTotal($testValue);
  Mage::getSingleton('core/session')->setCustomerPendingTotal($testValue);
}

Screen shot: http://prntscr.com/fxz7eu

Step 2: Now Configure an event in config.xml from your custom module

/app/code/local/Namespace/ModuleName/et/config.xml

  <frontend>
      <events>
         <checkout_submit_all_after>
                <observers>
                    <checkout_submit_all_after>
                        <class>intermediatepage/observer</class>
                        <method>checkout_submit_all_after</method>
                    </checkout_submit_all_after>
                </observers>
         </checkout_submit_all_after>
      </events>
</frontend>

Note: Here i am taking an example with intermediatepage class name

Step 3: Now Create a column in the sales_flat_order for example as shown in the screenshot http://prntscr.com/fxxqgt

Step 4: create an observer file

/app/code/local/Namespace/ModuleName/Model/Observer.php

<?php

class Namespace_ModuleName_Model_Observer {

    public function checkout_submit_all_after(Varien_Event_Observer $observer)
     {
    $order = $observer->getEvent()->getOrder();
    $pendingtotal=Mage::getSingleton('core/session')->getCustomerPendingTotal();
    $paymenttotal=Mage::getSingleton('core/session')->getCustomerPaymentTotal();
    $order->setOrderPendingPayment($pendingtotal);
    $order->setOrderPaymentNow($paymenttotal);
    $order->save();

 //unset the session values
 Mage::getSingleton('core/session')->getCustomerPendingTotal('');
 Mage::getSingleton('core/session')->getCustomerPendingTotal('');

 }

}

Step 5: after done the changes in xml please remove cache and session and please try this in default magento from your custom module in locally.

3
  • where you are getting the current quote Order ? Jul 20 '17 at 7:17
  • Actually i have explained u point no 2 , this is for saving the custom value in the order table just try this and get back with u r doubts thanks
    – Nagaraju K
    Jul 20 '17 at 7:34
  • hi@ magento12345 please check now i have updated the code now please check now u can save the data into sales_flat_quote & sales_flat_order tables
    – Nagaraju K
    Jul 20 '17 at 8:49

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.