/** * @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 string $entityType * @param object $entity * @return \Magento\Catalog\Api\Data\ProductInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entityType, $entity) { /** @var $entity \Magento\Catalog\Api\Data\ProductInterface */ if ($entity->getTypeId() != \Magento\Bundle\Model\Product\Type::TYPE_CODE) { return $entity; } $entityExtension = $entity->getExtensionAttributes(); $options = $this->optionRepository->getListByProduct($entity); if ($options) { $entityExtension->setBundleProductOptions($options); } $entity->setExtensionAttributes($entityExtension); return $entity; }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function modifyData(array $data) { /** @var \Magento\Catalog\Api\Data\ProductInterface $product */ $product = $this->locator->getProduct(); $modelId = $product->getId(); $isBundleProduct = $product->getTypeId() === Type::TYPE_CODE; if ($isBundleProduct && $modelId) { $data[$modelId][BundlePanel::CODE_BUNDLE_OPTIONS][BundlePanel::CODE_BUNDLE_OPTIONS] = []; /** @var \Magento\Bundle\Api\Data\OptionInterface $option */ foreach ($this->optionsRepository->getList($product->getSku()) as $option) { $selections = []; /** @var \Magento\Bundle\Api\Data\LinkInterface $productLink */ foreach ($option->getProductLinks() as $productLink) { $linkedProduct = $this->productRepository->get($productLink->getSku()); $integerQty = 1; if ($linkedProduct->getExtensionAttributes()->getStockItem()) { if ($linkedProduct->getExtensionAttributes()->getStockItem()->getIsQtyDecimal()) { $integerQty = 0; } } $selections[] = ['selection_id' => $productLink->getId(), 'option_id' => $productLink->getOptionId(), 'product_id' => $linkedProduct->getId(), 'name' => $linkedProduct->getName(), 'sku' => $linkedProduct->getSku(), 'is_default' => $productLink->getIsDefault() ? '1' : '0', 'selection_price_value' => $productLink->getPrice(), 'selection_price_type' => $productLink->getPriceType(), 'selection_qty' => (bool) $integerQty ? (int) $productLink->getQty() : $productLink->getQty(), 'selection_can_change_qty' => $productLink->getCanChangeQuantity(), 'selection_qty_is_integer' => (bool) $integerQty, 'position' => $productLink->getPosition(), 'delete' => '']; } $data[$modelId][BundlePanel::CODE_BUNDLE_OPTIONS][BundlePanel::CODE_BUNDLE_OPTIONS][] = ['position' => $option->getPosition(), 'option_id' => $option->getOptionId(), 'title' => $option->getTitle(), 'default_title' => $option->getDefaultTitle(), 'type' => $option->getType(), 'required' => $option->getRequired() ? '1' : '0', 'bundle_selections' => $selections]; } } return $data; }
/** * @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); }