如何在prestashop中的renderList中插入预设过滤器?

时间:2017-04-19 09:13:40

标签: php filter prestashop action-filter

由于我能够在@TheDrot的帮助下选择上一个问题,现在有了这个,当我重新初始化过滤它没有返回标准过滤器我需要一些帮助才能知道我必须改变什么在代码中让它工作。 result in image 我发送代码和结果的图像没有成功。 谢谢你的帮助。 谢谢。 result 2

public function processResetFilters($list_id = null)    
    {
        if ($list_id === null) {
            $list_id = isset($this->list_id) ? $this->list_id : $this->table;
        }

        $prefix = '';//var_dump($list_id, $prefix, $this->context->cookie);
        $filters = $this->context->cookie->getFamily($prefix.$list_id.'Filter_');
        foreach ($filters as $cookie_key => $filter) {
            if (strncmp($cookie_key, $prefix.$list_id.'Filter_', 7 + Tools::strlen($prefix.$list_id)) == 0) {
                $key = substr($cookie_key, 7 + Tools::strlen($prefix.$list_id));
                if (is_array($this->fields_list) && array_key_exists($key, $this->fields_list)) {
                    $this->context->cookie->$cookie_key = null;
                }
                unset($this->context->cookie->$cookie_key);
            }
        }

        if (isset($this->context->cookie->{'submitFilter'.$list_id})) {
            unset($this->context->cookie->{'submitFilter'.$list_id});
        }
        if (isset($this->context->cookie->{$prefix.$list_id.'Orderby'})) {
            unset($this->context->cookie->{$prefix.$list_id.'Orderby'});
        }
        if (isset($this->context->cookie->{$prefix.$list_id.'Orderway'})) {
            unset($this->context->cookie->{$prefix.$list_id.'Orderway'});
        }

        $_POST = array();
        $this->_filter = false;
        unset($this->_filterHaving);
        unset($this->_having);

        //your code
       $table= 'contabilidade_faturacao';
    $startTime = date('Y-m-d', mktime(0, 0, 0, date('m')-1  , 1 , date('Y'))); 
    $endTime = date('Y-m-d', mktime(23, 59, 59, date('m'), date('d')-date('j'), date('Y'))); 
    $table = 'contabilidade_faturacao';
    $date_filter_key = $table.'Filter_cf!data';
    $this->context->cookie->{$date_filter_key} = serialize(array(
        $startTime,
        $endTime
    ));
    $this->context->cookie->write();
    }

3 个答案:

答案 0 :(得分:0)

在呈现列表之前,检查是否未设置日期过滤器,然后将默认日期过滤器写入cookie。

$date_filter_key = $table.'Filter_cf!data';
if (!Tools::getIsset('local_'.$date_filter_key) && !isset($this->context->cookie->{$date_filter_key}) {
    $this->context->cookie->{$date_filter_key} = serialize(array(
        '2017-02-01',
        '2017-02-28'
    ));
    $this->context->cookie->write();
}

重置过滤器时,必须使用默认过滤器重写cookie。

public function processResetFilters($list_id = null)
{
    ...
    // your code
    $startTime = date('Y-m-d', mktime(0, 0, 0, date('m')-1  , 1 , date('Y'))); 
    $endTime = date('Y-m-d', mktime(23, 59, 59, date('m'), date('d')-date('j'), date('Y'))); 
    $table = 'contabilidade_faturacao';
    $date_filter_key = $this->table.'Filter_cf!data';
    $this->context->cookie->{$date_filter_key} = serialize(array(
        $startTime,
        $endTime
    ));
    $this->context->cookie->write();
}

答案 1 :(得分:0)

这可以通过覆盖HelperList.php核心文件(Path:/classes/helper/HelperList.php)来实现

有一个名为displayListHeader()的函数,它获取列表中的所有过滤器并将它们传递给TPL文件(/admin/themes/default/template/helpers/list/list_header.tpl),该文件呈现所有过滤器

您可以覆盖HelperList.php类中的displayListHeader()函数,以更改HelperList中过滤器的行为。

答案 2 :(得分:0)

I apologize @TheDrot, but nothing is happening, I send my code to vers and can tell me where I am missing.
I send the image of my result.[result code in image][1]
I'm sorry I'm not figuring out how I can make it work but I'm still new to prestashop.
Thank you for your precious help.

public function processResetFilters($list_id = null)    
    {
        if ($list_id === null) {
            $list_id = isset($this->list_id) ? $this->list_id : $this->table;
        }

        $prefix = '';//var_dump($list_id, $prefix, $this->context->cookie);
        $filters = $this->context->cookie->getFamily($prefix.$list_id.'Filter_');
        foreach ($filters as $cookie_key => $filter) {
            if (strncmp($cookie_key, $prefix.$list_id.'Filter_', 7 + Tools::strlen($prefix.$list_id)) == 0) {
                $key = substr($cookie_key, 7 + Tools::strlen($prefix.$list_id));
                if (is_array($this->fields_list) && array_key_exists($key, $this->fields_list)) {
                    $this->context->cookie->$cookie_key = null;
                }
                unset($this->context->cookie->$cookie_key);
            }
        }

        if (isset($this->context->cookie->{'submitFilter'.$list_id})) {
            unset($this->context->cookie->{'submitFilter'.$list_id});
        }
        if (isset($this->context->cookie->{$prefix.$list_id.'Orderby'})) {
            unset($this->context->cookie->{$prefix.$list_id.'Orderby'});
        }
        if (isset($this->context->cookie->{$prefix.$list_id.'Orderway'})) {
            unset($this->context->cookie->{$prefix.$list_id.'Orderway'});
        }

        $_POST = array();
        $this->_filter = false;
        unset($this->_filterHaving);
        unset($this->_having);

        //your code
        $startTime = date('Y-m-d', mktime(0, 0, 0, date('m')-1  , 1 , date('Y'))); 
        $endTime = date('Y-m-d', mktime(23, 59, 59, date('m'), date('d')-date('j'), date('Y'))); 
        $table = 'contabilidade_faturacao';
        $date_filter_key = $this->table.'Filter_cf!data';
        if (!Tools::getIsset('local_'.$date_filter_key) && !isset($this->context->cookie->{$date_filter_key})) {
            $this->context->cookie->{$date_filter_key} = serialize(array(
                $startTime,
                $endTime
            ));
            $this->context->cookie->write();
        }
    }


  [1]: https://i.stack.imgur.com/2peoP.png