4


I'm working on a custom loyalty points module. During the checkout the customer has the option to redeem his points.
In the module setup I have created a redeem_points eav_attribute (it's present in the eav_attribute table) and I have added the attribute to the quote, well, sort of...
Here is how I've done it:

  • in the 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 method public function getDefaultEntities() that only returns an array which contains (amongst 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 the 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 the checkout, when I redeem points, the method savePoints($data) 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'll notice that I debug the quote object in firephp: at this moment (in this step of the checkout, just afetr saving it into the quote) I can see the redeemed_points attribute, with the correct value.
My problem is that in the next step this attribute is gone from the quote object :(
So I understand I haven't quite managed to include my redeemed_points attribute in the quote object, but i really don't know what I'm missing...
Any thaughts someone?

Joe Mastey
  • 26,809
  • 13
  • 80
  • 104
OSdave
  • 8,538
  • 7
  • 45
  • 60
  • please refer to this http://stackoverflow.com/questions/4315660/alter-table-in-magento-setup-script-without-using-sql/4318501#4318501 – Anton S Nov 30 '10 at 21:42
  • mmm, this link explains how to create/alter table inside the setup script, but that is not my problem here: the tables are created/altered, I can see the tables/fields in my db. My problem is that I can't manage to include redeemed_points into quote object. Well, it's there when I call the savePoints function in my Model_Type_Onepage, but not disappears in the next step. – OSdave Dec 01 '10 at 09:48

3 Answers3

7

Try manually deleting var/cache/*. DB schema's are cached in there and sometimes Magento won't pick up your new table columns even if they are in there. Also this doesn't seem to get cleared by using the clear cache feature in the backend, but only by manually deleting everything in the directory.

kalenjordan
  • 2,446
  • 1
  • 24
  • 38
  • Spend 3 hours trying to figure out why attributes wasn't getting saved into db. I deleted cache many times from backend, but once i deleted var/cache/* manually it started working :) – puntable Sep 19 '17 at 22:26
1

The following extended example includes programmatic cache clear:

$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();
Aleksey Razbakov
  • 624
  • 8
  • 29
0

There is an 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();
Beto Castillo
  • 867
  • 9
  • 16