/** * @param object $entity * @param array $arguments * @return \Magento\Catalog\Api\Data\ProductInterface|object * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entity, $arguments = []) { $bundleProductOptions = $entity->getExtensionAttributes()->getBundleProductOptions(); if ($entity->getTypeId() !== 'bundle' || empty($bundleProductOptions)) { return $entity; } if (!$entity->getCopyFromView()) { /** @var \Magento\Catalog\Api\Data\ProductInterface $entity */ foreach ($this->optionRepository->getList($entity->getSku()) as $option) { $this->removeOptionLinks($entity->getSku(), $option); $this->optionRepository->delete($option); } $options = $bundleProductOptions ?: []; foreach ($options as $option) { $option->setOptionId(null); $this->optionRepository->save($entity, $option); } } else { //save only labels and not selections + product links $options = $bundleProductOptions ?: []; foreach ($options as $option) { $this->optionRepository->save($entity, $option); $entity->setCopyFromView(false); } } return $entity; }
/** * {@inheritdoc} */ public function save(\Magento\Bundle\Api\Data\OptionInterface $option) { $product = $this->productRepository->get($option->getSku(), true); if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) { throw new InputException(__('Only implemented for bundle product')); } return $this->optionRepository->save($product, $option); }
/** * @param \Magento\Catalog\Api\ProductRepositoryInterface $subject * @param callable $proceed * @param \Magento\Catalog\Api\Data\ProductInterface $product * @param bool $saveOptions * @return \Magento\Catalog\Api\Data\ProductInterface * @throws \Magento\Framework\Exception\CouldNotSaveException * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundSave(\Magento\Catalog\Api\ProductRepositoryInterface $subject, \Closure $proceed, \Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false) { /** @var \Magento\Catalog\Api\Data\ProductInterface $result */ $result = $proceed($product, $saveOptions); if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) { return $result; } /* @var \Magento\Bundle\Api\Data\OptionInterface[] $options */ $bundleProductOptions = $product->getExtensionAttributes()->getBundleProductOptions(); if (is_array($bundleProductOptions)) { foreach ($bundleProductOptions as $option) { $this->optionRepository->save($result, $option); } } return $result; }
/** * @param string $entityType * @param object $entity * @return object * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entityType, $entity) { $bundleProductOptions = $entity->getExtensionAttributes()->getBundleProductOptions(); if ($entity->getTypeId() !== 'bundle' || $bundleProductOptions === null) { return $entity; } /** @var \Magento\Catalog\Api\Data\ProductInterface $entity */ foreach ($this->optionRepository->getList($entity->getSku()) as $option) { $this->removeOptionLinks($entity->getSku(), $option); $this->optionRepository->delete($option); } $options = $entity->getExtensionAttributes()->getBundleProductOptions() ?: []; foreach ($options as $option) { $option->setOptionId(null); $this->optionRepository->save($entity, $option); } return $entity; }
/** * @param \Magento\Catalog\Api\ProductRepositoryInterface $subject * @param callable $proceed * @param \Magento\Catalog\Api\Data\ProductInterface $product * @param bool $saveOptions * @return \Magento\Catalog\Api\Data\ProductInterface * @throws \Magento\Framework\Exception\CouldNotSaveException * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundSave(\Magento\Catalog\Api\ProductRepositoryInterface $subject, \Closure $proceed, \Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false) { /** @var \Magento\Catalog\Api\Data\ProductInterface $result */ $result = $proceed($product, $saveOptions); if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) { return $result; } /* @var \Magento\Bundle\Api\Data\OptionInterface[] $options */ $extendedAttributes = $product->getExtensionAttributes(); if ($extendedAttributes === null) { return $result; } $bundleProductOptions = $extendedAttributes->getBundleProductOptions(); if ($bundleProductOptions == null) { return $result; } /** @var \Magento\Bundle\Api\Data\OptionInterface[] $bundleProductOptions */ $existingOptions = $this->optionRepository->getList($product->getSku()); $existingOptionsMap = []; foreach ($existingOptions as $existingOption) { $existingOptionsMap[$existingOption->getOptionId()] = $existingOption; } $updatedOptionIds = []; foreach ($bundleProductOptions as $bundleOption) { $optionId = $bundleOption->getOptionId(); if ($optionId) { $updatedOptionIds[] = $optionId; } } $optionIdsToDelete = array_diff(array_keys($existingOptionsMap), $updatedOptionIds); //Handle new and existing options foreach ($bundleProductOptions as $option) { $this->optionRepository->save($result, $option); } //Delete options that are not in the list foreach ($optionIdsToDelete as $optionId) { $this->optionRepository->delete($existingOptionsMap[$optionId]); } return $subject->get($result->getSku(), false, $result->getStoreId(), true); }