public function testProcessCustomOptions() { $optionId = 23; $quoteItemOption = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Item\\Option')->disableOriginalConstructor()->getMock(); $this->cartItem->expects($this->atLeastOnce())->method('getOptionByCode')->with('info_buyRequest')->willReturn($quoteItemOption); $quoteItemOption->expects($this->once())->method('getValue')->willReturn('a:1:{s:7:"options";a:1:{i:' . $optionId . ';a:2:{i:0;s:1:"5";i:1;s:1:"6";}}} '); $this->customOptionFactory->expects($this->once())->method('create')->willReturn($this->customOption); $this->customOption->expects($this->once())->method('setOptionId')->with($optionId); $this->customOption->expects($this->once())->method('setOptionValue')->with('5,6'); $this->cartItem->expects($this->atLeastOnce())->method('getProductOption')->willReturn(false); $this->productOptionFactory->expects($this->once())->method('create')->willReturn($this->productOption); $this->productOption->expects($this->once())->method('getExtensionAttributes')->willReturn(false); $this->extensionFactory->expects($this->once())->method('create')->willReturn($this->extensibleAttribute); $this->extensibleAttribute->expects($this->once())->method('setCustomOptions')->with([$optionId => $this->customOption]); $this->productOption->expects($this->once())->method('setExtensionAttributes')->with($this->extensibleAttribute); $this->cartItem->expects($this->once())->method('setProductOption')->with($this->productOption); $this->assertSame($this->cartItem, $this->processor->processOptions($this->cartItem)); }
/** * @inheritDoc */ public function convertToProductOption(DataObject $request) { $options = $request->getOptions(); if (!empty($options) && is_array($options)) { $data = []; foreach ($options as $optionId => $optionValue) { if (is_array($optionValue)) { $optionValue = implode(',', $optionValue); } /** @var CustomOption $option */ $option = $this->customOptionFactory->create(); $option->setOptionId($optionId)->setOptionValue($optionValue); $data[] = $option; } return ['custom_options' => $data]; } return []; }
/** * Update options values * * @param array $options * @return null */ protected function updateOptionsValues(array &$options) { foreach ($options as $optionId => &$optionValue) { /** @var \Magento\Catalog\Model\CustomOptions\CustomOption $option */ $option = $this->customOptionFactory->create(); $option->setOptionId($optionId); if (is_array($optionValue)) { $optionValue = implode(',', $optionValue); } $option->setOptionValue($optionValue); $optionValue = $option; } }