0

我正在尝试使用手工艺品过滤我的产品。

有没有办法做 craft.commerce.products.search("(field_color:red ORfield_color:blue) (field_size:S ORfield_size:M OR field_size:XXL)")

或者任何其他解决方案都可以帮助我实现这个过滤器。

这是我的代码:

    {% set productNumber = 12 %}
    {% set priceMin = 0 %}
    {% set priceMax = 1000 %}

    {% set query = "(field_color:red OR field_color:blue) (field_size:S OR           field_size:M OR field_size:XXL)" %}
    {% set product_array = craft.commerce.products({
                type:typeArray|default(""),
                order:sort|default(""),
                defaultPrice: [
                'and',
                '>= ' ~ priceMin,
                '<= ' ~ priceMax,
                ]
                }).search(query) %}

    {% set product_array_limited = product_array.limit(productNumber|default("")) %}
    {% paginate product_array_limited as product_array_pagenated %}

        {% for product in product_array_pagenated %}

        "Here is my product"

        {% endfor %}

    {% endpaginate %}
4

1 回答 1

1

我自己想通了。我正在使用工艺“relatedTo”过滤器来加入搜索内容。在我的系统中,“颜色”和“尺寸”被保存为类别。在商务产品部分,“颜色”和“尺寸”与具有“productColor”和“productSize”字段的产品相关。

就像下图一样:

产品部分相关尺寸和颜色类别示例图片

而“尺寸”和“颜色”的类别包含“标题”、“蛞蝓”。

类别示例图像中的大小和颜色

在工艺类别中,您将看到该类别的 Id,如下图所示。

类别 ID 示例图片

这是我的代码:

{% set colorArray = ["blue","black"] %}
{% set sizeArray = ["s","28"] %}

{% set colorId = [] %}
{% for color in colorArray %}
   {% set colorId = colorId | merge(craft.categories.slug(color).Ids()) %}
{% endfor %}

{% set sizeId = [] %}
{% for size in sizeArray %}
   {% set sizeId = sizeId | merge(craft.categories.slug(size).Ids()) %}
{% endfor %}

{% set IdMerge = craft.customcode.mergeArrayRemoveRedundancy(colorId,sizeId) %}
{% set productNumber = 12 %}
{% set priceMin = 0 %}
{% set priceMax = 1000 %}

{% set product_array = craft.commerce.products({
                type:typeArray|default(""),
                order:sort|default(""),
                defaultPrice: [
                'and',
                '>= ' ~ priceMin,
                '<= ' ~ priceMax,
                ]
                }).relatedTo(IdMerge) %}
{% for product in product_array_pagenated %}

{# Your product #}

{% endfor %}

这是我的“customcode”插件中的“mergeArrayRemoveRedundancy”函数的 PHP 类。

 /**
 * merge array remove redundancy
 * @param $a1 array
 * @param $a2 array
 * @return array
 */
public function mergeArrayRemoveRedundancy($a1,$a2){
    $a1 = (array)$a1;
    $a2 = (array)$a2;
    return array_unique(array_merge($a1,$a2), SORT_REGULAR);
}

您可以使用“ https://pluginfactory.io/ ”创建自定义工艺插件,只需在 /yourPluginFolder/variables 文件夹下添加 PHP 类。

于 2018-04-19T00:38:42.167 回答