【问题标题】:Custom filter to search criteria. Magento 2自定义过滤器搜索条件。 Magento 2
【发布时间】:2017-07-13 21:53:56
【问题描述】:

我需要为年份添加范围过滤器。我在我的模块 FullText\Collection 中重新定义了 Magento 类。我还在 search_request.xml 文件中进行了更改。我找到了代码,它对我有用:

$skus = [ 'CNS334', 'U012840' ];

    $this->filterBuilder->setField('sku');
    $this->filterBuilder->setValue($skus);
    $this->filterBuilder->setConditionType('in');

    $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());

但我在其他表中有数据。我尝试加入,但我无法获得任何过滤结果。

   $this->getSelect()->join(
        [
            'my' => 'make_year',
        ],
        'e.entity_id = my.product_id'

$this->searchCriteriaBuilder->addFilter($this->filterBuilder
        ->setField('year')
        ->setValue(2014)
        ->setConditionType('from')
        ->create());

$this->searchCriteriaBuilder->addFilter($this->filterBuilder
        ->setField('year')
        ->setValue(2015)
        ->setConditionType('to')
        ->create());

    $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());

【问题讨论】:

    标签: magento search filter criteria


    【解决方案1】:

    要将您的查询与其他表连接,您需要条件映射器。

    例如,请查看\Magento\CatalogInventory\Model\ResourceModel\Stock\Item\StockItemCriteria::setStockStatus() - 这里是启动过滤器。

    data 数组中设置新项目会触发映射器方法\Magento\CatalogInventory\Model\ResourceModel\Stock\Item\StockItemCriteriaMapper::mapStockStatus()

    这个映射器包含\Magento\Framework\DB\Select,我们可以像在旧式集合中一样使用它。 映射器方法的名称与标准的data 数组索引相关。所以如果你添加$this->data['custom_field'],映射器函数应该是mapCustomField()

    另请注意,如果该字段有特定的映射器功能,则映射器将尝试按mapped fields 过滤。

    【讨论】:

    • 死 github 链接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 2019-12-24
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多