123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\ConfigurableProduct\Model\Plugin;
- use Magento\Catalog\Model\ProductFactory;
- use Magento\Catalog\Api\Data\ProductInterface;
- use Magento\Framework\Exception\InputException;
- use Magento\Catalog\Api\ProductRepositoryInterface;
- use Magento\Framework\Exception\CouldNotSaveException;
- use Magento\ConfigurableProduct\Api\Data\OptionInterface;
- use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
- use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
- class ProductRepositorySave
- {
- /**
- * @var ProductAttributeRepositoryInterface
- */
- private $productAttributeRepository;
- /**
- * @var ProductFactory
- */
- private $productFactory;
- /**
- * @param ProductAttributeRepositoryInterface $productAttributeRepository
- * @param ProductFactory $productFactory
- */
- public function __construct(
- ProductAttributeRepositoryInterface $productAttributeRepository,
- ProductFactory $productFactory
- ) {
- $this->productAttributeRepository = $productAttributeRepository;
- $this->productFactory = $productFactory;
- }
- /**
- * Validate product links and reset configurable attributes to configurable product
- *
- * @param ProductRepositoryInterface $subject
- * @param ProductInterface $result
- * @param ProductInterface $product
- * @param bool $saveOptions
- * @return ProductInterface
- * @throws CouldNotSaveException
- * @throws InputException
- *
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function afterSave(
- ProductRepositoryInterface $subject,
- ProductInterface $result,
- ProductInterface $product,
- $saveOptions = false
- ) {
- if ($product->getTypeId() !== Configurable::TYPE_CODE) {
- return $result;
- }
- $extensionAttributes = $result->getExtensionAttributes();
- if ($extensionAttributes === null) {
- return $result;
- }
- $configurableLinks = (array) $extensionAttributes->getConfigurableProductLinks();
- $configurableOptions = (array) $extensionAttributes->getConfigurableProductOptions();
- if (empty($configurableLinks) && empty($configurableOptions)) {
- return $result;
- }
- $attributeCodes = [];
- /** @var OptionInterface $configurableOption */
- foreach ($configurableOptions as $configurableOption) {
- $eavAttribute = $this->productAttributeRepository->get($configurableOption->getAttributeId());
- $attributeCode = $eavAttribute->getAttributeCode();
- $attributeCodes[] = $attributeCode;
- }
- $this->validateProductLinks($attributeCodes, $configurableLinks);
- $result->getTypeInstance()->resetConfigurableAttributes($product);
- return $result;
- }
- /**
- * @param array $attributeCodes
- * @param array $linkIds
- * @return $this
- * @throws InputException
- */
- private function validateProductLinks(array $attributeCodes, array $linkIds)
- {
- $valueMap = [];
- foreach ($linkIds as $productId) {
- $variation = $this->productFactory->create()->load($productId);
- $valueKey = '';
- foreach ($attributeCodes as $attributeCode) {
- if (!$variation->getData($attributeCode)) {
- throw new InputException(
- __('Product with id "%1" does not contain required attribute "%2".', $productId, $attributeCode)
- );
- }
- $valueKey = $valueKey . $attributeCode . ':' . $variation->getData($attributeCode) . ';';
- }
- if (isset($valueMap[$valueKey])) {
- throw new InputException(
- __(
- 'Products "%1" and "%2" have the same set of attribute values.',
- $productId,
- $valueMap[$valueKey]
- )
- );
- }
- $valueMap[$valueKey] = $productId;
- }
- }
- }
|