1

I have a custom column on my sales order grid showing the ordered items sku's. Done by following the guide here. It worked well in previous versions but no longer does work correctly in 1.9.4.3.

When I go to filter the column by any given sky, I get the error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'skus' in 'where clause', query was: SELECT COUNT(DISTINCT main_table.entity_id) FROM sales_flat_order_grid AS main_table INNER JOIN sales_flat_order_item ON sales_flat_order_item.order_id=main_table.entity_id WHERE (skus LIKE '%tst%')

Backtracing this, the query is from here:

    public function getSelectCountSql()
{
    $countSelect = parent::getSelectCountSql();

    if (Mage::app()->getRequest()->getControllerName() == 'sales_order') {
        $countSelect->reset(Zend_Db_Select::GROUP);
        $countSelect->reset(Zend_Db_Select::COLUMNS);
        $countSelect->columns("COUNT(DISTINCT main_table.entity_id)");

        $havingCondition = $countSelect->getPart(Zend_Db_Select::HAVING);
        if (count($havingCondition)) {
            $countSelect->where(
                str_replace("group_concat(`sales_flat_order_item`.sku SEPARATOR ', ')", 'sales_flat_order_item.sku', $havingCondition[0])
            );

        }
    }

    return $countSelect;
}

In order to try rectify this I added 'filter_index' => 'sku', to my addColumnAfter, via a helper function.

This has fixed the above SQL error, but I am now faced with the below issue(s):

Filtering by multiple skus returns no result. i.e,

Filter by x,z, even if order has say sku's x,c,v,z,y, no result returned.

Also, Filtered result shows only the sku I am filtering by. i.e,

Filter by x, will only show x sku in the column even if the order has say items x, y and z.

How would I get it to work so the sku column shows all skus on the order when filtering and multiple sku filter?

My filter callback:

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

    $collection->getSelect()->having(
        "group_concat(`sales_flat_order_item`.sku SEPARATOR ', ') like ?", "%$value%");
    return $collection;
}

The helper function:

 public function getSkusColumnParams()
{
    return array(
        'header' => 'SKUs',
        'index' => 'skus',
        'filter_index' => 'sku',
        'type' => 'text',
        'filter_condition_callback' => array(Mage::getSingleton('Atwix_ExtendedGrid_model_observer'), 'filterSkus'),
    );
}

Config.xml:

<config>
<modules>
    <Atwix_ExtendedGrid>
        <version>1.0.1</version>
    </Atwix_ExtendedGrid>
</modules>
<global>
    <blocks>
        <atwix_extendedgrid>
            <class>Atwix_ExtendedGrid_Block</class>
        </atwix_extendedgrid>
    </blocks>
    <helpers>
        <atwix_extendedgrid>
            <class>Atwix_ExtendedGrid_Helper</class>
        </atwix_extendedgrid>
    </helpers>
    <models>
        <atwix_extendedgrid>
            <class>Atwix_ExtendedGrid_Model</class>
        </atwix_extendedgrid>
        <sales_resource>
            <rewrite>
                <order_grid_collection>Atwix_ExtendedGrid_Model_Resource_Sales_Order_Grid_Collection</order_grid_collection>
            </rewrite>
        </sales_resource>
    </models>
</global>
<adminhtml>
    <events>
        <sales_order_grid_collection_load_before>
            <observers>
                <atwix_exgrid>
                    <model>atwix_extendedgrid/observer</model>
                    <method>salesOrderGridCollectionLoadBefore</method>
                </atwix_exgrid>
            </observers>
        </sales_order_grid_collection_load_before>
    </events>
    <layout>
        <updates>
            <atwix_extendedgrid>
                <file>atwix/extendedgrid.xml</file>
            </atwix_extendedgrid>
        </updates>
    </layout>
</adminhtml>

3
  • Did you solve this we have the same error where filter_condition_callback is not respected in 1 grid ... M1 tries to filter by an EAV column that does not exist .. and hence we see an error
    – snh_nl
    Jan 16, 2020 at 15:23
  • @snh_nl I didn't truly resolve the issue. But, adding 'filter_index' will fix it Jan 17, 2020 at 16:13
  • This will work only for native attributes
    – snh_nl
    Jan 18, 2020 at 9:22

2 Answers 2

0

There was a code breaking change

Read

https://github.com/OpenMage/magento-lts/issues/882

And apply

https://github.com/OpenMage/magento-lts/pull/887

Please mark as the answer if correct

4
0

snh_nl isn't wrong, but it's just as easy to solve this problem without modifying core code.

If you add your callback function to a custom block class that extends Mage_Adminhtml_Block_Widget_Grid, that will satisfy the new condition recently added by Magento in the function _addColumnFilterToCollection($column) in app/code/core/Mage/Adminhtml/Block/Widget/Grid.php

OLD core code:

if ($column->getFilterConditionCallback()) {

NEW core code added in 1.9.4.3:

if ($column->getFilterConditionCallback() && $column->getFilterConditionCallback()[0] instanceof self) {


So in your case, create app/code/local/Atwix/ExtendedGrid/Block/Widget/Grid.php

class Atwix_ExtendedGrid_Block_Widget_Grid extends Mage_Adminhtml_Block_Widget_Grid {
    public function filterSkus($collection, $column)
    {
        if (!$value = $column->getFilter()->getValue()) {
            return $this;
        }

        $collection->getSelect()->having(
            "group_concat(`sales_flat_order_item`.sku SEPARATOR ', ') like ?", "%$value%");
        return $collection;
    }
}

And modify the callback in your helper function:

public function getSkusColumnParams()
{
    return array(
        'header' => 'SKUs',
        'index' => 'skus',
        'filter_index' => 'sku',
        'type' => 'text',
        'filter_condition_callback' => array(Mage::getBlockSingleton('atwix_extendedgrid/widget_grid'), 'filterSkus'),
    );
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.