/** * @covers \Magento\Catalog\Model\Product::addOption * @covers \Magento\Catalog\Model\Product::getOptionById * @covers \Magento\Catalog\Model\Product::getOptions */ public function testOptionApi() { $this->assertEquals([], $this->_model->getOptions()); $optionId = uniqid(); $option = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product\\Option', ['data' => ['key' => 'value']]); $option->setId($optionId); $this->_model->addOption($option); $this->assertSame($option, $this->_model->getOptionById($optionId)); $this->assertEquals([$optionId => $option], $this->_model->getOptions()); }
/** * @covers \Magento\Catalog\Model\Product::setOptions * @covers \Magento\Catalog\Model\Product::getOptionById * @covers \Magento\Catalog\Model\Product::getOptions */ public function testOptionApi() { $this->assertNull($this->_model->getOptions()); $optionId = uniqid(); $option = $this->objectManager->create('Magento\\Catalog\\Model\\Product\\Option', ['data' => ['key' => 'value']]); $option->setId($optionId); $this->_model->setOptions([$option]); $this->assertSame($option, $this->_model->getOptionById($optionId)); $this->assertEquals([$option], $this->_model->getOptions()); }
public function testGetOptions() { $option1Id = 2; $optionMock1 = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product\\Option')->disableOriginalConstructor()->setMethods(['getId', 'setProduct'])->getMock(); $option2Id = 3; $optionMock2 = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product\\Option')->disableOriginalConstructor()->setMethods(['getId', 'setProduct'])->getMock(); $expectedOptions = [$option1Id => $optionMock1, $option2Id => $optionMock2]; $this->model->setOptions($expectedOptions); $this->assertEquals($expectedOptions, $this->model->getOptions()); //Calling the method again, empty options array will be returned $this->model->setOptions([]); $this->assertEquals([], $this->model->getOptions()); }
/** * Build buy request based on product custom options * * @param Product $product * @return \Magento\Framework\DataObject */ private function prepareBuyRequest(Product $product) { $options = []; /** @var $option \Magento\Catalog\Model\Product\Option */ foreach ($product->getOptions() as $option) { switch ($option->getGroupByType()) { case \Magento\Catalog\Model\Product\Option::OPTION_GROUP_DATE: $value = ['year' => 2013, 'month' => 8, 'day' => 9, 'hour' => 13, 'minute' => 35]; break; case \Magento\Catalog\Model\Product\Option::OPTION_GROUP_SELECT: $value = key($option->getValues()); break; default: $value = 'test'; break; } $options[$option->getId()] = $value; } return new \Magento\Framework\DataObject(['qty' => 1, 'options' => $options]); }
/** * Return true if product has options * * @param \Magento\Catalog\Model\Product $product * @return bool */ public function hasOptions($product) { if ($product->getOptions()) { return true; } $attributes = $this->getConfigurableAttributes($product); if (count($attributes)) { foreach ($attributes as $attribute) { /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $attribute */ if ($attribute->getData('prices')) { return true; } } } return false; }
/** * @magentoDataFixture Magento/Catalog/_files/product_simple.php * @magentoAppIsolation enabled */ public function testGetOptions() { $this->_model->load(1); $options = $this->_model->getOptions(); $this->assertEquals(4, count($options)); }
/** * @param \Magento\Catalog\Model\Product $product * @return void */ protected function processDynamicOptionsData(\Magento\Catalog\Model\Product $product) { if ((int) $product->getPriceType() !== \Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC) { return; } if ($product->getOptionsReadonly()) { return; } $product->setCanSaveCustomOptions(true); $customOptions = $product->getProductOptions(); if (!$customOptions) { return; } foreach (array_keys($customOptions) as $key) { $customOptions[$key]['is_delete'] = 1; } $newOptions = $product->getOptions(); foreach ($customOptions as $customOptionData) { if ((bool) $customOptionData['is_delete']) { continue; } $customOption = $this->customOptionFactory->create(['data' => $customOptionData]); $customOption->setProductSku($product->getSku()); $customOption->setOptionId(null); $newOptions[] = $customOption; } $product->setOptions($newOptions); }
/** * Process custom defined options for product * * @param \Magento\Framework\DataObject $buyRequest * @param \Magento\Catalog\Model\Product $product * @param string $processMode * @return array */ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $product, $processMode) { $transport = new \StdClass(); $transport->options = []; $options = null; if ($product->getHasOptions()) { $options = $product->getOptions(); } if ($options !== null) { foreach ($options as $option) { /* @var $option \Magento\Catalog\Model\Product\Option */ $group = $option->groupFactory($option->getType())->setOption($option)->setProduct($product)->setRequest($buyRequest)->setProcessMode($processMode)->validateUserValue($buyRequest->getOptions()); $preparedValue = $group->prepareForCart(); if ($preparedValue !== null) { $transport->options[$option->getId()] = $preparedValue; } } } $eventName = sprintf('catalog_product_type_prepare_%s_options', $processMode); $this->_eventManager->dispatch($eventName, ['transport' => $transport, 'buy_request' => $buyRequest, 'product' => $product]); return $transport->options; }
/** * @magentoDataFixture Magento/Catalog/_files/product_simple.php * @magentoAppIsolation enabled */ public function testGetOptions() { $this->_model = $this->productRepository->get('simple'); $this->assertEquals(4, count($this->_model->getOptions())); }
/** * Check if product can be bought * * @param \Magento\Catalog\Model\Product $product * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function checkProductBuyState($product) { if (!$product->getSkipCheckRequiredOption()) { foreach ($product->getOptions() as $option) { if ($option->getIsRequire()) { $customOption = $product->getCustomOption(self::OPTION_PREFIX . $option->getId()); if (!$customOption || strlen($customOption->getValue()) == 0) { $product->setSkipCheckRequiredOption(true); throw new \Magento\Framework\Exception\LocalizedException(__('The product has required options.')); } } } } return $this; }
/** * {@inheritdoc} */ public function getOptions() { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getOptions'); if (!$pluginInfo) { return parent::getOptions(); } else { return $this->___callPlugins('getOptions', func_get_args(), $pluginInfo); } }
/** * Return true if product has options * * @param \Magento\Catalog\Model\Product $product * @return bool */ public function hasOptions($product) { if ($product->getOptions()) { return true; } $attributes = $this->getConfigurableAttributes($product); if (count($attributes)) { return true; } return false; }
/** * Process custom defined options for product * * @param \Magento\Framework\DataObject $buyRequest * @param \Magento\Catalog\Model\Product $product * @param string $processMode * @return array * @throws LocalizedException */ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $product, $processMode) { $transport = new \StdClass(); $transport->options = []; $options = null; if ($product->getHasOptions()) { $options = $product->getOptions(); } if ($options !== null) { $results = []; foreach ($options as $option) { /* @var $option \Magento\Catalog\Model\Product\Option */ try { $group = $option->groupFactory($option->getType())->setOption($option)->setProduct($product)->setRequest($buyRequest)->setProcessMode($processMode)->validateUserValue($buyRequest->getOptions()); } catch (LocalizedException $e) { $results[] = $e->getMessage(); continue; } $preparedValue = $group->prepareForCart(); if ($preparedValue !== null) { $transport->options[$option->getId()] = $preparedValue; } } if (count($results) > 0) { throw new LocalizedException(__(implode("\n", $results))); } } $eventName = sprintf('catalog_product_type_prepare_%s_options', $processMode); $this->_eventManager->dispatch($eventName, ['transport' => $transport, 'buy_request' => $buyRequest, 'product' => $product]); return $transport->options; }