Как добавить адрес электронной почты в сетку администратора заказов на продажу?


Пытаюсь добавить адрес электронной почты в сетку продаж, но после добавления не удается выполнить поиск по номеру заказа. Ошибка броска:

SQLSTATE[23000]: Нарушение ограничения целостности: 1052 Столбец "increment_id" в предложении where неоднозначен, запрос был: ВЫБЕРИТЕ КОЛИЧЕСТВО(*) ИЗ sales_flat_order_grid КАК main_table ВНУТРЕННЕЕ СОЕДИНЕНИЕ customer_entity В main_table.customer_id = customer_entity.entity_id, ГДЕ (increment_id КАК '%0013%')

Мой код:

protected function _prepareCollection()
{
    $customer_entity = Mage::getSingleton('core/resource')->getTableName('customer_entity');
    $collection = Mage::getResourceModel($this->_getCollectionClass());

    $collection->getSelect()
           ->join(
           $customer_entity,
           'main_table.customer_id = '.$customer_entity.'.entity_id', array('customer_email' => 'email'));
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

И определение поля:

        $this->addColumn('customer_email', array(
        'header'    => Mage::helper('Sales')->__('Email'),
        'width'     => '100px',
        'index'     => 'customer_email',
        'type'        => 'text',

    ));

Что нужно сделать, чтобы это сработало? Для отображения электронной почты я могу просто добавить столбец, но затем, когда я пытаюсь выполнить поиск по электронной почте, magento выдает ошибку:

SQLSTATE[42S22]: Столбец не найден: 1054 Неизвестный столбец "customer_email" в предложении "где", запрос был: ВЫБЕРИТЕ КОЛИЧЕСТВО(*) ИЗ sales_flat_order_grid КАК main_table ГДЕ (customer_email КАК "%custom%")

Author: Rob D. A., 2017-06-28

2 answers

Прежде всего, вам нужно получить электронное письмо клиента, выполнив соединение с таблицей sales_flat_order. Ваша функция _prepareCollection() будет:

protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel($this->_getCollectionClass());
        $collection->getSelect()->joinLeft(
           array('ordersTable'=>'sales_flat_order'),
           'ordersTable.entity_id = main_table.entity_id',
           array('ordersTable.customer_email')
         );
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

В _prepareColumns() вам нужно будет добавить:

$this->addColumn('customer_email', array(
            'header' => $this->helper('sales')->__('Email'),
            'index' => 'customer_email',
            'filter_index' => 'ordersTable.customer_email',
        ));

Теперь, чтобы устранить проблему с поиском по номеру заказа, вам нужно добавить 'filter_index' в столбец "номер заказа". Проблема возникает из-за наличия повторяющегося столбца increment_id. Вот код:

$this->addColumn('real_order_id', array(
            'header'=> Mage::helper('sales')->__('Order #'),
            'width' => '80px',
            'type'  => 'text',
            'index' => 'increment_id',
            'filter_index'=>'main_table.increment_id'
        ));
 8
Author: Alex Constantinescu, 2017-06-28 21:33:57

Чтобы избежать "двусмысленности"

filter_index' => 'main_table.grand_total

Требуется. Проблема решена.

 2
Author: Rob D. A., 2017-06-29 05:39:10