添加SKU专栏Magento admin sales>订购

时间:2013-03-23 12:34:32

标签: php magento

我设法获得了我想要显示的字段,但是我无法将数据提取到例如SKU数据应来自目录>管理产品。

使用的Magento版本是1.5.1.0

enter image description here enter image description here

基本上我需要从同一个类中的'customer / customer_collection'和'sales / order_grid_collection'中提取数据,这可能吗?

5 个答案:

答案 0 :(得分:5)

要获得订单中的所有Skus,您可以使用frame_callback在单独的函数中查找订单的Skus。

在Sku专栏的_prepareColumns()函数中:

...

$this->addColumn('increment_id', array(
                 'header' => Mage::helper('sales')->__('SKU'),
                 'index' => 'increment_id',
                 'frame_callback' => array($this, 'callback_skus')
));

...

然后在Grid.php

中的某处添加此新功能
public function callback_skus($value, $row, $column, $isExport) {
    $increment_id = $value;
    $_order = Mage::getModel('sales/order')->loadByIncrementId($increment_id);
    $_items = $_order->getAllItems();
    $skus="";
    foreach ($_items as $item) {
            $skus .= $item->getSku()."<br/>";
    }
    return $skus;
}

这将返回订单的所有Skus而没有很多复杂的连接。


编辑 - 答案的第二部分

要启用上述搜索/过滤功能,您需要将_prepareCollection()更新为以下内容:

protected function _prepareCollection()
{

    $collection = Mage::getResourceModel($this->_getCollectionClass())
    ->join(
           'sales/order_item',
           '`sales/order_item`.order_id=`main_table`.entity_id',
               array(
                     'skus'  => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR ",")'),
               )
    );
    $collection->getSelect()->group('entity_id');
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

添加一个名为filter_skus()的新功能:

public function filter_skus($collection, $column) {
   if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }

    $this->getCollection()->getSelect()->where(
        "sku like ?"
    , "%$value%");

    return $this;

}

并更新addColumn sku,如下所示:

...

$this->addColumn('sku', array(
                 'header' => Mage::helper('sales')->__('SKU'),
                 'index' => 'increment_id',
                 'frame_callback' => array($this, 'callback_skus'),
                 'filter_condition_callback' => array($this, 'filter_skus'),
));

...

请注意对filter_condition_callback的新调用,该调用会添加进行搜索功能所需的where条件。

答案 1 :(得分:0)

尝试参考此链接:

http://www.atwix.com/magento/accessing-custom-attribute-at-checkout-or-cart/

它应该允许您将任何产品属性添加到sales / order_item,然后有效地使该属性在集合中可用。

答案 2 :(得分:0)

我已经按照上面“seanbreeden”帖子中提到的完成步骤进行了操作 https://stackoverflow.com/posts/15589455/revisions

但是对于SKU搜索功能,我在此功能中添加了一行

public function filter_skus($collection, $column) {
   if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }
$this->getCollection()->getSelect()->join( array('table_alias'=>'sales_flat_order_item'), 'main_table.entity_id = table_alias.order_id',array('table_alias.sku'));
$this->getCollection()->getSelect()->where("sku like ?", "%$value%");

    return $this;
}

添加此行后,搜索工作正常,

答案 3 :(得分:0)

Step 1:  Create the Renderer directory under /app/code/core/Mage/Adminhtml/Block/Sales/Order

Step 2:
Create a Block File in /app/code/core/Mage/Adminhtml/Block/Sales/Order/Renderer/Red.php (You can use any name)
<?php


class
Mage_Adminhtml_Block_Sales_Order_Renderer_Red extends
Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract

    {
      public function render(Varien_Object $row)

       {

         //$value = $row->getData($this->getColumn()->getIndex());



        $order_id=$row->getData('increment_id');

        $order = Mage::getModel('sales/order')->loadByIncrementID($order_id);

        $items = $order->getAllItems();

        // print_r($items);exit();

            foreach ($items as $itemId => $item)

                {

                if($item->getSku())

                    {

                    $sku[] = $item->getSku();

                    }

                }

                if(count($sku))

                    {

                    $skutext = implode(',',$sku);

                    }

            $conbinetext=$skutext;

            return $conbinetext;

        }

    }



?>
Step 3: Add the Below Code in
/app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php

$this->addColumn('Shopby',


  array(


  'header'=>Mage::helper('catalog')->__('Sku'),

  'index' => 'shopby',

  'filter'=> false,


  'sortable'=>false,

  'renderer'=> 'Mage_Adminhtml_Block_Sales_Order_Renderer_Red',// THIS IS WHAT THIS POST IS ALL ABOUT

  ));

答案 4 :(得分:0)

注意:为了不覆盖magento核心功能,您可以创建自己的模块并在Grid.php中使用以下代码

打开app \ code \ core \ Mage \ Adminhtml \ Block \ Sales \ Order \ Grid.php

替换以下功能

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->getSelect()->joinLeft(
            'sales_flat_order_item',
            '`sales_flat_order_item`.order_id=`main_table`.entity_id',
            array('skus'  => new Zend_Db_Expr('group_concat(`sales_flat_order_item`.sku SEPARATOR ",")'))
    );
    $collection->getSelect()->group('entity_id');

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

现在将以下代码放在_prepareColumns()函数

$this->addColumn('sku', array(
    'header' => Mage::helper('sales')->__('SKU'),
    'index' => 'skus',
    'type' => 'text',
    'width' => '100px',
    'filter'    => false,
));

来源:http://chandreshrana.blogspot.in/2016/12/how-to-add-sku-column-in-sales-order.html