Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Advanced Class Reference
Inheritance diagram for Advanced:
AbstractModel DataObject

Public Member Functions

 __construct (Context $context, Registry $registry, AttributeCollectionFactory $attributeCollectionFactory, Visibility $catalogProductVisibility, Config $catalogConfig, CurrencyFactory $currencyFactory, ProductFactory $productFactory, StoreManagerInterface $storeManager, ProductCollectionFactory $productCollectionFactory, AdvancedFactory $advancedFactory, array $data=[])
 
 addFilters ($values)
 
 getAttributes ()
 
 getProductCollection ()
 
 prepareProductCollection ($collection)
 
 getSearchCriterias ()
 
- Public Member Functions inherited from AbstractModel
 __construct (\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
 
 __sleep ()
 
 __wakeup ()
 
 setIdFieldName ($name)
 
 getIdFieldName ()
 
 getId ()
 
 setId ($value)
 
 isDeleted ($isDeleted=null)
 
 hasDataChanges ()
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 setDataChanges ($value)
 
 getOrigData ($key=null)
 
 setOrigData ($key=null, $data=null)
 
 dataHasChangedFor ($field)
 
 getResourceName ()
 
 getResourceCollection ()
 
 getCollection ()
 
 load ($modelId, $field=null)
 
 beforeLoad ($identifier, $field=null)
 
 afterLoad ()
 
 isSaveAllowed ()
 
 setHasDataChanges ($flag)
 
 save ()
 
 afterCommitCallback ()
 
 isObjectNew ($flag=null)
 
 beforeSave ()
 
 validateBeforeSave ()
 
 getCacheTags ()
 
 cleanModelCache ()
 
 afterSave ()
 
 delete ()
 
 beforeDelete ()
 
 afterDelete ()
 
 afterDeleteCommit ()
 
 getResource ()
 
 getEntityId ()
 
 setEntityId ($entityId)
 
 clearInstance ()
 
 getStoredData ()
 
 getEventPrefix ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 

Protected Member Functions

 addSearchCriteria ($attribute, $value)
 
- Protected Member Functions inherited from AbstractModel
 _construct ()
 
 _init ($resourceModel)
 
 _setResourceModel ($resourceName, $collectionName=null)
 
 _getResource ()
 
 _getEventData ()
 
 _beforeLoad ($modelId, $field=null)
 
 _afterLoad ()
 
 _hasModelChanged ()
 
 _getValidatorBeforeSave ()
 
 _createValidatorBeforeSave ()
 
 _getValidationRulesBeforeSave ()
 
 _clearReferences ()
 
 _clearData ()
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $_searchCriterias = []
 
 $_productCollection
 
 $_catalogConfig
 
 $_catalogProductVisibility
 
 $_attributeCollectionFactory
 
 $_storeManager
 
 $_productFactory
 
 $_currencyFactory
 
 $productCollectionFactory
 
- Protected Attributes inherited from AbstractModel
 $_eventPrefix = 'core_abstract'
 
 $_eventObject = 'object'
 
 $_idFieldName = 'id'
 
 $_hasDataChanges = false
 
 $_origData
 
 $_isDeleted = false
 
 $_resource
 
 $_resourceCollection
 
 $_resourceName
 
 $_collectionName
 
 $_cacheTag = false
 
 $_dataSaveAllowed = true
 
 $_isObjectNew = null
 
 $_validatorBeforeSave = null
 
 $_eventManager
 
 $_cacheManager
 
 $_registry
 
 $_logger
 
 $_appState
 
 $_actionValidator
 
 $storedData = []
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Catalog advanced search model

@method int getEntityTypeId() @method \Magento\CatalogSearch\Model\Advanced setEntityTypeId(int $value) @method int getAttributeSetId() @method \Magento\CatalogSearch\Model\Advanced setAttributeSetId(int $value) @method string getTypeId() @method \Magento\CatalogSearch\Model\Advanced setTypeId(string $value) @method string getSku() @method \Magento\CatalogSearch\Model\Advanced setSku(string $value) @method int getHasOptions() @method \Magento\CatalogSearch\Model\Advanced setHasOptions(int $value) @method int getRequiredOptions() @method \Magento\CatalogSearch\Model\Advanced setRequiredOptions(int $value) @method string getCreatedAt() @method \Magento\CatalogSearch\Model\Advanced setCreatedAt(string $value) @method string getUpdatedAt() @method \Magento\CatalogSearch\Model\Advanced setUpdatedAt(string $value)

Author
Magento Core Team core@.nosp@m.mage.nosp@m.ntoco.nosp@m.mmer.nosp@m.ce.co.nosp@m.m @SuppressWarnings(PHPMD.CouplingBetweenObjects) @api
Since
100.0.2

Definition at line 48 of file Advanced.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( Context  $context,
Registry  $registry,
AttributeCollectionFactory  $attributeCollectionFactory,
Visibility  $catalogProductVisibility,
Config  $catalogConfig,
CurrencyFactory  $currencyFactory,
ProductFactory  $productFactory,
StoreManagerInterface  $storeManager,
ProductCollectionFactory  $productCollectionFactory,
AdvancedFactory  $advancedFactory,
array  $data = [] 
)

Construct

Parameters
Context$context
Registry$registry
AttributeCollectionFactory$attributeCollectionFactory
Visibility$catalogProductVisibility
Config$catalogConfig
CurrencyFactory$currencyFactory
ProductFactory$productFactory
StoreManagerInterface$storeManager
ProductCollectionFactory$productCollectionFactory
AdvancedFactory$advancedFactory
array$data@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 130 of file Advanced.php.

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  }
$storeManager

Member Function Documentation

◆ addFilters()

addFilters (   $values)

Add advanced search filters to product collection

Parameters
array$values
Returns
$this
Exceptions
LocalizedException@SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Definition at line 168 of file Advanced.php.

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  }
$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
$table
Definition: trigger.php:14

◆ addSearchCriteria()

addSearchCriteria (   $attribute,
  $value 
)
protected

Add search criteria.

Parameters
EntityAttribute$attribute
mixed$value
Returns
void

Definition at line 306 of file Advanced.php.

307  {
308  if (!empty($value)) {
309  $this->_searchCriterias[] = ['name' => $attribute->getStoreLabel(), 'value' => $value];
310  }
311  }
$value
Definition: gender.phtml:16

◆ getAttributes()

getAttributes ( )

Retrieve array of attributes used in advanced search

Returns
array|\Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection

Definition at line 241 of file Advanced.php.

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  }
getData($key='', $index=null)
Definition: DataObject.php:119
$attributes
Definition: matrix.phtml:13

◆ getProductCollection()

getProductCollection ( )

Retrieve advanced search product collection

Returns
Collection

Definition at line 266 of file Advanced.php.

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  }

◆ getSearchCriterias()

getSearchCriterias ( )

Returns prepared search criterias in text

Returns
array

Definition at line 390 of file Advanced.php.

◆ prepareProductCollection()

prepareProductCollection (   $collection)

Prepare product collection

Parameters
Collection$collection
Returns
$this

Definition at line 286 of file Advanced.php.

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  }

Field Documentation

◆ $_attributeCollectionFactory

$_attributeCollectionFactory
protected

Definition at line 83 of file Advanced.php.

◆ $_catalogConfig

$_catalogConfig
protected

Definition at line 69 of file Advanced.php.

◆ $_catalogProductVisibility

$_catalogProductVisibility
protected

Definition at line 76 of file Advanced.php.

◆ $_currencyFactory

$_currencyFactory
protected

Definition at line 104 of file Advanced.php.

◆ $_productCollection

$_productCollection
protected

Definition at line 62 of file Advanced.php.

◆ $_productFactory

$_productFactory
protected

Definition at line 97 of file Advanced.php.

◆ $_searchCriterias

$_searchCriterias = []
protected

Definition at line 55 of file Advanced.php.

◆ $_storeManager

$_storeManager
protected

Definition at line 90 of file Advanced.php.

◆ $productCollectionFactory

$productCollectionFactory
protected

Definition at line 111 of file Advanced.php.


The documentation for this class was generated from the following file: