/**
* Add all attributes and apply pricing logic to products collection
* to get correct values in different products lists.
* E.g. crosssells, upsells, new products, recently viewed
*
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
protected function _addProductAttributesAndPrices(
\Magento\Catalog\Model\ResourceModel\Product\Collection $collection
) {
return $collection
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addAttributeToSelect($this->_catalogConfig->getProductAttributes())
->addUrlRewrite();
}
{
$connection = $this->getConnection();
$storeLabelExpr = $connection->getCheckSql('al.value IS NOT NULL', 'al.value', 'main_table.frontend_label');
$select = $connection->select()->from(
['main_table' => $this->getTable('eav_attribute')]
)->join(
['additional_table' => $this->getTable('catalog_eav_attribute')],
'main_table.attribute_id = additional_table.attribute_id'
)->joinLeft(
['al' => $this->getTable('eav_attribute_label')],
'al.attribute_id = main_table.attribute_id AND al.store_id = ' . (int)$this->getStoreId(),
['store_label' => $storeLabelExpr]
)->where(
'main_table.entity_type_id = ?',
$this->getEntityTypeId()
)->where(
'additional_table.used_in_product_listing = ?',
1
);
return $connection->fetchAll($select);
The result of the $select->assemble()
:
SELECT
`main_table`.*
, `additional_table`.*
, IF(al.value IS NOT NULL, al.value, main_table.frontend_label) AS `store_label`
FROM
`eav_attribute` AS `main_table`
INNER JOIN
`catalog_eav_attribute` AS `additional_table`
ON
main_table.attribute_id = additional_table.attribute_id
LEFT JOIN
`eav_attribute_label` AS `al`
ON
al.attribute_id = main_table.attribute_id
AND
al.store_id = 1
WHERE
(main_table.entity_type_id = 4)
AND
(additional_table.used_in_product_listing = 1)
/**
* Import attributes data from external source
*
* @param string|Type $entityType
* @param array $attributes
* @return $this
*/
public function importAttributesData($entityType, array $attributes)
{
$entityType = $this->getEntityType($entityType);
foreach ($attributes as $attributeData) {
if (!$this->_validateAttributeData($attributeData)) {
continue;
}
/**
* Validate attribute data from import
*
* @param array $attributeData
* @return bool
*/
protected function _validateAttributeData($attributeData = null)
{
if (!is_array($attributeData)) {
return false;
}
$requiredKeys = ['attribute_id', 'attribute_code', 'entity_type_id', 'attribute_model'];
foreach ($requiredKeys as $key) {
if (!array_key_exists($key, $attributeData)) {
return false;
}
}
return true;
}
/**
* Add attribute to entities in collection
* If $attribute=='*' select all attributes
*
* @param array|string|integer|\Magento\Framework\App\Config\Element $attribute
* @param bool|string $joinType
* @return $this
*/
public function addAttributeToSelect($attribute, $joinType = false)
{
if ($this->isEnabledFlat()) {
/**
* Add attribute to entities in collection
*
* If $attribute == '*' select all attributes
*
* @param array|string|integer|\Magento\Framework\App\Config\Element $attribute
* @param bool|string $joinType flag for joining attribute
* @return $this
* @throws LocalizedException
*/
public function addAttributeToSelect($attribute, $joinType = false)
{
if (is_array($attribute)) {
foreach ($attribute as $a) {
$this->addAttributeToSelect($a, $joinType);
}
return $this;
}
} else {
if (isset($this->_joinAttributes[$attribute])) {
$attrInstance = $this->_joinAttributes[$attribute]['attribute'];
} else {
$attrInstance = $this->_eavConfig->getAttribute($this->getEntity()->getType(), $attribute);
}
if (empty($attrInstance)) {
throw new LocalizedException(__('Invalid attribute requested: %1', (string)$attribute));
}
$this->_selectAttributes[$attrInstance->getAttributeCode()] = $attrInstance->getId();
}
return $this;
}