Question

so I have a renderer for orders and here is the code: the sku renderer is working fine I have problem with search and filtering the skus

$this->addColumn('sku', array(
                'header'=> Mage::helper('sales')->__('SKU'),
                'index' => 'sku',
                // begin: render the sku content
                'renderer' => new Lenmar_Adminhtml_Block_Sales_Order_RendererSkuFk1(),
                // end: render the sku content
                'type' => 'text',
           'filter_condition_callback' => array($this, '_addressFilter'),
            ));

I read in the article which I should use callback but I don't know how to write the function to select the sku because $this here doesn't have the sku also I used this but it is not correct and gives me error of unknown column sku:

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

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


        return $this;
    }

Error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sales_flat_order_item.sku' in 'where clause'

updated prepare collection:

 protected function _prepareCollection()
    {
        $storeCode='bceu';
        $stores = array_keys(Mage::app()->getStores());
        foreach($stores as $id){
          $store = Mage::app()->getStore($id);
          if($store->getCode()==$storeCode) {
            $storeId=$id;
          }
         }
        //$value=3200000001;        
        $from="2014-05-21"; 
        $collection = Mage::getResourceModel('sales/order_collection')

        ->addFieldToFilter('store_id',$storeId)
        //->addAttributeToFilter('increment_id', array('gt' => $value))

        ->addAttributeToFilter('created_at', array("from" => $from, "datetime" => true))
        ->join(array('a' => 'sales/order_address'), 'main_table.entity_id = a.parent_id AND a.address_type != \'billing\'', array(
                'city'       => 'city',
                'country_id' => 'country_id'
            ))
            ->join(array('c' => 'customer/customer_group'), 'main_table.customer_group_id = c.customer_group_id', array(
                'customer_group_code' => 'customer_group_code'
            ))
            ->addExpressionFieldToSelect(
                'fullname',
                'CONCAT({{customer_firstname}}, \' \', {{customer_lastname}})',
                array('customer_firstname' => 'main_table.customer_firstname', 'customer_lastname' => 'main_table.customer_lastname'))
            ->addExpressionFieldToSelect(
                'products',
                '(SELECT GROUP_CONCAT(\' \', x.name)
                    FROM sales_flat_order_item x
                    WHERE {{entity_id}} = x.order_id
                        AND x.product_type != \'configurable\')',
                array('entity_id' => 'main_table.entity_id')
            )

        ;

         // CUSTOM

        $collection->getSelect()->joinLeft(array('sfop'=>'sales_flat_order_payment'), 
    'main_table.entity_id = sfop.parent_id',array('po_number' => 'sfop.po_number'
    ));
Was it helpful?

Solution

Since sales_flat_order_item is not included in the original sql you will get Unknown column 'sales_flat_order_item.sku' error because that table is unknown

Try

$collection = Mage::getResourceModel('sales/order_collection')
....
->join(array('sfoi' => 'sales_flat_order_item'), 'main_table.entity_id = sfoi.order_id, array('sku'))
...
->group('main_table.entity_id')

If the example above work then undo and try

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

    $this->getCollection()->getSelect()
    ->join(array('sfoi' => 'sales_flat_order_item'), 'main_table.entity_id = sfoi.order_id, array('sku'))
    ->where(
        "sales_flat_order_item.sku like ?"
    , "%$value%");


    return $this;
}

Final answer:

protected function _addressFilter($collection, $column)
{
    if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }
    $this->getCollection()->getSelect()
    ->join(array('sfoi' => 'sales_flat_order_item'), 'main_table.entity_id = sfoi.order_id', array('sku'))
    ->where(
        "sfoi.sku like ?"
    , "%$value%")->group('main_table.entity_id');
 return $this;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top