产品过滤器显示错误的结果admin product grid magento

时间:2016-10-13 05:19:34

标签: magento magento-1.9

我使用的是Magento Enterprise Edition 1.14.3  我添加了一个自定义列simple_count来计算可配置产品中的产品总数。并在管理员中创建了自定义属性simple_count。如果我使用'type' => 'text'过滤器工作正常,但当我使用'type' => 'number'时,过滤器显示不正确的结果。 请参阅以下代码:

$this->addColumn('simple_count', array(
          'header'    => Mage::helper('catalog')->__('Simple Count'),
          'align'     =>'right',
          'width' => '100px',
          'type'  => 'number',           
          'index'     => 'simple_count',
          'renderer' => 'Wcl_Employee_Block_Adminhtml_Employee_Renderer_Simplecount',         
      ));

对于渲染部分:

class Wcl_Employee_Block_Adminhtml_Employee_Renderer_Simplecount extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract{

    public function render(Varien_Object $row)
    {
            $entityId = $row['entity_id'];
                $product = Mage::getModel('catalog/product')->load($entityId);
                $productType = $product->getTypeID(); 
                $sum = 0;
                if($productType == 'configurable'):     
            $childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);
                        //echo "Simple Count:".count($childProducts);
                        //$sum =0;
                        foreach($childProducts as $itempro):
                        $productdata = Mage::getModel('catalog/product')->load($itempro->getId());
                        $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productdata)->getQty();
                        $sum = $sum + $stock;
                        endforeach;
                        $product->setSimpleCount($sum); $product->save();
                        if($sum == 0):
                          return '0';
                        else:
                return $sum;
                        endif;
                else:
                        $product->setSimpleCount($sum); $product->save();   
                        return '0';     
                endif;   
   }     
}

如果有人知道这一点,请把我弄出来。 感谢

1 个答案:

答案 0 :(得分:0)

    you can use filter_condition_callback like this 
    $this->addColumn('simple_count', array(
              'header'    => Mage::helper('catalog')->__('Simple Count'),
              'align'     =>'right',
              'width' => '100px',
              'type'  => 'number',           
              'index'     => 'simple_count',
              'renderer' =>      'Wcl_Employee_Block_Adminhtml_Employee_Renderer_Simplecount', 
              'filter_condition_callback' => array($this, '_filterValidityCondition'),            
          ));

    protected function _filterValidityCondition($collection, $column) {
            if (!$value = $column->getFilter()->getValue()) {
                return;
            }
            if (isset($value['from']) && $value['from']) {

$this->getCollection()->addFieldToFilter($column->getIndex(), array('gteq' => $from));
            }
            if (isset($value['to']) && $value['to']) {

$this->getCollection()->addFieldToFilter($column->getIndex(), array('leeq' => $to));
            }
        }