Question

I've been doing a fair amount of googling, trial and error but I cannot find a solution to the problem.

  1. The ability to change the fields and order of sales_order_grid; and
  2. The ability to display two custom fields on this grid (filterable).

The former (point 1) has been solved, by extending the Mage_Adminhtml_Block_Widget_Grid in my custom module (I know about observers, yet other installed modules were overriding my changes with their observers).

Regardless, the latter is my current problem, below are the two methods that have failed me so far.

Method 1

<?php
/*  @var $this Mage_Sales_Model_Mysql4_Setup  */
$this->startSetup();
$connection = $this->getConnection();

/**
 * Create the payment method dropdown field, because this field _may_ be
 * used for searching we will create an index for it.
 */
$connection->addColumn(
    $this->getTable('sales/order_grid'),
    'x_payment_method',
    "ENUM('PayPal', 'SagePay') DEFAULT NULL"
);
$connection->addKey($this->getTable('sales/order_grid'), 'x_payment_type', 'x_payment_type');

/**
 * Create the order channel field to identify where the order was originally
 * generated from. Also add an index for this field for additional filtering.
 */
$connection->addColumn(
    $this->getTable('sales/order_grid'),
    'x_sale_channel',
    "ENUM('Amazon', 'Play', 'eBay', 'Website') NOT NULL DEFAULT 'Website'"
);
$connection->addKey($this->getTable('sales/order_grid'), 'x_sale_channel','x_sale_channel');

$this->endSetup();

Method 2

By this point I was tired of reading the same 7 articles that didn't help, so I tried to get ONE field working; I also checked the error logs in Magento and found "$this->getTable()" was errornous, thus I removed it.

<?php
/*  @var $this Mage_Sales_Model_Mysql4_Setup  */
$this->startSetup();
$connection = $this->getConnection();

/**
 * Create the payment method dropdown field, because this field _may_ be
 * used for searching we will create an index for it.
 */
$this->addAttribute('sales_flat_order', 'x_test_option', array(
    'label' => 'X Test Option',
    'type' => 'varchar',
    'input' => 'select',
    'visible' => true,
    'required' => false,
    'position' => 1,
    'visible_on_front'  => false,
    'option' => array('value' => array('web', 'test 1', 'test 2')),
    'default' => array('web'),
));

$this->endSetup();

Which makes one beg the question, what is the difference between a column and an attribute? My initial presumption was that, a column is added to an existing core table while an attribute is added to the EAV_* tables and appropriately related.

Was it helpful?

Solution

Your presumption is correct.
But for the sales_ table they are one and the same.
Initially the sales entities were EAV. They changed into flat tables starting with version 1.4.0.1 (I think). And for backwards compatibility both methods were kept.
For any other flat tabled entities (cms pages, blocks, polls) you cannot use addAttribute, only addColumn, but for sales it works both ways.
If you plan to make an extension that has to be compatible with versions before 1.4 then use addAttribute, otherwise I see no point in it.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top