67 private $configurableTypeResource;
72 private $metadataPool;
77 private $optionLoader;
110 $this->configurableTypeResource = $configurableTypeResource;
111 $this->optionLoader = $optionLoader;
117 public function get($sku,
$id)
129 __(
'The "%1" entity that was requested doesn\'t exist. Verify the entity and try again.',
$id)
140 return (array) $this->optionLoader->load(
$product);
148 $entityId = $this->configurableTypeResource->getEntityIdByAttribute(
$option);
149 $product = $this->getProductById($entityId);
152 $this->configurableTypeResource->saveProducts(
$product, []);
153 $this->configurableType->resetConfigurableAttributes(
$product);
156 __(
'The variations from the "%1" product can\'t be deleted.', $entityId)
160 $this->optionResource->delete(
$option);
163 __(
'The option with "%1" ID can\'t be deleted.',
$option->getId())
180 __(
"The option that was requested doesn't exist. Verify the entity and try again.")
190 public function save($sku, OptionInterface
$option)
192 $metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class);
200 throw new NoSuchEntityException(
202 'Option with id "%1" not found',
209 $product = $this->productRepository->get($sku);
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');
220 }
catch (\Exception $e) {
221 throw new CouldNotSaveException(
__(
'An error occurred while saving the option. Please try to save again.'));
225 throw new CouldNotSaveException(
__(
'An error occurred while saving the option. Please try to save again.'));
237 private function getProduct($sku)
239 $product = $this->productRepository->get($sku);
241 throw new InputException(
242 __(
'This is implemented for the "%1" configurable product only.', $sku)
255 private function getProductById(
$id)
259 throw new InputException(
260 __(
'This is implemented for the "%1" configurable product only.',
$id)
277 if (!
$option->getAttributeId()) {
278 $inputException->addError(
__(
'Option attribute ID is not specified.'));
281 $inputException->addError(
__(
'Option label is not specified.'));
284 $inputException->addError(
__(
'Option values are not specified.'));
288 $inputException->addError(
__(
'Value index is not specified for an option.'));
292 if ($inputException->wasErrorAdded()) {
293 throw $inputException;
301 private function getMetadataPool()
303 if (!$this->metadataPool) {
306 return $this->metadataPool;
save($sku, \Magento\ConfigurableProduct\Api\Data\OptionInterface $option)
$configurableAttributeFactory
$productAttributeRepository
validateNewOptionData(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)