Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OptionRepository.php
Go to the documentation of this file.
1 <?php
9 
23 
28 {
32  protected $productRepository;
33 
38 
42  protected $configurableType;
43 
47  protected $optionResource;
48 
52  protected $storeManager;
53 
58 
63 
67  private $configurableTypeResource;
68 
72  private $metadataPool;
73 
77  private $optionLoader;
78 
92  public function __construct(
94  \Magento\ConfigurableProduct\Api\Data\OptionValueInterfaceFactory $optionValueFactory,
97  \Magento\Store\Model\StoreManagerInterface $storeManager,
98  \Magento\Catalog\Api\ProductAttributeRepositoryInterface $productAttributeRepository,
100  \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurableTypeResource,
101  Loader $optionLoader
102  ) {
103  $this->productRepository = $productRepository;
104  $this->optionValueFactory = $optionValueFactory;
105  $this->configurableType = $configurableType;
106  $this->optionResource = $optionResource;
107  $this->storeManager = $storeManager;
108  $this->productAttributeRepository = $productAttributeRepository;
109  $this->configurableAttributeFactory = $configurableAttributeFactory;
110  $this->configurableTypeResource = $configurableTypeResource;
111  $this->optionLoader = $optionLoader;
112  }
113 
117  public function get($sku, $id)
118  {
119  $product = $this->getProduct($sku);
120 
121  $options = $this->optionLoader->load($product);
122  foreach ($options as $option) {
123  if ($option->getId() == $id) {
124  return $option;
125  }
126  }
127 
128  throw new NoSuchEntityException(
129  __('The "%1" entity that was requested doesn\'t exist. Verify the entity and try again.', $id)
130  );
131  }
132 
136  public function getList($sku)
137  {
138  $product = $this->getProduct($sku);
139 
140  return (array) $this->optionLoader->load($product);
141  }
142 
146  public function delete(OptionInterface $option)
147  {
148  $entityId = $this->configurableTypeResource->getEntityIdByAttribute($option);
149  $product = $this->getProductById($entityId);
150 
151  try {
152  $this->configurableTypeResource->saveProducts($product, []);
153  $this->configurableType->resetConfigurableAttributes($product);
154  } catch (\Exception $exception) {
155  throw new StateException(
156  __('The variations from the "%1" product can\'t be deleted.', $entityId)
157  );
158  }
159  try {
160  $this->optionResource->delete($option);
161  } catch (\Exception $exception) {
162  throw new StateException(
163  __('The option with "%1" ID can\'t be deleted.', $option->getId())
164  );
165  }
166  return true;
167  }
168 
172  public function deleteById($sku, $id)
173  {
174  $product = $this->getProduct($sku);
175  $attributeCollection = $this->configurableType->getConfigurableAttributeCollection($product);
177  $option = $attributeCollection->getItemById($id);
178  if ($option === null) {
179  throw new NoSuchEntityException(
180  __("The option that was requested doesn't exist. Verify the entity and try again.")
181  );
182  }
183  return $this->delete($option);
184  }
185 
190  public function save($sku, OptionInterface $option)
191  {
192  $metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class);
193  if ($option->getId()) {
195  $product = $this->getProduct($sku);
196  $data = $option->getData();
197  $option->load($option->getId());
198  $option->setData(array_replace_recursive($option->getData(), $data));
199  if (!$option->getId() || $option->getProductId() != $product->getData($metadata->getLinkField())) {
200  throw new NoSuchEntityException(
201  __(
202  'Option with id "%1" not found',
203  $option->getId()
204  )
205  );
206  }
207  } else {
209  $product = $this->productRepository->get($sku);
210  $this->validateNewOptionData($option);
211  $allowedTypes = [ProductType::TYPE_SIMPLE, ProductType::TYPE_VIRTUAL, ConfigurableType::TYPE_CODE];
212  if (!in_array($product->getTypeId(), $allowedTypes)) {
213  throw new \InvalidArgumentException('Incompatible product type');
214  }
215  $option->setProductId($product->getData($metadata->getLinkField()));
216  }
217 
218  try {
219  $option->save();
220  } catch (\Exception $e) {
221  throw new CouldNotSaveException(__('An error occurred while saving the option. Please try to save again.'));
222  }
223 
224  if (!$option->getId()) {
225  throw new CouldNotSaveException(__('An error occurred while saving the option. Please try to save again.'));
226  }
227  return $option->getId();
228  }
229 
237  private function getProduct($sku)
238  {
239  $product = $this->productRepository->get($sku);
240  if (\Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE !== $product->getTypeId()) {
241  throw new InputException(
242  __('This is implemented for the "%1" configurable product only.', $sku)
243  );
244  }
245  return $product;
246  }
247 
255  private function getProductById($id)
256  {
257  $product = $this->productRepository->getById($id);
258  if (\Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE !== $product->getTypeId()) {
259  throw new InputException(
260  __('This is implemented for the "%1" configurable product only.', $id)
261  );
262  }
263  return $product;
264  }
265 
275  {
276  $inputException = new InputException();
277  if (!$option->getAttributeId()) {
278  $inputException->addError(__('Option attribute ID is not specified.'));
279  }
280  if (!$option->getLabel()) {
281  $inputException->addError(__('Option label is not specified.'));
282  }
283  if (!$option->getValues()) {
284  $inputException->addError(__('Option values are not specified.'));
285  } else {
286  foreach ($option->getValues() as $optionValue) {
287  if (null === $optionValue->getValueIndex()) {
288  $inputException->addError(__('Value index is not specified for an option.'));
289  }
290  }
291  }
292  if ($inputException->wasErrorAdded()) {
293  throw $inputException;
294  }
295  }
296 
301  private function getMetadataPool()
302  {
303  if (!$this->metadataPool) {
304  $this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
305  }
306  return $this->metadataPool;
307  }
308 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
save($sku, \Magento\ConfigurableProduct\Api\Data\OptionInterface $option)
__construct(\Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\ConfigurableProduct\Api\Data\OptionValueInterfaceFactory $optionValueFactory, \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurableType, \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute $optionResource, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Api\ProductAttributeRepositoryInterface $productAttributeRepository, \Magento\ConfigurableProduct\Model\Product\Type\Configurable\AttributeFactory $configurableAttributeFactory, \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurableTypeResource, Loader $optionLoader)