Skip to content

Commit 52ee1b9

Browse files
Merge remote-tracking branch 'origin/MAGETWO-61133' into Borg-PR-11-22
2 parents 638e1f5 + 916ce7c commit 52ee1b9

File tree

9 files changed

+738
-401
lines changed

9 files changed

+738
-401
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\ConfigurableProduct\Model;
8+
9+
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
10+
use Magento\Framework\App\ScopeResolverInterface;
11+
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute;
12+
use Magento\Framework\App\ScopeInterface;
13+
use Magento\Framework\DB\Select;
14+
use Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionProvider;
15+
16+
class AttributeOptionProvider implements AttributeOptionProviderInterface
17+
{
18+
/**
19+
* @var ScopeResolverInterface
20+
*/
21+
private $scopeResolver;
22+
23+
/**
24+
* @var Attribute
25+
*/
26+
private $attributeResource;
27+
28+
/**
29+
* @var OptionProvider
30+
*/
31+
private $attributeOptionProvider;
32+
33+
/**
34+
* @param Attribute $attributeResource
35+
* @param OptionProvider $attributeOptionProvider ,
36+
* @param ScopeResolverInterface $scopeResolver
37+
*/
38+
public function __construct(
39+
Attribute $attributeResource,
40+
OptionProvider $attributeOptionProvider,
41+
ScopeResolverInterface $scopeResolver
42+
) {
43+
$this->attributeResource = $attributeResource;
44+
$this->attributeOptionProvider = $attributeOptionProvider;
45+
$this->scopeResolver = $scopeResolver;
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function getAttributeOptions(AbstractAttribute $superAttribute, $productId)
52+
{
53+
$scope = $this->scopeResolver->getScope();
54+
$select = $this->getAttributeOptionsSelect($superAttribute, $productId, $scope);
55+
56+
$data = $this->attributeResource->getConnection()->fetchAll($select);
57+
if ($superAttribute->getSourceModel()) {
58+
foreach ($data as $key => $value) {
59+
$optionText = $superAttribute->getSource()->getOptionText($value['value_index']);
60+
$data[$key]['default_title'] = $optionText;
61+
$data[$key]['option_title'] = $optionText;
62+
}
63+
}
64+
65+
return $data;
66+
}
67+
68+
/**
69+
* Get load options for attribute select
70+
*
71+
* @param AbstractAttribute $superAttribute
72+
* @param int $productId
73+
* @param ScopeInterface $scope
74+
* @return Select
75+
*/
76+
private function getAttributeOptionsSelect(AbstractAttribute $superAttribute, $productId, ScopeInterface $scope)
77+
{
78+
$select = $this->attributeResource->getConnection()->select()->from(
79+
['super_attribute' => $this->attributeResource->getTable('catalog_product_super_attribute')],
80+
[
81+
'sku' => 'entity.sku',
82+
'product_id' => 'product_entity.entity_id',
83+
'attribute_code' => 'attribute.attribute_code',
84+
'value_index' => 'entity_value.value',
85+
'super_attribute_label' => 'attribute_label.value',
86+
]
87+
)->joinInner(
88+
['product_entity' => $this->attributeResource->getTable('catalog_product_entity')],
89+
"product_entity.{$this->attributeOptionProvider->getProductEntityLinkField()} = super_attribute.product_id",
90+
[]
91+
)->joinInner(
92+
['product_link' => $this->attributeResource->getTable('catalog_product_super_link')],
93+
'product_link.parent_id = super_attribute.product_id',
94+
[]
95+
)->joinInner(
96+
['attribute' => $this->attributeResource->getTable('eav_attribute')],
97+
'attribute.attribute_id = super_attribute.attribute_id',
98+
[]
99+
)->joinInner(
100+
['entity' => $this->attributeResource->getTable('catalog_product_entity')],
101+
'entity.entity_id = product_link.product_id',
102+
[]
103+
)->joinInner(
104+
['entity_value' => $superAttribute->getBackendTable()],
105+
implode(
106+
' AND ',
107+
[
108+
'entity_value.attribute_id = super_attribute.attribute_id',
109+
'entity_value.store_id = 0',
110+
"entity_value.{$this->attributeOptionProvider->getProductEntityLinkField()} = "
111+
. "entity.{$this->attributeOptionProvider->getProductEntityLinkField()}",
112+
]
113+
),
114+
[]
115+
)->joinLeft(
116+
['attribute_label' => $this->attributeResource->getTable('catalog_product_super_attribute_label')],
117+
implode(
118+
' AND ',
119+
[
120+
'super_attribute.product_super_attribute_id = attribute_label.product_super_attribute_id',
121+
'attribute_label.store_id = ' . \Magento\Store\Model\Store::DEFAULT_STORE_ID,
122+
]
123+
),
124+
[]
125+
)->where(
126+
'super_attribute.product_id = ?',
127+
$productId
128+
)->where(
129+
'attribute.attribute_id = ?',
130+
$superAttribute->getAttributeId()
131+
);
132+
133+
if (!$superAttribute->getSourceModel()) {
134+
$select->columns(
135+
[
136+
'option_title' => $this->attributeResource->getConnection()->getIfNullSql(
137+
'option_value.value',
138+
'default_option_value.value'
139+
),
140+
'default_title' => 'default_option_value.value',
141+
]
142+
)->joinLeft(
143+
['option_value' => $this->attributeResource->getTable('eav_attribute_option_value')],
144+
implode(
145+
' AND ',
146+
[
147+
'option_value.option_id = entity_value.value',
148+
'option_value.store_id = ' . $scope->getId(),
149+
]
150+
),
151+
[]
152+
)->joinLeft(
153+
['default_option_value' => $this->attributeResource->getTable('eav_attribute_option_value')],
154+
implode(
155+
' AND ',
156+
[
157+
'default_option_value.option_id = entity_value.value',
158+
'default_option_value.store_id = ' . \Magento\Store\Model\Store::DEFAULT_STORE_ID,
159+
]
160+
),
161+
[]
162+
);
163+
}
164+
165+
return $select;
166+
}
167+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\ConfigurableProduct\Model;
7+
8+
/**
9+
* Interface to retrieve options for attribute
10+
* @api
11+
* @since 100.2.0
12+
*/
13+
interface AttributeOptionProviderInterface
14+
{
15+
/**
16+
* Retrieve options for attribute
17+
*
18+
* @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
19+
* @param int $productId
20+
* @return array
21+
* @since 100.2.0
22+
*/
23+
public function getAttributeOptions(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute, $productId);
24+
}

app/code/Magento/ConfigurableProduct/Model/OptionRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public function validateNewOptionData(OptionInterface $option)
279279
$inputException->addError(__('Option values are not specified.'));
280280
} else {
281281
foreach ($option->getValues() as $optionValue) {
282-
if (!$optionValue->getValueIndex()) {
282+
if (null === $optionValue->getValueIndex()) {
283283
$inputException->addError(__('Value index is not specified for an option.'));
284284
}
285285
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\ConfigurableProduct\Model\ResourceModel\Attribute;
7+
8+
use Magento\Framework\EntityManager\MetadataPool;
9+
use Magento\Catalog\Api\Data\ProductInterface;
10+
11+
class OptionProvider
12+
{
13+
/**
14+
* Product metadata pool
15+
*
16+
* @var MetadataPool
17+
*/
18+
private $metadataPool;
19+
20+
/**
21+
* @param MetadataPool $metadataPool
22+
*/
23+
public function __construct(
24+
MetadataPool $metadataPool
25+
) {
26+
$this->metadataPool = $metadataPool;
27+
}
28+
29+
/**
30+
* Get product entity link field
31+
*
32+
* @return string
33+
*/
34+
public function getProductEntityLinkField()
35+
{
36+
return $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
37+
}
38+
}

0 commit comments

Comments
 (0)