1

I want to add new custom option to upload image like extension : https://shop.emiprotechnologies.com/documentation/custom-option-image-for-magento-2 .
So basically , i override some file to add new custom and function to upload, but image not save in pub/media and save name file on database
1. Create module :
2. Declare your new product option : app/code/Vendor/Module/etc/product_options.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/product_options.xsd">
    <option name="select" label="Select" renderer="Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Options\Type\Select">
        <inputType name="gallery" label="Thumb Gallery" />
        <inputType name="gallery_popup" label="Thumb Gallery Popup" />
        <inputType name="gallery_multi_select" label="Thumb Gallery Multi Select" />
        </option>

</config>


3. Override file Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\CustomOptions to add new custom option :
app/code/Vendor/Module/etc/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">
    <preference for="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\CustomOptions" type="Vendor\Module\Ui\DataProvider\Product\Form\Modifier\CustomOptions" />
</config>

app/code/Vendor/Module/Ui/DataProvider/Product/Form/Modifier/CustomOptions.php

<?php


namespace Vendor\Module\Ui\DataProvider\Product\Form\Modifier;

use [...];

class CustomOptions  extends \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier
{
    const FIELD_IMAGE = 'image';
    const FIELD_COLOR = 'color';
    [...]
    protected function getSelectTypeGridConfig($sortOrder)
{
    $options = [...];

    return [],
        'children' => [
            'record' => [
                'arguments' => [...],
                'children' => [
                    static::FIELD_TITLE_NAME => $this->getTitleFieldConfig(
                        10,
                        $this->locator->getProduct()->getStoreId() ? $options : []
                    ),
                    static::FIELD_PRICE_NAME => $this->getPriceFieldConfigForSelectType(20),
                    static::FIELD_PRICE_TYPE_NAME => $this->getPriceTypeFieldConfig(30, ['fit' => true]),
                    static::FIELD_SKU_NAME => $this->getSkuFieldConfig(40),
                    static::FIELD_IMAGE => $this->getImageFieldConfig(50),
                    static::FIELD_COLOR => $this->getColorFieldConfig(70),
                    static::FIELD_SORT_ORDER_NAME => $this->getPositionFieldConfig(92),
                    static::FIELD_IS_DELETE => $this->getIsDeleteFieldConfig(95)
                ]
            ]
        ]
    ];
}
   protected function getImageFieldConfig($sortOrder)
    {
        return [
          'arguments' => [
              'data' => [
                 'config' => [
                    'label' => __('Upload'),
                    'componentType' => Field::NAME,
                    'formElement' => \Magento\Ui\Component\Form\Element\DataType\Media\Image::NAME,
                        'dataScope' => static::FIELD_IMAGE,
                    'dataType' => \Magento\Ui\Component\Form\Element\DataType\Media\Image::NAME,
                    'sortOrder' => $sortOrder,
                ],
            ],
        ],
    ];
 }
    [...]
}

Now, you can select image to upload, but image not save file name on database. what is mistake in my code.. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.