在Magento中获取客户在订单和发票网格中的自定义属性

时间:2016-11-23 01:30:33

标签: magento attributes magento-1.9

我已经在互联网上搜索了一个答案,包括许多SO问题,但似乎没有一个问题能为我提供我所需要的东西。

我有一个客户的自定义属性(customer_number)。我需要能够在管理区域中的订单发票网格中显示此内容。

我已经通过将Mage_Adminhtml_Block_Orders_Grid复制到本地池并使用以下代码获得了客户电子邮件

$collection->getSelect()
    ->join(
        'sales_flat_order_address',
        'main_table.entity_id = sales_flat_order_address.parent_id',
        array('email')
    );

这显然只适用于订单网格。

有人可以帮助我在这些表格中获取customer_number属性吗?

使用CE 1.9。

更新

我在_prepareCollection()中添加了以下代码的列:

$gen_number_attr = Mage::getSingleton('customer/customer')->getResource()->getAttribute('gen_number');
$collection->getSelect()->joinLeft(
    array(
       'table_customer_number' => $gen_number_attr->getBackend()->getTable()),
       'main_table.customer_id = table_customer_number.entity_id AND table_customer_number.attribute_id = '.$gen_number_attr->getId(). ' AND table_customer_number.entity_type_id = '.Mage::getSingleton('customer/customer')->getResource()->getTypeId(),
        array(
           'customer_number' =>'table_customer_number.value'
        )
    );

现在的问题是,当我尝试过滤时,我收到以下错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'customer_number' in 'where clause', query was: SELECT COUNT(*) FROM `sales_flat_order_grid` AS `main_table`
INNER JOIN `sales_flat_order_address` ON main_table.entity_id = sales_flat_order_address.parent_id WHERE (`customer_number` LIKE '%123%')

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

订单: - 复制默认的'app / code / core / Mage / Adminhtml / Block / Sales / Order / Grid.phtml'并将其放在'app / code / local / Mage / Adminhtml / Block / Sales / Order中/Grid.phtml”。现在从新的本地目录打开Grid.php文件,仔细查看以下代码块:

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
            /* adding term and condition section */
                 $customerterm_conditionAttr = Mage::getSingleton('customer/customer')->getResource()->getAttribute('term_condition');
                 $collection->getSelect()
                            ->joinLeft(
                                array('cusTerm_conditionTb' => $customerterm_conditionAttr->getBackend()->getTable()),
                                'main_table.customer_id = cusTerm_conditionTb.entity_id AND cusTerm_conditionTb.attribute_id = '.$customerterm_conditionAttr->getId(). ' AND cusTerm_conditionTb.entity_type_id = '.Mage::getSingleton('customer/customer')->getResource()->getTypeId(),
                                array('term_condition' =>'cusTerm_conditionTb.value')
                            ); 
            /* end adding term and condition section */  
    $this->setCollection($collection);
    return parent::_prepareCollection();
}
protected function _prepareColumns()
    {
      /* adding term and condition tab */
        $this->addColumn('term_condition', array(
            'header' => Mage::helper('sales')->__('Term and condition'),
            'index' => 'term_condition',
        ));
        /* adding term and condition tab */
    }

发票: - 复制默认的'app / code / core / Mage / Adminhtml / Block / Sales / Invoice / Grid.phtml'并将其放在'app / code / local / Mage / Adminhtml / Block / Sales / Invoice中/Grid.phtml”。现在从新的本地目录打开Grid.php文件,仔细查看以下代码块:

protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel($this->_getCollectionClass());
          /* adding term and condition section */
            $customerterm_conditionAttr = Mage::getSingleton('customer/customer')->getResource()->getAttribute('term_condition');
            $collection->getSelect()
                            ->joinLeft(
                                array('sales_flat_order'),
                                'main_table.order_id = sales_flat_order.entity_id',
                                array('customer_id' =>'customer_id')
                            );
            $collection->getSelect()
                            ->joinLeft(
                                array('cusTerm_conditionTb' => $customerterm_conditionAttr->getBackend()->getTable()),
                                'sales_flat_order.customer_id = cusTerm_conditionTb.entity_id AND cusTerm_conditionTb.attribute_id = '.$customerterm_conditionAttr->getId(). ' AND cusTerm_conditionTb.entity_type_id = '.Mage::getSingleton('customer/customer')->getResource()->getTypeId(),
                                array('term_condition' =>'cusTerm_conditionTb.value')
                            );                    
            /* end adding term and condition section */ 
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }
protected function _prepareColumns()
    {
       /* adding term and condition tab */
       $this->addColumn('term_condition', array(
            'header' => Mage::helper('sales')->__('Term and condition'),
            'index' => 'term_condition',
        ));
        /* adding term and condition tab */
    }