I would like to create a free input text field attribute, where basically the client, on the product page, have this input text field where he can insert a text, and if he chose to, the product will cost a bit more, basically a configured product. The problem is that when I try to create new attributes, the only option I get from Magento 2 are: Dropdown, Visual Swatch and Text Swatch(is a text I just click on, not a input field).
1 Answer
This functionality is not available in magento.
To achieve this you can create a free input text on product details page which is not related to any attribute on your addtocart.phtml or somewhere else but inside the product details form with proper name(input name).
After that you need to create an observer on event "checkout_cart_product_add_after". You will get the custom input value on post request and set the custom input label and value by following option:
$additionalOptions[] = [
'label' => 'Custom Label',
'value' => $value
];
$item->addOption([
'code' => 'additional_options',
'value' => json_encode($additionalOptions)
]);
Now you can set the custom price according to your requirement by following code on the same observer:
$item->setCustomPrice($customprice);
$item->setOriginalCustomPrice($customprice);
$item->getProduct()->setIsSuperMode(true);
Hope this helps!