Skip to content

Commit

Permalink
Fix bug 26449: Set null for configurable options of parent product wh…
Browse files Browse the repository at this point in the history
…enever it's saved

- Remove static function
  • Loading branch information
tna274 committed Mar 19, 2020
1 parent 1071acf commit 3b6b038
Showing 1 changed file with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\ConfigurableProduct\Plugin\Model\ResourceModel;

use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Framework\Indexer\ActionInterface;
use Magento\ConfigurableProduct\Api\Data\OptionInterface;
use Magento\Catalog\Api\Data\ProductAttributeInterface;

/**
* Plugin product resource model
Expand All @@ -26,18 +28,42 @@ class Product
*/
private $productIndexer;

/**
* @var ProductAttributeRepositoryInterface
*/
private $productAttributeRepository;

/**
* @var \Magento\Framework\Api\SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

/**
* @var \Magento\Framework\Api\FilterBuilder
*/
private $filterBuilder;

/**
* Initialize Product dependencies.
*
* @param Configurable $configurable
* @param ActionInterface $productIndexer
* @param ProductAttributeRepositoryInterface $productAttributeRepository
* @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
* @param \Magento\Framework\Api\FilterBuilder $filterBuilder
*/
public function __construct(
Configurable $configurable,
ActionInterface $productIndexer
ActionInterface $productIndexer,
ProductAttributeRepositoryInterface $productAttributeRepository,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
\Magento\Framework\Api\FilterBuilder $filterBuilder
) {
$this->configurable = $configurable;
$this->productIndexer = $productIndexer;
$this->productAttributeRepository = $productAttributeRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->filterBuilder = $filterBuilder;
}

/**
Expand Down Expand Up @@ -72,13 +98,23 @@ private function resetConfigurableOptionsData($object)
{
$extensionAttribute = $object->getExtensionAttributes();
if ($extensionAttribute && $extensionAttribute->getConfigurableProductOptions()) {
/** @var ProductAttributeRepositoryInterface $productAttributeRepository */
$productAttributeRepository = \Magento\Framework\App\ObjectManager::getInstance()
->get(ProductAttributeRepositoryInterface::class);
$attributeIds = [];
/** @var OptionInterface $option */
foreach ($extensionAttribute->getConfigurableProductOptions() as $option) {
$eavAttribute = $productAttributeRepository->get($option->getAttributeId());
$object->setData($eavAttribute->getAttributeCode(), null);
$attributeIds[] = $option->getAttributeId();
}

$filter = $this->filterBuilder
->setField(ProductAttributeInterface::ATTRIBUTE_ID)
->setConditionType('in')
->setValue($attributeIds)
->create();
$this->searchCriteriaBuilder->addFilters([$filter]);
$searchCriteria = $this->searchCriteriaBuilder->create();
$optionAttributes = $this->productAttributeRepository->getList($searchCriteria)->getItems();

foreach ($optionAttributes as $optionAttribute) {
$object->setData($optionAttribute->getAttributeCode(), null);
}
}
}
Expand Down

0 comments on commit 3b6b038

Please sign in to comment.