/** * @param string $name * @param string $primaryFieldName * @param string $requestFieldName * @param CollectionFactory $collectionFactory * @param RequestInterface $request * @param array $meta * @param array $data */ public function __construct($name, $primaryFieldName, $requestFieldName, CollectionFactory $collectionFactory, RequestInterface $request, array $meta = [], array $data = []) { parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data); $this->request = $request; $this->collection = $collectionFactory->create(); $this->collection->setExcludeSetFilter((int) $this->request->getParam('template_id', 0)); }
/** * @param string $name * @param string $primaryFieldName * @param string $requestFieldName * @param CollectionFactory $collectionFactory * @param StoreRepositoryInterface $storeRepository * @param ArrayManager $arrayManager * @param array $meta * @param array $data */ public function __construct($name, $primaryFieldName, $requestFieldName, CollectionFactory $collectionFactory, StoreRepositoryInterface $storeRepository, ArrayManager $arrayManager, array $meta = [], array $data = []) { parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data); $this->collection = $collectionFactory->create(); $this->storeRepository = $storeRepository; $this->arrayManager = $arrayManager; }
/** * Enable requested attribute for products, if no products are using it * * Date attributes is supported for attribute_from values only. * * @param array $data Key => Value pairs of attribute_code and count * @return void */ public function execute($data) { $visibility = $this->catalogProductVisibility->getVisibleInCatalogIds(); $attributes = $this->attributeCollectionFactory->create()->addFieldToFilter('attribute_code', ['in' => array_keys($data)]); foreach ($attributes as $attribute) { $collection = $this->productCollectionFactory->create()->setPageSize(1)->setCurPage(1); switch ($attribute->getFrontendInput()) { case 'boolean': $value = 1; $collection->addAttributeToFilter($attribute, 1); break; case 'date': $value = $this->localeDate->date()->format('Y-m-d H:i:s'); $collection->addAttributeToFilter($attribute, [['date' => true, 'to' => $value]]); break; } if ($collection->getSize()) { // customer already has some products with specified attribute continue; } foreach ($this->getStoreIds() as $storeId) { $visibleProducts = $this->productCollectionFactory->create()->setStoreId($storeId)->setVisibility($visibility)->addStoreFilter($storeId)->setPageSize($data[$attribute->getAttributeCode()])->setCurPage(1); if (!$visibleProducts->getSize()) { continue; } foreach ($visibleProducts as $product) { $product->addAttributeUpdate($attribute->getAttributeCode(), (int) in_array(0, $this->getStoreIds()), 0); $product->setStoreId($storeId)->setData($attribute->getAttributeCode(), $value)->save(); } } } }
/** * Retrieve list of filterable attributes * * @return array|\Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */ public function getList() { /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */ $collection = $this->collectionFactory->create(); $collection->setItemObjectClass('Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute')->addStoreLabel($this->storeManager->getStore()->getId())->setOrder('position', 'ASC'); $collection = $this->_prepareAttributeCollection($collection); $collection->load(); return $collection; }
/** * {@inheritdoc} */ public function getList($attributeSetName) { /** @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection $collection */ $collection = $this->collectionFactory->create(); $collection->setAttributeSetFilterBySetName($attributeSetName, Product::ENTITY); $collection->setFrontendInputTypeFilter('media_image'); $collection->addStoreLabel($this->storeManager->getStore()->getId()); return $collection->getItems(); }
/** * Retrieve list of attributes * * @param array $ids * @return array */ public function getAttributes($ids) { $collection = $this->collectionFactory->create(); $collection->addFieldToFilter('main_table.attribute_id', $ids); $attributes = []; foreach ($collection->getItems() as $attribute) { $attributes[] = ['id' => $attribute->getId(), 'label' => $attribute->getFrontendLabel(), 'code' => $attribute->getAttributeCode(), 'options' => $attribute->getSource()->getAllOptions(false)]; } return $attributes; }
/** * Search for attributes by part of attribute's label in admin store * * @return void */ public function executeInternal() { $this->storeManager->setCurrentStore(\Magento\Store\Model\Store::ADMIN_CODE); $collection = $this->collectionFactory->create(); $collection->addFieldToFilter('main_table.attribute_id', $this->getRequest()->getParam('attributes')); $attributes = []; foreach ($collection->getItems() as $attribute) { $attributes[] = ['id' => $attribute->getId(), 'label' => $attribute->getFrontendLabel(), 'code' => $attribute->getAttributeCode(), 'options' => $attribute->getSource()->getAllOptions(false)]; } $this->getResponse()->representJson($this->jsonHelper->jsonEncode($attributes)); }
/** * Constructor. * * @param ItemFactory $itemFactory Autocomplete item factory. * @param ProductCollection $productCollection Autocomplete product collection. * @param AttributeCollectionFactory $attributeCollectionFactory Product attribute collection factory. * @param AutocompleteHelper $autocompleteHelper Autocomplete configuration helper. * @param StoreManagerInterface $storeManager Store manager. * @param string $type Autocomplete type code. */ public function __construct(ItemFactory $itemFactory, ProductCollection $productCollection, AttributeCollectionFactory $attributeCollectionFactory, AutocompleteHelper $autocompleteHelper, StoreManagerInterface $storeManager, $type = self::AUTOCOMPLETE_TYPE) { $this->itemFactory = $itemFactory; $this->type = $type; $this->storeManager = $storeManager; $this->productCollection = $productCollection; $this->attributeCollection = $attributeCollectionFactory->create(); $this->autocompleteHelper = $autocompleteHelper; $this->loadAttributeCollection(); $this->prepareProductCollection(); }
/** * Retrieve list of attributes with admin store label containing $labelPart * * @param string $labelPart * @param int $templateId * @return \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */ public function getSuggestedAttributes($labelPart, $templateId = null) { $escapedLabelPart = $this->_resourceHelper->addLikeEscape($labelPart, ['position' => 'any']); /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */ $collection = $this->_collectionFactory->create()->addFieldToFilter('frontend_label', ['like' => $escapedLabelPart]); $collection->setExcludeSetFilter($templateId ?: $this->getRequest()->getParam('template_id'))->setPageSize(20); $result = []; foreach ($collection->getItems() as $attribute) { /** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */ $result[] = ['id' => $attribute->getId(), 'label' => $attribute->getFrontendLabel(), 'code' => $attribute->getAttributeCode()]; } return $result; }
/** * Check if group contains system attributes * * @return bool */ public function hasSystemAttributes() { $result = false; /** @var $attributesCollection \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */ $attributesCollection = $this->_attributeCollectionFactory->create(); $attributesCollection->setAttributeGroupFilter($this->getId()); foreach ($attributesCollection as $attribute) { if (!$attribute->getIsUserDefined()) { $result = true; break; } } return $result; }
/** * Get fields prefixes * * @return array */ public function getPrefixes() { // use cached eav config $entityTypeId = $this->_eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId(); /* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */ $collection = $this->_attributeCollectionFactory->create(); $collection->setEntityTypeFilter($entityTypeId); $collection->setFrontendInputTypeFilter('media_image'); $prefixes = []; foreach ($collection as $attribute) { /* @var $attribute \Magento\Eav\Model\Entity\Attribute */ $prefixes[] = ['field' => $attribute->getAttributeCode() . '_', 'label' => $attribute->getFrontend()->getLabel()]; } return $prefixes; }
public function testGetList() { $storeMock = $this->getMock('\\Magento\\Store\\Model\\Store', [], [], '', false); $this->storeManagerMock->expects($this->once())->method('getStore')->will($this->returnValue($storeMock)); $storeId = 4321; $storeMock->expects($this->once())->method('getId')->will($this->returnValue($storeId)); $collectionMock = $this->getMock('\\Magento\\Catalog\\Model\\ResourceModel\\Product\\Attribute\\Collection', [], [], '', false); $this->collectionFactoryMock->expects($this->once())->method('create')->will($this->returnValue($collectionMock)); $collectionMock->expects($this->once())->method('setItemObjectClass')->with('Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute')->will($this->returnSelf()); $collectionMock->expects($this->once())->method('addStoreLabel')->with($storeId)->will($this->returnSelf()); $collectionMock->expects($this->once())->method('setOrder')->with('position', 'ASC'); $collectionMock->expects($this->once())->method('addIsFilterableInSearchFilter')->will($this->returnSelf()); $collectionMock->expects($this->once())->method('load'); $this->assertEquals($collectionMock, $this->model->getList()); }
/** * Retrieve searchable attributes * * @return \Magento\Catalog\Model\Entity\Attribute[] */ protected function getSearchableAttributes() { /** @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection $productAttributes */ $productAttributes = $this->productAttributeCollectionFactory->create(); $productAttributes->addFieldToFilter(['is_searchable', 'is_visible_in_advanced_search', 'is_filterable', 'is_filterable_in_search'], [1, 1, [1, 2], 1]); return $productAttributes; }
/** * Retrieve searchable attributes * * @param string $backendType * @return \Magento\Eav\Model\Entity\Attribute[] */ private function getSearchableAttributes($backendType = null) { if (null === $this->searchableAttributes) { $this->searchableAttributes = []; /** @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection $productAttributes */ $productAttributes = $this->productAttributeCollectionFactory->create(); $productAttributes->addToIndexFilter(true); /** @var \Magento\Eav\Model\Entity\Attribute[] $attributes */ $attributes = $productAttributes->getItems(); $this->eventManager->dispatch('catelogsearch_searchable_attributes_load_after', ['engine' => $this->engine, 'attributes' => $attributes]); $entity = $this->getEavConfig()->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getEntity(); foreach ($attributes as $attribute) { $attribute->setEntity($entity); } $this->searchableAttributes = $attributes; } if ($backendType !== null) { $attributes = []; foreach ($this->searchableAttributes as $attributeId => $attribute) { if ($attribute->getBackendType() == $backendType) { $attributes[$attributeId] = $attribute; } } return $attributes; } return $this->searchableAttributes; }
/** * Retrieve attribute collection prefiltered with only attribute usable in rules. * * @return \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */ public function getAttributeCollection() { if ($this->attributeCollection === null) { $this->attributeCollection = $this->attributeCollectionFactory->create(); $mapping = $this->getMapping(); $attributeNameMapping = array_flip($this->fieldNameMapping); $arrayNameCb = function (FieldInterface $field) use($attributeNameMapping) { $attributeName = $field->getName(); if (isset($attributeNameMapping[$attributeName])) { $attributeName = $attributeNameMapping[$attributeName]; } return $attributeName; }; $attributeFilterCb = function (FieldInterface $field) use($mapping) { try { $fieldName = $field->getName(); $optionTextFieldName = $this->mappingHelper->getOptionTextFieldName($fieldName); $field = $mapping->getField($optionTextFieldName); } catch (\Exception $e) { } return $field->isFilterable() || $field->isSearchable(); }; $fieldNames = array_map($arrayNameCb, array_filter($this->getMapping()->getFields(), $attributeFilterCb)); $this->attributeCollection->addFieldToFilter('attribute_code', $fieldNames)->addFieldToFilter('backend_type', ['neq' => 'datetime']); } return $this->attributeCollection; }
protected function _getAttributeCollection() { if (is_null($this->attributeCollection)) { $this->attributeCollection = $this->attributeCollectionFactory->create(); } return $this->attributeCollection; }
/** * Retrieve searchable attributes * * @return Attribute[] */ private function getSearchableAttributes() { if ($this->searchableAttributes === null) { $this->searchableAttributes = []; /** @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection $productAttributes */ $productAttributes = $this->collectionFactory->create(); $productAttributes->addToIndexFilter(true); /** @var \Magento\Eav\Model\Entity\Attribute[] $attributes */ $attributes = $productAttributes->getItems(); $entity = $this->eavConfig->getEntityType(Product::ENTITY)->getEntity(); foreach ($attributes as $attribute) { $attribute->setEntity($entity); } $this->searchableAttributes = $attributes; } return $this->searchableAttributes; }
/** * Retrieve collection of all attributes * * @return \Magento\Framework\Data\Collection\AbstractDb */ protected function _getAttributesCollection() { if (!$this->_attributesCollection) { $this->_attributesCollection = $this->_attributeCollectionFactory->create()->load(); foreach ($this->_attributesCollection as $attribute) { $attribute->setEntity($this->getEntity()); } } return $this->_attributesCollection; }
/** * @param $params * @return \stdClass */ public function getFiltersValues($params) { $filters = new \stdClass(); if (isset($params['cat'])) { $filters->filter_hc_category = ''; $category = $this->catalogCategory->load($params['cat']); $categories = explode('/', $category->getPath()); foreach ($categories as $cat) { $name = $category = $this->catalogCategory->load($cat)->getName(); if (strpos($name, '/') !== false) { $name = str_replace('/', '\\/', $name); } $filters->filter_hc_category .= '/' . $name; } unset($params['cat']); } if (isset($params['price'])) { $prices = explode('-', $params['price']); if (!empty($prices[0])) { $filters->filter_from_incl_price = $prices[0]; } if (!empty($prices[1])) { $filters->filter_to_incl_price = $prices[1]; } unset($params['price']); } if (isset($params)) { $list = $this->factory->create(); $list->load(); foreach ($params as $param => $values) { $getAttribute = null; foreach ($list as $cat) { if ($param == $cat->getName()) { $getAttribute = $cat; } } if ($getAttribute !== null) { $values = html_entity_decode($values); preg_match_all('!\\d+!', $values, $matches); if (is_array($matches[0])) { $attrValues = array(); $paramName = 'filter_' . $param; foreach ($matches[0] as $id) { $attribute = $attribute = $getAttribute->getSource()->getOptionText($id); $attrValues[] = $attribute; } $filters->paramName = $attrValues; } } } } return $filters; }
/** * Retrieve array of attributes used in advanced search * * @return array|\Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */ public function getAttributes() { $attributes = $this->getData('attributes'); if ($attributes === null) { $product = $this->_productFactory->create(); $attributes = $this->_attributeCollectionFactory->create()->addHasOptionsFilter()->addDisplayInAdvancedSearchFilter()->addStoreLabel($this->_storeManager->getStore()->getId())->setOrder('main_table.attribute_id', 'asc')->load(); foreach ($attributes as $attribute) { $attribute->setEntity($product->getResource()); } $this->setData('attributes', $attributes); } return $attributes; }
/** * Loads all attributes with options for attribute * * @param string $attributeCode * @return $this */ protected function loadAttributeOptions($attributeCode) { /** @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection $collection */ $collection = $this->attributeCollectionFactory->create(); $collection->addFieldToSelect(['attribute_code', 'attribute_id']); $collection->addFieldToFilter('attribute_code', $attributeCode); $collection->setFrontendInputTypeFilter(['in' => ['select', 'multiselect']]); foreach ($collection as $item) { $options = $this->attrOptionCollectionFactory->create()->setAttributeFilter($item->getAttributeId())->setPositionOrder('asc', true)->load(); $this->attributeCodeOptionsPair[$item->getAttributeCode()] = $options; } return $this; }
/** * Add attribute to attribute set * * @return \Magento\Framework\Controller\Result\Json */ public function execute() { $request = $this->getRequest(); $response = new \Magento\Framework\DataObject(); $response->setError(false); try { $attributeSet = $this->_objectManager->create('Magento\\Eav\\Model\\Entity\\Attribute\\Set')->load($request->getParam('templateId')); /** @var \Magento\Eav\Model\ResourceModel\Attribute\Collection $collection */ $attributesCollection = $this->attributeCollectionFactory->create(); $attributesIds = $request->getParam('attributesIds'); if ($attributesIds['excludeMode'] === 'false' && !empty($attributesIds['selected'])) { $attributesCollection->addFieldToFilter('main_table.attribute_id', ['in' => $attributesIds['selected']]); } elseif ($attributesIds['excludeMode'] === 'true') { $attributesCollection->setExcludeSetFilter($attributeSet->getId()); } else { throw new \Magento\Framework\Exception\LocalizedException(__('Please, specify attributes')); } $groupCode = $request->getParam('groupCode'); /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\Collection $attributeGroupCollection */ $attributeGroupCollection = $this->_objectManager->get('Magento\\Eav\\Model\\ResourceModel\\Entity\\Attribute\\Group\\Collection'); $attributeGroupCollection->setAttributeSetFilter($attributeSet->getId()); $attributeGroupCollection->addFilter('attribute_group_code', $groupCode); $attributeGroupCollection->setPageSize(1); $attributeGroup = $attributeGroupCollection->getFirstItem(); if (!$attributeGroup->getId()) { $attributeGroup->addData(['attribute_group_code' => $groupCode, 'attribute_set_id' => $attributeSet->getId(), 'attribute_group_name' => $request->getParam('groupName'), 'sort_order' => $request->getParam('groupSortOrder')]); $attributeGroup->save(); } foreach ($attributesCollection as $attribute) { $attribute->setAttributeSetId($attributeSet->getId())->loadEntityAttributeIdBySet(); $attribute->setAttributeGroupId($attributeGroup->getId())->setSortOrder('0')->save(); } } catch (\Exception $e) { $response->setError(true); $response->setMessage($e->getMessage()); } return $this->resultJsonFactory->create()->setJsonData($response->toJson()); }
/** * Retrieve Unused in Attribute Set Attribute Tree as JSON * * @return string */ public function getAttributeTreeJson() { $items = []; $setId = $this->_getSetId(); $collection = $this->_collectionFactory->create()->setAttributeSetFilter($setId)->load(); $attributesIds = ['0']; /* @var $item \Magento\Eav\Model\Entity\Attribute */ foreach ($collection->getItems() as $item) { $attributesIds[] = $item->getAttributeId(); } $attributes = $this->_collectionFactory->create()->setAttributesExcludeFilter($attributesIds)->addVisibleFilter()->load(); foreach ($attributes as $child) { $attr = ['text' => $child->getAttributeCode(), 'id' => $child->getAttributeId(), 'cls' => 'leaf', 'allowDrop' => false, 'allowDrag' => true, 'leaf' => true, 'is_user_defined' => $child->getIsUserDefined(), 'entity_id' => $child->getEntityId()]; $items[] = $attr; } if (count($items) == 0) { $items[] = ['text' => __('Empty'), 'id' => 'empty', 'cls' => 'folder', 'allowDrop' => false, 'allowDrag' => false]; } return $this->_jsonEncoder->encode($items); }
/** * Save search weights for catalog attributes * {@inheritdoc} */ public function afterModelSave() { $attributes = $this->getModel()->getData('attributes'); if (!is_array($attributes)) { return parent::afterModelSave(); } $entityTypeId = $this->objectManager->create('Magento\\Eav\\Model\\Entity')->setType(Product::ENTITY)->getTypeId(); $collection = $this->attributeCollectionFactory->create()->addFieldToFilter('is_searchable', 1); /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */ foreach ($collection as $attribute) { if (!in_array($attribute->getAttributeCode(), $attributes) && $attribute->getIsSearchable()) { $attribute->setIsSearchable(0)->save(); } } foreach ($attributes as $code => $weight) { /** @var \Magento\Eav\Model\Entity\Attribute $attribute */ $attribute = $this->eavAttribute->loadByCode($entityTypeId, $code); $attribute->setSearchWeight($weight)->setIsSearchable(1)->save(); } return parent::afterModelSave(); }
/** * Get attributes from attribute set. * * @param $attributeSetId * * @return array */ public function _getAttributesArray($attributeSetId) { $result = []; $attributes = $this->attributeCollection->create()->setAttributeSetFilter($attributeSetId)->getItems(); foreach ($attributes as $attribute) { $result[] = $attribute->getAttributeCode(); } return $result; }
/** * @param AppResource $resource * @param StoreManagerInterface $storeManager * @param CollectionFactory $attributeCollectionFactory */ public function __construct(AppResource $resource, StoreManagerInterface $storeManager, CollectionFactory $attributeCollectionFactory) { $this->resource = $resource; $this->storeManager = $storeManager; $this->attributeCollection = $attributeCollectionFactory->create(); }
/** * Retrieve list of attributes applicable for configurable product * * @return \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */ public function getApplicableAttributes() { /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */ $collection = $this->collectionFactory->create(); return $collection->addFieldToFilter('frontend_input', 'select')->addFieldToFilter('is_user_defined', 1)->addFieldToFilter('is_global', \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL); }
/** * Retrieve a new product attribute collection. * * @return \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */ public function getAttributeCollection() { return $this->attributeCollectionFactory->create(); }
/** * Prepare product attributes grid collection object * * @return $this */ protected function _prepareCollection() { $collection = $this->_collectionFactory->create()->addVisibleFilter(); $this->setCollection($collection); return parent::_prepareCollection(); }