-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
OptionManagement.php
155 lines (142 loc) · 4.35 KB
/
OptionManagement.php
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);
namespace Magento\Swatches\Plugin\Eav\Model\Entity\Attribute;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Model\Product\Attribute\OptionManagement as CatalogOptionManagement;
use Magento\Eav\Api\Data\AttributeInterface;
use Magento\Eav\Api\Data\AttributeOptionInterface;
use Magento\Eav\Model\AttributeRepository;
use Magento\Store\Model\Store;
use Magento\Swatches\Helper\Data;
/**
* OptionManagement Plugin
*/
class OptionManagement
{
/**
* @var AttributeRepository
*/
private $attributeRepository;
/**
* @var Data
*/
private $swatchHelper;
/**
* @param AttributeRepository $attributeRepository
* @param Data $swatchHelper
*/
public function __construct(
AttributeRepository $attributeRepository,
Data $swatchHelper
) {
$this->attributeRepository = $attributeRepository;
$this->swatchHelper = $swatchHelper;
}
/**
* Add swatch value to the attribute option
*
* @param CatalogOptionManagement $subject
* @param string $attributeCode
* @param AttributeOptionInterface $option
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeAdd(
CatalogOptionManagement $subject,
?string $attributeCode,
AttributeOptionInterface $option
) {
$attribute = $this->initAttribute($attributeCode);
if (!$attribute) {
return;
}
$optionId = $this->getNewOptionId($option);
$this->setSwatchAttributeOption($attribute, $option, $optionId);
}
/**
* Update swatch value of attribute option
*
* @param CatalogOptionManagement $subject
* @param string $attributeCode
* @param int $optionId
* @param AttributeOptionInterface $option
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeUpdate(
CatalogOptionManagement $subject,
$attributeCode,
$optionId,
AttributeOptionInterface $option
) {
$attribute = $this->initAttribute($attributeCode);
if (!$attribute) {
return;
}
$this->setSwatchAttributeOption($attribute, $option, (string)$optionId);
}
/**
* Set attribute swatch option
*
* @param AttributeInterface $attribute
* @param AttributeOptionInterface $option
* @param string $optionId
*/
private function setSwatchAttributeOption(
AttributeInterface $attribute,
AttributeOptionInterface $option,
string $optionId
): void {
$optionsValue = trim($option->getValue() ?: '');
if ($this->swatchHelper->isVisualSwatch($attribute)) {
$attribute->setData('swatchvisual', ['value' => [$optionId => $optionsValue]]);
} else {
$options = [];
$options['value'][$optionId][Store::DEFAULT_STORE_ID] = $optionsValue;
if (is_array($option->getStoreLabels())) {
foreach ($option->getStoreLabels() as $label) {
if (!isset($options['value'][$optionId][$label->getStoreId()])) {
$options['value'][$optionId][$label->getStoreId()] = null;
}
}
}
$attribute->setData('swatchtext', $options);
}
}
/**
* Init swatch attribute
*
* @param string $attributeCode
* @return bool|AttributeInterface
*/
private function initAttribute($attributeCode)
{
if (empty($attributeCode)) {
return false;
}
$attribute = $this->attributeRepository->get(
ProductAttributeInterface::ENTITY_TYPE_CODE,
$attributeCode
);
if (!$attribute || !$this->swatchHelper->isSwatchAttribute($attribute)) {
return false;
}
return $attribute;
}
/**
* Get option id to create new option
*
* @param AttributeOptionInterface $option
* @return string
*/
private function getNewOptionId(AttributeOptionInterface $option): string
{
$optionId = trim($option->getValue() ?: '');
if (empty($optionId)) {
$optionId = 'new_option';
}
return 'id_' . $optionId;
}
}