本文整理汇总了PHP中Magento\Catalog\Model\Product::getOptions方法的典型用法代码示例。如果您正苦于以下问题:PHP Product::getOptions方法的具体用法?PHP Product::getOptions怎么用?PHP Product::getOptions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\Catalog\Model\Product
的用法示例。
在下文中一共展示了Product::getOptions方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testOptionApi
/**
* @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());
}
示例2: testOptionApi
/**
* @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());
}
示例3: testGetOptions
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());
}
示例4: prepareBuyRequest
/**
* 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]);
}
示例5: hasOptions
/**
* 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;
}
示例6: testGetOptions
/**
* @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));
}
示例7: processDynamicOptionsData
/**
* @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);
}
示例8: _prepareOptions
/**
* 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;
}
示例9: testGetOptions
/**
* @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()));
}
示例10: checkProductBuyState
/**
* 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;
}
示例11: getOptions
/**
* {@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);
}
}
示例12: hasOptions
/**
* 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;
}
示例13: _prepareOptions
/**
* 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;
}