在 Prestashop 1.5 中列出具有多个类别的产品 [英] Listing products with multiple categories in Prestashop 1.5

查看:48
本文介绍了在 Prestashop 1.5 中列出具有多个类别的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与 Prestashop 1.5 Web 服务通信以列出产品,它的过滤器对于基本情况来说足够简单,但是对于多个类别呢:

I'm communicating with Prestashop 1.5 Web Services for listing products and its filters are simple enough for basic cases, but what about multiple categories:

  1. 我可以获得属于类别 1 和类别 2 的产品列表吗?
  2. 我可以获得属于类别 1 或类别 2 的产品清单吗?

推荐答案

这里是 classes/webservice/WebserviceRequest 中的 Prestashop 1.5 过滤器管理流程,供您参考:

For your information here is Prestashop 1.5 Filter management process in classes/webservice/WebserviceRequest:

/**
 * get SQL retrieve Filter
 *
 * @param string $sqlId
 * @param string $filterValue
 * @param string $tableAlias = 'main.'
 * @return string
 */
protected function getSQLRetrieveFilter($sqlId, $filterValue, $tableAlias = 'main.')
{
    $ret = '';
    preg_match('/^(.*)\[(.*)\](.*)$/', $filterValue, $matches);
    if (count($matches) > 1)
    {
        if ($matches[1] == '%' || $matches[3] == '%')
            $ret .= ' AND '.bqSQL($tableAlias).'`'.bqSQL($sqlId).'` LIKE "'.pSQL($matches[1].$matches[2].$matches[3])."\"\n";
        elseif ($matches[1] == '' && $matches[3] == '')
        {
            if (strpos($matches[2], '|') > 0)
            {
                $values = explode('|', $matches[2]);
                $ret .= ' AND (';
                $temp = '';
                foreach ($values as $value)
                    $temp .= bqSQL($tableAlias).'`'.bqSQL($sqlId).'` = "'.bqSQL($value).'" OR ';
                $ret .= rtrim($temp, 'OR ').')'."\n";
            }
            elseif (preg_match('/^([\d\.:-\s]+),([\d\.:-\s]+)$/', $matches[2], $matches3))
            {
                unset($matches3[0]);
                if (count($matches3) > 0)
                {
                    sort($matches3);
                    $ret .= ' AND '.$tableAlias.'`'.bqSQL($sqlId).'` BETWEEN "'.pSQL($matches3[0]).'" AND "'.pSQL($matches3[1])."\"\n";
                }
            }
            else
                $ret .= ' AND '.$tableAlias.'`'.bqSQL($sqlId).'`="'.pSQL($matches[2]).'"'."\n";
        }
        elseif ($matches[1] == '>')
            $ret .= ' AND '.$tableAlias.'`'.bqSQL($sqlId).'` > "'.pSQL($matches[2])."\"\n";
        elseif ($matches[1] == '<')
            $ret .= ' AND '.$tableAlias.'`'.bqSQL($sqlId).'` < "'.pSQL($matches[2])."\"\n";
        elseif ($matches[1] == '!')
            $ret .= ' AND '.$tableAlias.'`'.bqSQL($sqlId).'` != "'.pSQL($matches[2])."\"\n";
    }
    else
        $ret .= ' AND '.$tableAlias.'`'.bqSQL($sqlId).'` '.(Validate::isFloat(pSQL($filterValue)) ? 'LIKE' : '=').' "'.pSQL($filterValue)."\"\n";
    return $ret;
}

由此我们可以注意到,不同于 官方文档 说:

From this we can notice that, unlike what the official documention says:

  • 我们可以使用值之间的管道 | 模拟 OR 条件.
  • 我们可以模拟一个 BETWEEN 条件,在值之间有一个昏迷 ,.
  • 我们可以模拟一个 LIKE %% 条件,其前导或/和后继 % 值占我们值的百分比.
  • 我们可以模拟一个 >> 条件,在值前面有一个 >.
  • 我们可以模拟一个 < 条件,在值前面加上一个 <.
  • 我们可以模拟一个 != 条件,在值前面加上一个 !.
  • We can simulate a OR condition with a pipe | between values.
  • We can simulate a BETWEEN condition with a coma , between values.
  • We can simulate a LIKE %% condition with a leading or/and a following percent % of our value.
  • We can simulate a > condition with a > in front of the value.
  • We can simulate a < condition with a < in front of the value.
  • We can simulate a != condition with a ! in front of the value.

但我找不到模拟 AND 条件的解决方案.如果您发现有人随时更新此答案.

But I can't find a solution to simulate a AND condition. If you find one feel free to update this answer.

您可以轻松覆盖此方法以添加您自己的 AND 过滤器.

You can easily override this method to add your own AND filter.

对于 Prestashop 1.6,! 条件略有不同:

For Prestashop 1.6 there's a little difference with the ! condition:

} elseif ($matches[1] == '!') {
     $multiple_values = explode('|', $matches[2]);
     foreach ($multiple_values as $value) {
         $ret .= ' AND '.$tableAlias.'`'.bqSQL($sqlId).'` != "'.pSQL($value)."\"\n";
     }
}

  • 我们可以通过在每个值之间添加一个管道 | 来模拟 NOT IN(),并在每个值之间添加一个前导 !.
    • We can simulate a NOT IN() by adding a pipe | between each values with a leading !.
    • 这篇关于在 Prestashop 1.5 中列出具有多个类别的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆