/** * Convert option data to array * @param \Magento\Catalog\Api\Data\ProductCustomOptionInterface $option * @return array */ public function toArray(\Magento\Catalog\Api\Data\ProductCustomOptionInterface $option) { $optionData = $option->getData(); $values = $option->getValues(); $valuesData = []; if (!empty($values)) { foreach ($values as $key => $value) { $valuesData[$key] = $value->getData(); } } $optionData['values'] = $valuesData; return $optionData; }
/** * {@inheritdoc} */ public function save(\Magento\Catalog\Api\Data\ProductCustomOptionInterface $option) { $sku = $option->getProductSku(); $product = $this->productRepository->get($sku, true); $optionData = $this->converter->toArray($option); if ($option->getOptionId()) { if (!$product->getOptionById($option->getOptionId())) { throw new NoSuchEntityException(); } $originalValues = $product->getOptionById($option->getOptionId())->getValues(); if (!empty($optionData['values'])) { $optionData['values'] = $this->markRemovedValues($optionData['values'], $originalValues); } } unset($optionData['product_sku']); $product->setProductOptions([$optionData]); $existingOptions = $product->getOptions(); try { $this->productRepository->save($product, true); } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not save product option')); } $product = $this->productRepository->get($sku, true); if (!$option->getOptionId()) { $currentOptions = $product->getOptions(); if ($existingOptions == null) { $newID = array_keys($currentOptions); } else { $newID = array_diff(array_keys($currentOptions), array_keys($existingOptions)); } if (empty($newID)) { throw new CouldNotSaveException(__('Could not save product option')); } $newID = current($newID); } else { $newID = $option->getOptionId(); } $option = $this->get($sku, $newID); return $option; }
/** * @covers \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper::initialize * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testInitialize() { $this->websiteMock->expects($this->once())->method('getId')->willReturn($this->websiteId); $this->storeMock->expects($this->once())->method('getWebsite')->willReturn($this->websiteMock); $this->storeManagerMock->expects($this->once())->method('getStore')->with(true)->willReturn($this->storeMock); $this->customOptionMock->expects($this->once())->method('setProductSku'); $this->customOptionMock->expects($this->once())->method('setOptionId'); $productData = ['stock_data' => ['stock_data'], 'options' => ['option1' => ['is_delete' => true], 'option2' => ['is_delete' => false]]]; $attributeNonDate = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)->disableOriginalConstructor()->getMock(); $attributeDate = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)->disableOriginalConstructor()->getMock(); $attributeNonDateBackEnd = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend::class)->disableOriginalConstructor()->getMock(); $attributeDateBackEnd = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Backend\Datetime::class)->disableOriginalConstructor()->getMock(); $attributeNonDate->expects($this->any())->method('getBackend')->willReturn($attributeNonDateBackEnd); $attributeDate->expects($this->any())->method('getBackend')->willReturn($attributeDateBackEnd); $this->productMock->expects($this->any())->method('getProductLinks')->willReturn([]); $attributeNonDateBackEnd->expects($this->any())->method('getType')->willReturn('non-datetime'); $attributeDateBackEnd->expects($this->any())->method('getType')->willReturn('datetime'); $attributesArray = [$attributeNonDate, $attributeDate]; $useDefaults = ['attributeCode1', 'attributeCode2']; $this->requestMock->expects($this->at(0))->method('getPost')->with('product')->willReturn($productData); $this->requestMock->expects($this->at(1))->method('getPost')->with('use_default')->willReturn($useDefaults); $this->requestMock->expects($this->at(3))->method('getPost')->with('options_use_default')->willReturn(true); $this->stockFilterMock->expects($this->once())->method('filter')->with(['stock_data'])->willReturn(['stock_data']); $this->storeManagerMock->expects($this->once())->method('hasSingleStore')->willReturn(true); $this->productMock->expects($this->once())->method('isLockedAttribute')->with('media')->willReturn(true); $this->productMock->expects($this->once())->method('unlockAttribute')->with('media'); $this->productMock->expects($this->any())->method('getProductLinks')->willReturn([]); $this->productMock->expects($this->once())->method('lockAttribute')->with('media'); $this->productMock->expects($this->once())->method('getAttributes')->willReturn($attributesArray); $productData['category_ids'] = []; $productData['website_ids'] = []; $this->productMock->expects($this->once())->method('addData')->with($productData); $this->productMock->expects($this->once())->method('getSku')->willReturn('sku'); $this->productMock->expects($this->once())->method('setWebsiteIds')->with([$this->websiteId]); $this->productMock->expects($this->any())->method('getOptionsReadOnly')->willReturn(false); $this->productMock->expects($this->once())->method('setOptions')->with([$this->customOptionMock]); $this->assertEquals($this->productMock, $this->helper->initialize($this->productMock)); }
/** * Receive option value based on option type * * @param ProductCustomOptionInterface $option * @return null|string */ protected function getOptionRequestValue(ProductCustomOptionInterface $option) { $returnValue = null; switch ($option->getType()) { case 'field': $returnValue = 'Test value'; break; case 'date_time': $returnValue = '2015-09-09 07:16:00'; break; case 'drop_down': $returnValue = '3-1-select'; break; case 'radio': $returnValue = '4-1-radio'; break; } return $returnValue; }
/** * {@inheritdoc} */ public function save(\Magento\Catalog\Api\Data\ProductCustomOptionInterface $option) { $productSku = $option->getProductSku(); if (!$productSku) { throw new CouldNotSaveException(__('ProductSku should be specified')); } $product = $this->productRepository->get($productSku); $metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class); $option->setData('product_id', $product->getData($metadata->getLinkField())); $option->setOptionId(null); $option->save(); return $option; }
/** * {@inheritdoc} */ public function save(\Magento\Catalog\Api\Data\ProductCustomOptionInterface $option) { $product = $this->productRepository->get($option->getProductSku()); $metadata = $this->metadataPool->getMetadata(ProductInterface::class); $option->setData('product_id', $product->getData($metadata->getLinkField())); $option->setOptionId(null); $option->save(); return $option; }