/** * @param \Magento\Catalog\Api\Data\ProductInterface $product * @param \Magento\ConfigurableProduct\Api\Data\OptionInterface[] $options * @return $this */ protected function saveConfigurableProductOptions(\Magento\Catalog\Api\Data\ProductInterface $product, array $options) { $existingOptionIds = []; if ($product->getExtensionAttributes() !== null) { $extensionAttributes = $product->getExtensionAttributes(); if ($extensionAttributes->getConfigurableProductOptions() !== null) { $existingOptions = $extensionAttributes->getConfigurableProductOptions(); foreach ($existingOptions as $option) { $existingOptionIds[] = $option->getId(); } } } $updatedOptionIds = []; foreach ($options as $option) { if ($option->getId()) { $updatedOptionIds[] = $option->getId(); } $this->optionRepository->save($product->getSku(), $option); } $optionIdsToDelete = array_diff($existingOptionIds, $updatedOptionIds); foreach ($optionIdsToDelete as $optionId) { $this->optionRepository->deleteById($product->getSku(), $optionId); } return $this; }
/** * @param string $entityType * @param ProductInterface $entity * @return ProductInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entityType, $entity) { if ($entity->getTypeId() !== Configurable::TYPE_CODE) { return $entity; } $extensionAttributes = $entity->getExtensionAttributes(); $extensionAttributes->setConfigurableProductLinks($this->getLinkedProducts($entity)); $extensionAttributes->setConfigurableProductOptions($this->optionLoader->load($entity)); $entity->setExtensionAttributes($extensionAttributes); return $entity; }
/** * Return the stock item that needs to be updated. * If the stock item does not need to be updated, return null. * * @param \Magento\Catalog\Api\Data\ProductInterface $product * @return \Magento\CatalogInventory\Api\Data\StockItemInterface|null */ protected function getStockItemToBeUpdated($product) { // from the API, all the data we care about will exist as extension attributes of the original product $extendedAttributes = $product->getExtensionAttributes(); if ($extendedAttributes !== null) { $stockItem = $extendedAttributes->getStockItem(); if ($stockItem != null) { return $stockItem; } } // we have no new stock item information to update, however we need to ensure that the product does have some // stock item information present. On a newly created product, it will not have any stock item info. $stockItem = $this->stockRegistry->getStockItem($product->getId()); if ($stockItem->getItemId() != null) { // we already have stock item info, so we return null since nothing more needs to be updated return null; } return $stockItem; }
/** * @param ProductInterface $entity * @param array $arguments * @return ProductInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entity, $arguments = []) { if ($entity->getTypeId() !== Configurable::TYPE_CODE) { return $entity; } $extensionAttributes = $entity->getExtensionAttributes(); if ($extensionAttributes === null) { return $entity; } if ($extensionAttributes->getConfigurableProductOptions() !== null) { $this->deleteConfigurableProductAttributes($entity); } $configurableOptions = (array) $extensionAttributes->getConfigurableProductOptions(); if (!empty($configurableOptions)) { $this->saveConfigurableProductAttributes($entity, $configurableOptions); } $configurableLinks = (array) $extensionAttributes->getConfigurableProductLinks(); $this->resourceModel->saveProducts($entity, $configurableLinks); return $entity; }
/** * Initialize data for configurable product * * @param Helper $subject * @param ProductInterface $product * @return ProductInterface * @throws \InvalidArgumentException * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterInitialize(Helper $subject, ProductInterface $product) { $attributes = $this->request->getParam('attributes'); $productData = $this->request->getPost('product', []); if ($product->getTypeId() !== ConfigurableProduct::TYPE_CODE || empty($attributes)) { return $product; } $setId = $this->request->getPost('new-variations-attribute-set-id'); if ($setId) { $product->setAttributeSetId($setId); } $extensionAttributes = $product->getExtensionAttributes(); $product->setNewVariationsAttributeSetId($setId); $configurableOptions = []; if (!empty($productData['configurable_attributes_data'])) { $configurableOptions = $this->optionsFactory->create((array) $productData['configurable_attributes_data']); } $extensionAttributes->setConfigurableProductOptions($configurableOptions); $this->setLinkedProducts($product, $extensionAttributes); $product->setCanSaveConfigurableAttributes((bool) $this->request->getPost('affect_configurable_product_attributes')); $product->setExtensionAttributes($extensionAttributes); return $product; }
/** * @param ProductInterface $product * @return StockItemInterface * @throws LocalizedException */ private function getStockItemFromProduct(ProductInterface $product) { $stockItem = null; $extendedAttributes = $product->getExtensionAttributes(); if ($extendedAttributes !== null) { $stockItem = $extendedAttributes->getStockItem(); if ($stockItem) { $this->validateStockItem($product, $stockItem); } } return $stockItem; }
/** * @param \Magento\Catalog\Api\Data\ProductInterface $product * @param \Magento\Downloadable\Api\Data\SampleInterface[] $samples * @return $this */ protected function saveSamples(\Magento\Catalog\Api\Data\ProductInterface $product, array $samples) { $existingSampleIds = []; $extensionAttributes = $product->getExtensionAttributes(); if ($extensionAttributes !== null) { $existingSamples = $extensionAttributes->getDownloadableProductSamples(); if ($existingSamples !== null) { foreach ($existingSamples as $existingSample) { $existingSampleIds[] = $existingSample->getId(); } } } $updatedSampleIds = []; foreach ($samples as $sample) { $sampleId = $sample->getId(); if ($sampleId) { $updatedSampleIds[] = $sampleId; } $this->sampleRepository->save($product->getSku(), $sample); } $sampleIdsToDelete = array_diff($existingSampleIds, $updatedSampleIds); foreach ($sampleIdsToDelete as $sampleId) { $this->sampleRepository->delete($sampleId); } return $this; }