开发者

Add new attribute to Magento quote/order

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开发者_如何学JAVA 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?


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.


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();


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜