1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* List of suggested attributes
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ConfigurableProduct\Model;
class SuggestedAttributeList
{
/**
* @var ConfigurableAttributeHandler
*/
protected $configurableAttributeHandler;
/**
* Catalog resource helper
*
* @var \Magento\Catalog\Model\ResourceModel\Helper
*/
protected $_resourceHelper;
/**
* @param ConfigurableAttributeHandler $configurableAttributeHandler
* @param \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper
*/
public function __construct(
\Magento\ConfigurableProduct\Model\ConfigurableAttributeHandler $configurableAttributeHandler,
\Magento\Catalog\Model\ResourceModel\Helper $resourceHelper
) {
$this->configurableAttributeHandler = $configurableAttributeHandler;
$this->_resourceHelper = $resourceHelper;
}
/**
* Retrieve list of attributes with admin store label containing $labelPart
*
* @param string $labelPart
* @return \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection
*/
public function getSuggestedAttributes($labelPart)
{
$escapedLabelPart = $this->_resourceHelper->addLikeEscape($labelPart, ['position' => 'any']);
/** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */
$collection = $this->configurableAttributeHandler->getApplicableAttributes()->addFieldToFilter(
'frontend_label',
['like' => $escapedLabelPart]
);
$result = [];
foreach ($collection->getItems() as $id => $attribute) {
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
if ($this->configurableAttributeHandler->isAttributeApplicable($attribute)) {
$result[$id] = [
'id' => $attribute->getId(),
'label' => $attribute->getFrontendLabel(),
'code' => $attribute->getAttributeCode(),
'options' => $attribute->getSource()->getAllOptions(false),
];
}
}
return $result;
}
}