How to get
OptionTypeId
of custom option of simple product with option type field or area. Option type dropdown return the value of
OptionTypeId
but I also want this value with option type field or area.
Thanks in advance.
Please try this code:
$customOptions = $objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($_product);
foreach($customOptions as $optionKey => $optionVal){
foreach($optionVal->getValues() as $valuesKey => $valuesVal) {
echo $valuesVal->getId().' '.$valuesVal->getTitle();
}
}
$optionVal->getValues()
It will return OptionTypeId if option type is dropdown, checkbox or radio. I want optionTypeId with option type text and area.
Commented
Jul 6, 2022 at 12:53
Also using below code you can check product have custom option or not ?
<?php
namespace namespace\modulename\Model;
use Exception;
use Psr\Log\LoggerInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
class ProductOptions
{
private $logger;
private $productRepository;
public function __construct(
LoggerInterface $logger,
ProductRepositoryInterface $productRepository
) {
$this->logger = $logger;
$this->productRepository = $productRepository;
}
public function hasOptionsProduct()
{
$sku = '24-WG080';
$hasOptions = false;
try {
$product = $this->productRepository->get($sku);
$hasOptions = $product->hasOptions();
} catch (Exception $exception) {
throw new Exception($exception->getMessage());
}
return $hasOptions;
}
}
if product has custom option then you can get optionTypeId().