1

I am using Magento2.3 I need to how to add custom option of product when create order programmatically?

I used below code for add custom option to product but it is not working

$product = $om->create('\Magento\Catalog\Model\Product')->load(5);

    $cart = $om->create('Magento\Checkout\Model\Cart');    
    $params = array();      
    $options = array();
    $params['qty'] = 1;
    $params['product'] = 5;

    foreach ($product->getOptions() as $o) 
    {       
        foreach ($o->getValues() as $value) 
        {
            $options[$value['option_id']] = $value['option_type_id'];

        }           
    }

    $params['options'] = $options;
    $cart->addProduct($product, intval($params));

above code using after getting this error below

Fatal error: Uncaught Magento\Framework\Exception\LocalizedException: A server error stopped your order from being placed. Please try to place your order again. in /var/www/html/testproject/vendor/magento/module-quote/Model/QuoteManagement.php:368 Stack trace: #0 /var/www/html/testproject/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Quote\Model\QuoteManagement->placeOrder('87', NULL) #1 /var/www/html/testproject/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Quote\Model\QuoteManagement\Interceptor->___callParent('placeOrder', Array) #2 /var/www/html/testproject/vendor/magento/module-braintree/Plugin/OrderCancellation.php(63): Magento\Quote\Model\QuoteManagement\Interceptor->Magento\Framework\Interception\{closure}('87', NULL) #3 /var/www/html/testproject/vendor/magento/framework/Interception/Interceptor.php(135): Magento\Braintree\Plugin\OrderCancellation->aroundPlaceOrder(Object(Magento\Quote\Model\QuoteManagement\Interceptor), Object(Closure), '87') #4 /var/www/html/testproject in /var/www/html/testproject/vendor/magento/module-quote/Model/QuoteManagement.php on line 368

Please help me if any one know about this thanks for in advance.