How can I add a new text field in the custom options section in Magento 2 as shown in the screenshot below?
Magento 2 Custom Options – How to Add New Text Field in Custom Options Section
custom-optionsmagento2product
Related Solutions
This is because there is no code that will do this in the Magento core. The controller that does the product save action is in the file app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php
. Here you will find the code snippet.
/**
* Initialize product options
*/
if (isset($productData['options']) && !$product->getOptionsReadonly()) {
$product->setProductOptions($productData['options']);
}
$product->setCanSaveCustomOptions(
(bool)$this->getRequest()->getPost('affect_product_custom_options')
&& !$product->getOptionsReadonly()
);
All this is doing is setting the options on the product option and doing no image upload.
Then when you look at app/code/core/Mage/Catalog/Model/Product.php
there is the function _beforeSave
this function does the actual processing on options before saving them. What it does is loops through all the options and then adds them.
$this->getOptionInstance()->addOption($option);
So there is no code for uploading images or even saving them against the option itself. You will need to extend the options table to add your custom item, or use a custom model for storing option images.
Then what I would suggest is to listen to the admin event catalog_product_prepare_save
. This is fired after the options are set on the product. It has the product object and the request. You can use this event to perform the actual image upload and then update the product object if you need to.
Create a plugin for that. So you need to create new module Or add following code into your existing module.
SR/StackExchange/etc/adminhtml/di.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Catalog\Model\Config\Source\Product\Options\Price"> <plugin name="sr_stackexchange_add_price_type" type="SR\StackExchange\Plugin\Catalog\Model\Config\Source\Product\Options\Price" sortOrder="1"/> </type> </config>
SR/StackExchange/Plugin/Catalog/Model/Config/Source/Product/Options/Price.php
namespace SR\StackExchange\Plugin\Catalog\Model\Config\Source\Product\Options; class Price { /** * {@inheritdoc} * * @codeCoverageIgnore */ public function afterToOptionArray( \Magento\Catalog\Model\Config\Source\Product\Options\Price $subject, array $priceTypeOption ){ $priceTypeOption[] = ['value' => 'new_type', 'label' => __('New Type')]; return $priceTypeOption; } }
Clear magento cache.
Best Answer
Answer: I got the answer for the above question to add the text field by simply editing the customOptions.php file
step1: Add your text field at line number:68
const FIELD_NEW_TEXT_NAME = 'new_text_field';
step2:Add the Children at line number:584,634
static::FIELD_NEW_TEXT_NAME => $this->getNewTextFieldConfig(41),
step3:Copy the Total function code of getMaxCharactersFieldConfig and Create the function at line number:936
by following code:
step 4:Created the respected columns for fields in database find the table name below:
catalog_product_option catalog_product_option_type_value
Step 5:clear the cache and check the out put.