/** * @param AbstractEntity $resource * @param DataObject|null $object * @return array */ private function checkAndInitAttributes(AbstractEntity $resource, DataObject $object = null) { $attributeCodes = $this->config->getEntityAttributeCodes($resource->getEntityType(), $object); $attributes = []; /** * Check and init default attributes */ $defaultAttributes = $resource->getDefaultAttributes(); foreach ($defaultAttributes as $attributeCode) { $attributeIndex = array_search($attributeCode, $attributeCodes); if ($attributeIndex !== false) { $attribute = $resource->getAttribute($attributeCodes[$attributeIndex]); $attributes[] = $attribute; unset($attributeCodes[$attributeIndex]); } else { $attribute = $this->_getDefaultAttribute($resource, $attributeCode); $attributes[] = $attribute; $resource->addAttribute($attribute); } } foreach ($attributeCodes as $code) { $attribute = $resource->getAttribute($code); $attributes[] = $attribute; } return $attributes; }
/** * Get all attribute codes for a given entity type and attribute set * * @param string $entityType * @param int $attributeSetId * @param string|null $storeId * @return array Attribute codes */ public function getAllAttributeCodes($entityType, $attributeSetId = 0, $storeId = null) { if (null === $storeId) { $storeId = $this->storeManager->getStore()->getId(); } $object = new \Magento\Framework\DataObject(['store_id' => $storeId, 'attribute_set_id' => $attributeSetId]); return $this->eavConfig->getEntityAttributeCodes($entityType, $object); }
/** * Returns Configurable Products Collection with added swatch attributes * * @param ConfigurableProduct $subject * @param Collection $result * @return Collection * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterGetUsedProductCollection(ConfigurableProductType $subject, Collection $result) { $attributeCodes = ['image']; $entityType = $result->getEntity()->getType(); foreach ($this->eavConfig->getEntityAttributeCodes($entityType) as $code) { $attribute = $this->eavConfig->getAttribute($entityType, $code); if ($this->swatchHelper->isVisualSwatch($attribute) || $this->swatchHelper->isTextSwatch($attribute)) { $attributeCodes[] = $code; } } $result->addAttributeToSelect($attributeCodes); return $result; }
/** * * @return array */ public function getProductAttributes() { if (is_null($this->prodAttributes)) { $this->prodAttributes = $this->eavConfig->getEntityAttributeCodes(\Magento\Catalog\Model\Product::ENTITY); } return $this->prodAttributes; }
/** * Get filters from request * * @param array $request * @return array */ protected function getFilterArray(array $request) { $filterArray = []; $attributeCodes = $this->eavConfig->getEntityAttributeCodes(\Magento\Catalog\Model\Product::ENTITY); foreach ($request as $code => $value) { if (in_array($code, $attributeCodes)) { $attribute = $this->eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $code); if ($attribute->getId() && $this->canReplaceImageWithSwatch($attribute)) { $filterArray[$code] = $value; } } } return $filterArray; }
/** * {@inheritdoc} */ public function getAllAttributeSetMetadata($entityType, $attributeSetId = 0, $storeId = null) { if (null === $storeId) { $storeId = $this->_storeManager->getStore()->getId(); } $object = new \Magento\Framework\Object(['store_id' => $storeId, 'attribute_set_id' => $attributeSetId]); $attributeCodes = $this->_eavConfig->getEntityAttributeCodes($entityType, $object); $attributesMetadata = []; foreach ($attributeCodes as $attributeCode) { try { $attributesMetadata[] = $this->getAttributeMetadata($entityType, $attributeCode); } catch (NoSuchEntityException $e) { //If no such entity, skip } } return $attributesMetadata; }
/** * Retrieve configuration for all attributes * * @param null|\Magento\Framework\Object $object * @return $this */ public function loadAllAttributes($object = null) { $attributeCodes = $this->_eavConfig->getEntityAttributeCodes($this->getEntityType(), $object); /** * Check and init default attributes */ $defaultAttributes = $this->getDefaultAttributes(); foreach ($defaultAttributes as $attributeCode) { $attributeIndex = array_search($attributeCode, $attributeCodes); if ($attributeIndex !== false) { $this->getAttribute($attributeCodes[$attributeIndex]); unset($attributeCodes[$attributeIndex]); } else { $this->addAttribute($this->_getDefaultAttribute($attributeCode)); } } foreach ($attributeCodes as $code) { $this->getAttribute($code); } return $this; }
/** * Remove don't applicable attributes data * * @param \Magento\Catalog\Model\Product $product * @return void */ protected function _removeNotApplicableAttributes($product) { $entityType = $product->getResource()->getEntityType(); foreach ($this->_eavConfig->getEntityAttributeCodes($entityType, $product) as $attributeCode) { $attribute = $this->_eavConfig->getAttribute($entityType, $attributeCode); $applyTo = $attribute->getApplyTo(); if (is_array($applyTo) && count($applyTo) > 0 && !in_array($product->getTypeId(), $applyTo)) { $product->unsetData($attribute->getAttributeCode()); } } }
/** * Process product attributes on collection. * * @param $collection * * @return mixed */ public function _processProductAttributes($collection) { //if no product attribute or collection empty return collection if (empty($this->productAttribute) or !$collection->getSize()) { return $collection; } foreach ($collection as $collectionItem) { $items = $collectionItem->getAllItems(); foreach ($items as $item) { //loaded product $product = $item->getProduct(); //attributes array from loaded product $attributes = $this->config->getEntityAttributeCodes(\Magento\Catalog\Model\Product::ENTITY, $product); foreach ($this->productAttribute as $productAttribute) { $attribute = $productAttribute['attribute']; $cond = $productAttribute['conditions']; $value = $productAttribute['cvalue']; if ($cond == 'null') { if ($value == '0') { $cond = 'neq'; } elseif ($value == '1') { $cond = 'eq'; } $value = ''; } //if attribute is in product's attributes array if (in_array($attribute, $attributes)) { $attr = $this->config->getAttribute('catalog_product', $attribute); //frontend type $frontType = $attr->getFrontend()->getInputType(); //if type is select if ($frontType == 'select' or $frontType == 'multiselect') { $attributeValue = $product->getAttributeText($attribute); //evaluate conditions on values. if true then unset item from collection if ($this->_evaluate($value, $cond, $attributeValue)) { $collection->removeItemByKey($collectionItem->getId()); continue 3; } } else { $getter = 'get'; $exploded = explode('_', $attribute); foreach ($exploded as $one) { $getter .= ucfirst($one); } //@codingStandardsIgnoreStart $attributeValue = call_user_func([$product, $getter]); //@codingStandardsIgnoreEnd //if retrieved value is an array then loop through all array values. // example can be categories if (is_array($attributeValue)) { foreach ($attributeValue as $attrValue) { //evaluate conditions on values. if true then unset item from collection if ($this->_evaluate($value, $cond, $attrValue)) { $collection->removeItemByKey($collectionItem->getId()); continue 3; } } } else { //evaluate conditions on values. if true then unset item from collection if ($this->_evaluate($value, $cond, $attributeValue)) { $collection->removeItemByKey($collectionItem->getId()); continue 3; } } } } } } } return $collection; }