/** * Load counfigurable attribute by product and product's attribute * * @param \Magento\Catalog\Model\Product $product * @param \Magento\Catalog\Model\Resource\Eav\Attribute $attribute * @return void */ public function loadByProductAndAttribute($product, $attribute) { $id = $this->_getResource()->getIdByProductIdAndAttributeId($this, $product->getId(), $attribute->getId()); if ($id) { $this->load($id); } }
/** * Return Product Attribute Store Label * Set attribute name like frontend lable for custom attributes (which wasn't defined by Google) * * @param \Magento\Catalog\Model\Resource\Eav\Attribute $attribute * @param int $storeId Store View Id * @return string Attribute Store View Label or Attribute code */ public function getAttributeLabel($attribute, $storeId) { $attributeId = $attribute->getId(); $frontendLabel = $attribute->getFrontend()->getLabel(); if (is_array($frontendLabel)) { $frontendLabel = array_shift($frontendLabel); } if (!isset($this->_attributeLabels[$attributeId])) { $this->_attributeLabels[$attributeId] = $attribute->getStoreLabels(); } if (isset($this->_attributeLabels[$attributeId][$storeId])) { return $this->_attributeLabels[$attributeId][$storeId]; } elseif (!empty($frontendLabel)) { return $frontendLabel; } else { return $attribute->getAttributeCode(); } }
/** * @param \Magento\Catalog\Model\Resource\Eav\Attribute $attribute * @param array $data * @return array */ protected function getOption($attribute, $data) { $result = []; $data['option'] = explode("\n", $data['option']); /** @var \Magento\Eav\Model\Resource\Entity\Attribute\Option\Collection $options */ $options = $this->attrOptionCollectionFactory->create()->setAttributeFilter($attribute->getId())->setPositionOrder('asc', true)->load(); foreach ($data['option'] as $value) { if (!$options->getItemByColumnValue('value', $value)) { $result[] = $value; } } return $result ? $this->convertOption($result) : $result; }
/** * @param \Magento\Catalog\Model\Resource\Eav\Attribute $attribute * @param \Magento\Catalog\Model\Resource\Product\Collection $collection * @return $this */ protected function addGlobalAttribute(\Magento\Catalog\Model\Resource\Eav\Attribute $attribute, \Magento\Catalog\Model\Resource\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; return $this; }