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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ConfigurableProduct\Model;
use Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilderInterface;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\Framework\App\ScopeResolverInterface;
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute;
use Magento\Framework\DB\Select;
/**
* Provider for retrieving configurable options.
*/
class AttributeOptionProvider implements AttributeOptionProviderInterface
{
/**
* @var ScopeResolverInterface
*/
private $scopeResolver;
/**
* @var Attribute
*/
private $attributeResource;
/**
* @var OptionSelectBuilderInterface
*/
private $optionSelectBuilder;
/**
* @param Attribute $attributeResource
* @param ScopeResolverInterface $scopeResolver,
* @param OptionSelectBuilderInterface $optionSelectBuilder
*/
public function __construct(
Attribute $attributeResource,
ScopeResolverInterface $scopeResolver,
OptionSelectBuilderInterface $optionSelectBuilder
) {
$this->attributeResource = $attributeResource;
$this->scopeResolver = $scopeResolver;
$this->optionSelectBuilder = $optionSelectBuilder;
}
/**
* {@inheritdoc}
*/
public function getAttributeOptions(AbstractAttribute $superAttribute, $productId)
{
$scope = $this->scopeResolver->getScope();
$select = $this->optionSelectBuilder->getSelect($superAttribute, $productId, $scope);
$data = $this->attributeResource->getConnection()->fetchAll($select);
if ($superAttribute->getSourceModel()) {
$options = $superAttribute->getSource()->getAllOptions(false);
$optionLabels = [];
foreach ($options as $option) {
$optionLabels[$option['value']] = $option['label'];
}
foreach ($data as $key => $value) {
$optionText = isset($optionLabels[$value['value_index']])
? $optionLabels[$value['value_index']]
: false;
$data[$key]['default_title'] = $optionText;
$data[$key]['option_title'] = $optionText;
}
}
return $data;
}
}