Add new attribute to Magento quote / order


I am working on a user module for loyalty points. During the check, the client has the opportunity to redeem his points.
In the module setup, I created redeem_points eav_attribute (it is present in the eav_attribute table), and I added the attribute to the quote, well, sort of ...
Here is how I did it:

  • in mysql4-install-0.1.0.php I call $ installer-> installEntities ();
  • in Namespace_Module_Model_Resource_Eav_Mysql4_Setup (which extends Mage_Eav_Model_Entity_Setup) there is only 1 public function getDefaultEntities () method that returns an array containing (among other things):

    'quote' => array( 'entity_model' => 'sales/quote', 'table' => 'sales/quote', 'attributes' => array( 'redeemed_points' => array('type' => 'static') ), ), 
  • again in mysql4-install-0.1.0.php I create a column in the sales_flat_quote table, like this

      //add redeemed_points to quote table $installer->getConnection()->addColumn($installer->getTable('sales/quote'), 'redeemed_points', 'bigint(20)'); $installer->addAttribute('quote', 'redeemed_points', array('type'=>'static')); 

In control, when I redeem points, the savePoints ($ data) method from my class, which extends Mage_Checkout_Model_Type_Onepage, is called:

 public function savePoints($data) { //save data if ($data == 1) { $redeemedPoints = Mage::helper('points')->getRedeemablePoints(); $this->getQuote()->setRedeemedPoints($redeemedPoints['points']); } else { $this->getQuote()->setRedeemedPoints(0); } $this->getQuote()->collectTotals()->save(); $this->getCheckout() ->setStepData('points', 'complete', true); if ($this->getQuote()->isVirtual()) { $this->getCheckout()->setStepData('payment', 'allow', true); } else { $this->getCheckout()->setStepData('shipping_method', 'allow', true); } Mage::helper('firephp')->debug($this->getQuote()->debug()); return array(); } 

You will notice that I am debugging the quote object in firephp: at this point (at this stage of the check, just storing it in the quote), I can see the redeemed_points attribute with the correct value.
My problem is that in the next step this attribute will disappear from the quote object :(
Therefore, I understand that I was unable to include the redeemed_points attribute in the quote object, but I really don't know what I am missing ...
Does anyone beat someone?

+4
source share
3 answers

Try manually removing var / cache / *. The DB schema is cached there, and sometimes Magento will not collect your new table columns, even if they are there. Furthermore, this does not seem to be cleared using the clear cache function in the backend, but only manually deleting everything in the directory.

+7
source

The following extended example includes flushing software caches:

 $installer = Mage::getResourceModel('sales/setup', 'sales_setup'); $installer->startSetup(); $installer->addAttribute('order', 'new_attribute', array('type'=>'boolean','default'=>0)); $installer->addAttribute('quote', 'new_attribute', array('type'=>'boolean','default'=>0)); // Refresh DB table describing cache programmatically if (method_exists($this->_conn, 'resetDdlCache')) { $this->_conn->resetDdlCache('sales_flat_order'); $this->_conn->resetDdlCache('sales_flat_quote'); } $installer->endSetup(); 
+1
source

The easiest way: try this example:

 $installer = Mage::getResourceModel('sales/setup', 'sales_setup'); $installer->startSetup(); $installer->addAttribute('order', 'new_attribute', array('type'=>'boolean','default'=>0)); $installer->addAttribute('quote', 'new_attribute', array('type'=>'boolean','default'=>0)); $installer->endSetup(); 
0
source

All Articles