Magento. Add order comment attribute to order

2019-09-04 04:29发布

问题:

I want to add customer comments to my order entity. Currently my setup script looks like:

    <?php

$installer = $this;
/* @var $installer Mage_Sales_Model_Mysql4_Setup */

$installer->startSetup();

    $newFields = array(
        'k_customerordercomment' => array(
            'type'              => 'text',
            'label'                => 'Comments'
        ),
    );

    $entities = array('order');

    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');

    foreach($newFields as $attributeName => $attributeDefs) {
        foreach ($entities as $entity) {
            $setup->addAttribute($entity, $attributeName, array(
                'position'             => 1,
                'type'              => $attributeDefs['type'],
                'label'                => $attributeDefs['label'],
                'global'            => 1,
                'visible'           => 1,
                'required'          => 0,
                'user_defined'      => 1,
                'searchable'        => 0,
                'filterable'        => 0,
                'comparable'        => 0,
                'visible_on_front'  => 1,
                'visible_in_advanced_search' => 0,
                'unique'            => 0,
                'is_configurable'   => 0,
                'position'          => 1,
            ));                
        }
    }



        $installer->getConnection()->addColumn(
        $installer->getTable('sales_flat_order'),
        'k_customerordercomment',
        'text NOT NULL DEFAULT ""'
    );

    $c = array (
        'backend_type'    => 'text',     // MySQL-Datatype
        'frontend_input'  => 'textarea', // Type of the HTML form element
        'is_global'       => '1',
        'is_visible'      => '1',
        'is_required'     => '0',
        'is_user_defined' => '0',
        'frontend_label'  => 'Customer Order Comment',
    );
    $setup->addAttribute('order', 'k_customerordercomment', $c);

    $installer->endSetup();

I get entries in eav_attribute and column added in sales_flat_order. The problem is that I can't set values of comments by setdata or setKCustomerordercomment

But if i set value in DB and then load order from database I can see entered value.

What is wrong?

Magento 1.6.

回答1:

Since version 1.4 orders do not use eav srtukture. Use attribute type "static" instead of text and other setup class.

'type'=>'static'

/* @var $installer Mage_Sales_Model_Mysql4_Setup */

$installer = $this;
$installer->startSetup();

$installer->addAttribute('order', 'field_name', array('type'=>'static', 'visible' => true, 'required' => false, 'is_user_defined' => true, 'note' => 'Field comment'));

$installer->endSetup();


回答2:

This is what worked for me, I don't know why and how. But it works.

<?php

$installer = $this;
/* @var $installer Mage_Sales_Model_Mysql4_Setup */

$installer->startSetup();

$newFields = array(
    'customerordercomment' => array(
        'type'              => 'text',
        'label'                => 'Comments'
    ),
);

$entities = array('order');

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

foreach($newFields as $attributeName => $attributeDefs) {
    foreach ($entities as $entity) {
        $setup->addAttribute($entity, $attributeName, array(
            'position'             => 1,
            'type'              => $attributeDefs['type'],
            'label'                => $attributeDefs['label'],
            'global'            => 1,
            'visible'           => 1,
            'required'          => 0,
            'user_defined'      => 1,
            'searchable'        => 0,
            'filterable'        => 0,
            'comparable'        => 0,
            'visible_on_front'  => 1,
            'visible_in_advanced_search' => 0,
            'unique'            => 0,
            'is_configurable'   => 0,
            'position'          => 1,
        ));                
    }
}



    $installer->getConnection()->addColumn(
    $installer->getTable('sales_flat_order'),
    'customerordercomment',
    'text NOT NULL DEFAULT ""'
);

$c = array (
  'backend_type'    => 'text',     // MySQL-Datatype
  'frontend_input'  => 'textarea', // Type of the HTML form element
  'is_global'       => '1',
  'is_visible'      => '1',
  'is_required'     => '0',
  'is_user_defined' => '0',
  'frontend_label'  => 'Customer Order Comment',
);
$setup->addAttribute('order', 'customerordercomment', $c);

$installer->endSetup();


标签: magento