/** * Prepare search condition for attribute * * @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute * @param string|array $value * @return string|array * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function prepareCondition($attribute, $value) { $condition = false; if (is_array($value)) { if ($attribute->getBackendType() == 'varchar') { // multiselect // multiselect $condition = ['in_set' => $value]; } elseif (!isset($value['from']) && !isset($value['to'])) { // select // select $condition = ['in' => $value]; } elseif (isset($value['from']) && '' !== $value['from'] || isset($value['to']) && '' !== $value['to']) { // range $condition = $value; } } else { if (strlen($value) > 0) { if (in_array($attribute->getBackendType(), ['varchar', 'text', 'static'])) { $condition = ['like' => '%' . $value . '%']; // text search } else { $condition = $value; } } } return $condition; }
/** * Get Attribute Filter Class Name * * @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute * @return string */ protected function getAttributeFilterClass(\Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute) { $filterClassName = $this->filterTypes[self::ATTRIBUTE_FILTER]; if ($attribute->getAttributeCode() == 'price') { $filterClassName = $this->filterTypes[self::PRICE_FILTER]; } elseif ($attribute->getBackendType() == 'decimal') { $filterClassName = $this->filterTypes[self::DECIMAL_FILTER]; } return $filterClassName; }
/** * @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection * @return $this */ protected function addGlobalAttribute(\Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute, \Magento\Catalog\Model\ResourceModel\Product\Collection $collection) { $storeId = $this->storeManager->getStore()->getId(); switch ($attribute->getBackendType()) { case 'decimal': case 'datetime': case 'int': $alias = 'at_' . $attribute->getAttributeCode(); $collection->addAttributeToSelect($attribute->getAttributeCode(), 'inner'); break; default: $alias = 'at_' . md5($this->getId()) . $attribute->getAttributeCode(); $collection->getSelect()->join([$alias => $collection->getTable('catalog_product_index_eav')], "({$alias}.entity_id = e.entity_id) AND ({$alias}.store_id = {$storeId})" . " AND ({$alias}.attribute_id = {$attribute->getId()})", []); } $this->joinedAttributes[$attribute->getAttributeCode()] = $alias . '.value'; return $this; }
/** * {@inheritdoc} */ public function getBackendType() { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getBackendType'); if (!$pluginInfo) { return parent::getBackendType(); } else { return $this->___callPlugins('getBackendType', func_get_args(), $pluginInfo); } }
/** * @param FilterInterface $filter * @param string $query * @param Attribute $attribute * @return string */ private function processRangeNumeric(FilterInterface $filter, $query, $attribute) { $tableSuffix = $attribute->getBackendType() === 'decimal' ? '_decimal' : ''; $table = $this->resource->getTableName("catalog_product_index_eav{$tableSuffix}"); $select = $this->connection->select(); $currentStoreId = $this->scopeResolver->getScope()->getId(); $select->from(['main_table' => $table], 'entity_id')->columns([$filter->getField() => 'main_table.value'])->where('main_table.attribute_id = ?', $attribute->getAttributeId())->where('main_table.store_id = ?', $currentStoreId)->having($query); $resultQuery = 'search_index.entity_id IN ( select entity_id from ' . $this->conditionManager->wrapBrackets($select) . ' as filter )'; return $resultQuery; }