Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Advanced.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Catalog\Model\ProductFactory;
12 use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory;
13 use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
15 use Magento\CatalogSearch\Model\ResourceModel\AdvancedFactory;
16 use Magento\Directory\Model\CurrencyFactory;
22 
49 {
55  protected $_searchCriterias = [];
56 
63 
69  protected $_catalogConfig;
70 
77 
84 
90  protected $_storeManager;
91 
97  protected $_productFactory;
98 
104  protected $_currencyFactory;
105 
112 
130  public function __construct(
131  Context $context,
133  AttributeCollectionFactory $attributeCollectionFactory,
134  Visibility $catalogProductVisibility,
135  Config $catalogConfig,
136  CurrencyFactory $currencyFactory,
137  ProductFactory $productFactory,
139  ProductCollectionFactory $productCollectionFactory,
140  AdvancedFactory $advancedFactory,
141  array $data = []
142  ) {
143  $this->_attributeCollectionFactory = $attributeCollectionFactory;
144  $this->_catalogProductVisibility = $catalogProductVisibility;
145  $this->_catalogConfig = $catalogConfig;
146  $this->_currencyFactory = $currencyFactory;
147  $this->_productFactory = $productFactory;
148  $this->_storeManager = $storeManager;
149  $this->productCollectionFactory = $productCollectionFactory;
150  parent::__construct(
151  $context,
152  $registry,
153  $advancedFactory->create(),
154  $this->productCollectionFactory->create(),
155  $data
156  );
157  }
158 
168  public function addFilters($values)
169  {
170  $attributes = $this->getAttributes();
171  $allConditions = [];
172 
173  foreach ($attributes as $attribute) {
174  /* @var $attribute Attribute */
175  if (!isset($values[$attribute->getAttributeCode()])) {
176  continue;
177  }
178  $value = $values[$attribute->getAttributeCode()];
179  $preparedSearchValue = $this->getPreparedSearchCriteria($attribute, $value);
180  if (false === $preparedSearchValue) {
181  continue;
182  }
183  $this->addSearchCriteria($attribute, $preparedSearchValue);
184 
185  if ($attribute->getAttributeCode() == 'price') {
186  $rate = 1;
187  $store = $this->_storeManager->getStore();
188  $currency = $store->getCurrentCurrencyCode();
189  if ($currency != $store->getBaseCurrencyCode()) {
190  $rate = $store->getBaseCurrency()->getRate($currency);
191  }
192 
193  $value['from'] = (isset($value['from']) && is_numeric($value['from']))
194  ? (float)$value['from'] / $rate
195  : '';
196  $value['to'] = (isset($value['to']) && is_numeric($value['to']))
197  ? (float)$value['to'] / $rate
198  : '';
199  }
200 
201  if ($attribute->getBackendType() == 'datetime') {
202  $value['from'] = (isset($value['from']) && !empty($value['from']))
203  ? date('Y-m-d\TH:i:s\Z', strtotime($value['from']))
204  : '';
205  $value['to'] = (isset($value['to']) && !empty($value['to']))
206  ? date('Y-m-d\TH:i:s\Z', strtotime($value['to']))
207  : '';
208  }
209  $condition = $this->_getResource()->prepareCondition(
210  $attribute,
211  $value,
212  $this->getProductCollection()
213  );
214  if ($condition === false) {
215  continue;
216  }
217 
218  $table = $attribute->getBackend()->getTable();
219  if ($attribute->getBackendType() == 'static') {
220  $attributeId = $attribute->getAttributeCode();
221  } else {
222  $attributeId = $attribute->getId();
223  }
224  $allConditions[$table][$attributeId] = $condition;
225  }
226  if ($allConditions) {
227  $this->_registry->register('advanced_search_conditions', $allConditions);
228  $this->getProductCollection()->addFieldsToFilter($allConditions);
229  } else {
230  throw new LocalizedException(__('Enter a search term and try again.'));
231  }
232 
233  return $this;
234  }
235 
241  public function getAttributes()
242  {
243  $attributes = $this->getData('attributes');
244  if ($attributes === null) {
245  $product = $this->_productFactory->create();
246  $attributes = $this->_attributeCollectionFactory
247  ->create()
248  ->addHasOptionsFilter()
249  ->addDisplayInAdvancedSearchFilter()
250  ->addStoreLabel($this->_storeManager->getStore()->getId())
251  ->setOrder('main_table.attribute_id', 'asc')
252  ->load();
253  foreach ($attributes as $attribute) {
254  $attribute->setEntity($product->getResource());
255  }
256  $this->setData('attributes', $attributes);
257  }
258  return $attributes;
259  }
260 
266  public function getProductCollection()
267  {
268  if ($this->_productCollection === null) {
269  $collection = $this->productCollectionFactory->create();
271  if (!$collection) {
272  return $collection;
273  }
274  $this->_productCollection = $collection;
275  }
276 
278  }
279 
287  {
289  ->addAttributeToSelect($this->_catalogConfig->getProductAttributes())
290  ->setStore($this->_storeManager->getStore())
291  ->addMinimalPrice()
292  ->addTaxPercents()
293  ->addStoreFilter()
294  ->setVisibility($this->_catalogProductVisibility->getVisibleInSearchIds());
295 
296  return $this;
297  }
298 
306  protected function addSearchCriteria($attribute, $value)
307  {
308  if (!empty($value)) {
309  $this->_searchCriterias[] = ['name' => $attribute->getStoreLabel(), 'value' => $value];
310  }
311  }
312 
324  protected function getPreparedSearchCriteria($attribute, $value)
325  {
326  if (is_array($value)) {
327  if (isset($value['from']) && isset($value['to'])) {
328  if (!empty($value['from']) || !empty($value['to'])) {
329  if (isset($value['currency'])) {
331  $currencyModel = $this->_currencyFactory->create()->load($value['currency']);
332  $from = $currencyModel->format($value['from'], [], false);
333  $to = $currencyModel->format($value['to'], [], false);
334  } else {
335  $currencyModel = null;
336  }
337 
338  if (strlen($value['from']) > 0 && strlen($value['to']) > 0) {
339  // -
340  $value = sprintf(
341  '%s - %s',
342  $currencyModel ? $from : $value['from'],
343  $currencyModel ? $to : $value['to']
344  );
345  } elseif (strlen($value['from']) > 0) {
346  // and more
347  $value = __('%1 and greater', $currencyModel ? $from : $value['from']);
348  } elseif (strlen($value['to']) > 0) {
349  // to
350  $value = __('up to %1', $currencyModel ? $to : $value['to']);
351  }
352  } else {
353  return '';
354  }
355  }
356  }
357 
358  if (($attribute->getFrontendInput() == 'select' ||
359  $attribute->getFrontendInput() == 'multiselect') && is_array($value)
360  ) {
361  foreach ($value as $key => $val) {
362  $value[$key] = $attribute->getSource()->getOptionText($val);
363 
364  if (is_array($value[$key])) {
365  $value[$key] = $value[$key]['label'];
366  }
367  }
368  $value = implode(', ', $value);
369  } elseif ($attribute->getFrontendInput() == 'select' || $attribute->getFrontendInput() == 'multiselect') {
370  $value = $attribute->getSource()->getOptionText($value);
371  if (is_array($value)) {
372  $value = $value['label'];
373  }
374  } elseif ($attribute->getFrontendInput() == 'boolean') {
375  if (is_numeric($value)) {
376  $value = $value == 1 ? __('Yes') : __('No');
377  } else {
378  $value = false;
379  }
380  }
381 
382  return $value;
383  }
384 
390  public function getSearchCriterias()
391  {
393  }
394 }
getData($key='', $index=null)
Definition: DataObject.php:119
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$storeManager
$values
Definition: options.phtml:88
__()
Definition: __.php:13
addSearchCriteria($attribute, $value)
Definition: Advanced.php:306
$value
Definition: gender.phtml:16
$attributes
Definition: matrix.phtml:13
__construct(Context $context, Registry $registry, AttributeCollectionFactory $attributeCollectionFactory, Visibility $catalogProductVisibility, Config $catalogConfig, CurrencyFactory $currencyFactory, ProductFactory $productFactory, StoreManagerInterface $storeManager, ProductCollectionFactory $productCollectionFactory, AdvancedFactory $advancedFactory, array $data=[])
Definition: Advanced.php:130
$table
Definition: trigger.php:14