|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\ConfigurableProduct\Model\Plugin; |
| 7 | + |
| 8 | +use Magento\Catalog\Model\ProductFactory; |
| 9 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 10 | +use Magento\Framework\Exception\InputException; |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Framework\Exception\CouldNotSaveException; |
| 13 | +use Magento\ConfigurableProduct\Api\Data\OptionInterface; |
| 14 | +use Magento\Catalog\Api\ProductAttributeRepositoryInterface; |
| 15 | +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class AroundProductRepositorySave |
| 19 | + */ |
| 20 | +class AroundProductRepositorySave |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var ProductAttributeRepositoryInterface |
| 24 | + */ |
| 25 | + private $productAttributeRepository; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var ProductFactory |
| 29 | + */ |
| 30 | + private $productFactory; |
| 31 | + |
| 32 | + /** |
| 33 | + * @param ProductAttributeRepositoryInterface $productAttributeRepository |
| 34 | + * @param ProductFactory $productFactory |
| 35 | + */ |
| 36 | + public function __construct( |
| 37 | + ProductAttributeRepositoryInterface $productAttributeRepository, |
| 38 | + ProductFactory $productFactory |
| 39 | + ) { |
| 40 | + $this->productAttributeRepository = $productAttributeRepository; |
| 41 | + $this->productFactory = $productFactory; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @param ProductRepositoryInterface $subject |
| 46 | + * @param callable $proceed |
| 47 | + * @param ProductInterface $product |
| 48 | + * @param bool $saveOptions |
| 49 | + * @return ProductInterface |
| 50 | + * @throws CouldNotSaveException |
| 51 | + * @throws InputException |
| 52 | + * |
| 53 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 54 | + */ |
| 55 | + public function aroundSave( |
| 56 | + ProductRepositoryInterface $subject, |
| 57 | + \Closure $proceed, |
| 58 | + ProductInterface $product, |
| 59 | + $saveOptions = false |
| 60 | + ) { |
| 61 | + /** @var ProductInterface $result */ |
| 62 | + $result = $proceed($product, $saveOptions); |
| 63 | + if ($product->getTypeId() !== Configurable::TYPE_CODE) { |
| 64 | + return $result; |
| 65 | + } |
| 66 | + |
| 67 | + $extensionAttributes = $product->getExtensionAttributes(); |
| 68 | + if ($extensionAttributes === null) { |
| 69 | + return $result; |
| 70 | + } |
| 71 | + |
| 72 | + $configurableLinks = (array) $extensionAttributes->getConfigurableProductLinks(); |
| 73 | + $configurableOptions = (array) $extensionAttributes->getConfigurableProductOptions(); |
| 74 | + |
| 75 | + if (empty($configurableLinks) && empty($extensionAttributes)) { |
| 76 | + return $result; |
| 77 | + } |
| 78 | + |
| 79 | + $attributeCodes = []; |
| 80 | + /** @var OptionInterface $configurableOption */ |
| 81 | + foreach ($configurableOptions as $configurableOption) { |
| 82 | + $eavAttribute = $this->productAttributeRepository->get($configurableOption->getAttributeId()); |
| 83 | + $attributeCode = $eavAttribute->getAttributeCode(); |
| 84 | + $attributeCodes[] = $attributeCode; |
| 85 | + } |
| 86 | + $this->validateProductLinks($attributeCodes, $configurableLinks); |
| 87 | + |
| 88 | + return $subject->get($result->getSku(), false, $result->getStoreId(), true); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param array $attributeCodes |
| 93 | + * @param array $linkIds |
| 94 | + * @return $this |
| 95 | + * @throws InputException |
| 96 | + */ |
| 97 | + private function validateProductLinks(array $attributeCodes, array $linkIds) |
| 98 | + { |
| 99 | + $valueMap = []; |
| 100 | + if (empty($attributeCodes) && !empty($linkIds)) { |
| 101 | + throw new InputException( |
| 102 | + __('The configurable product does not have any variation attribute.') |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + foreach ($linkIds as $productId) { |
| 107 | + $variation = $this->productFactory->create()->load($productId); |
| 108 | + if (!$variation->getId()) { |
| 109 | + throw new InputException(__('Product with id "%1" does not exist.', $productId)); |
| 110 | + } |
| 111 | + $valueKey = ''; |
| 112 | + foreach ($attributeCodes as $attributeCode) { |
| 113 | + if (!$variation->getData($attributeCode)) { |
| 114 | + throw new InputException( |
| 115 | + __('Product with id "%1" does not contain required attribute "%2".', $productId, $attributeCode) |
| 116 | + ); |
| 117 | + } |
| 118 | + $valueKey = $valueKey . $attributeCode . ':' . $variation->getData($attributeCode) . ';'; |
| 119 | + } |
| 120 | + if (isset($valueMap[$valueKey])) { |
| 121 | + throw new InputException( |
| 122 | + __('Products "%1" and %2 have the same set of attribute values.', $productId, $valueMap[$valueKey]) |
| 123 | + ); |
| 124 | + } |
| 125 | + $valueMap[$valueKey] = $productId; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments