0

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.

2 Answers 2

0

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();
           }
    }
2
  • $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
  • in textarea field you did not get option. because textarea have no option. Commented Jul 6, 2022 at 12:58
0

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().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.