public function testAddOptionArray() { $optionCode = 1234; $optionData = ['product' => 'test', 'code' => $optionCode]; $optionMock = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Item\\Option')->setMethods(['setData', 'setItem', 'getCode', '__wakeup', 'isDeleted'])->disableOriginalConstructor()->getMock(); $optionMock->expects($this->once())->method('setData')->with($optionData)->will($this->returnValue($optionMock)); $optionMock->expects($this->once())->method('setItem')->with($this->model)->will($this->returnValue($optionMock)); $optionMock->expects($this->exactly(3))->method('getCode')->will($this->returnValue($optionCode)); $this->itemOptionFactory->expects($this->at(0))->method('create')->will($this->returnValue($optionMock)); $this->model->addOption($optionData); $this->assertEquals([$optionMock], $this->model->getOptions()); $this->assertEquals([$optionCode => $optionMock], $this->model->getOptionsByCode()); $this->assertEquals($optionMock, $this->model->getOptionByCode($optionCode)); }
/** * Parse user input value and return cart prepared value * * @param string $optionValue * @param array $productOptionValues Values for product option * @return string|null * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function parseOptionValue($optionValue, $productOptionValues) { // search quote item option Id in option value if (preg_match('/\\[([0-9]+)\\]/', $optionValue, $matches)) { $confItemOptionId = $matches[1]; $option = $this->_itemOptionFactory->create()->load($confItemOptionId); try { unserialize($option->getValue()); return $option->getValue(); } catch (\Exception $e) { return null; } } else { return null; } }
/** * Add option to item * * @param \Magento\Quote\Model\Quote\Item\Option|\Magento\Framework\DataObject $option * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function addOption($option) { if (is_array($option)) { $option = $this->_itemOptionFactory->create()->setData($option)->setItem($this); } elseif ($option instanceof \Magento\Framework\DataObject && !$option instanceof \Magento\Quote\Model\Quote\Item\Option) { $option = $this->_itemOptionFactory->create()->setData($option->getData())->setProduct($option->getProduct())->setItem($this); } elseif ($option instanceof \Magento\Quote\Model\Quote\Item\Option) { $option->setItem($this); } else { throw new \Magento\Framework\Exception\LocalizedException(__('We found an invalid item option format.')); } $exOption = $this->getOptionByCode($option->getCode()); if ($exOption) { $exOption->addData($option->getData()); } else { $this->_addOptionCode($option); $this->_options[] = $option; } return $this; }