1

want to do this

enter image description here

I have followed similar (Magento 2 add new custom option text field) topic but it's about custom options for simple products. Somebody in commets say's that he achieved this for bundle following this link. But It didn't work for me.

Here I what i have:

di.xml file in my module

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<plugin name="bachus_bundlecontextpros_custom_option" type="Bachus\BundleContextPros\Plugin\Bundle\Ui\DataProvider\Product\Form\Modifier\BundleCustomOptions" sortOrder="1"/>
</type> 
</config>

and my BundleCustomOptions class

namespace Bachus\BundleContextPros\Plugin\Bundle\Ui\DataProvider\Product\Form\Modifier;

class BundleCustomOptions {

public function afterModifyMeta(
    \Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\BundleCustomOptions $subject,
    $meta
    ) {
        $meta['custom_options']['children']['options']['children']['record']['children']['container_option']['children']['container_common']['children']['custom_text'] =
        $this->getTitleFieldConfig(
            200,
            [
                'arguments' => [
                    'data' => [
                        'config' => [
                            'label' => __('Custom Text'),
                            'component' => 'Magento_Catalog/component/static-type-input',
                            'valueUpdate' => 'input',
                            'imports' => [
                                'optionId' => '${ $.provider }:${ $.parentScope }.option_id'
                            ]
                        ],
                    ],
                ],
            ]
            );
        return $meta;
}

/**
 * Get config for "Title" fields
 *
 * @param int $sortOrder
 * @param array $options
 * @return array
 */
protected function getTitleFieldConfig($sortOrder, array $options = [])
{
    return array_replace_recursive(
        [
            'arguments' => [
                'data' => [
                    'config' => [
                        'label' => __('Custom Text'),
                        'componentType' => \Magento\Ui\Component\Form\Field::NAME,
                        'formElement' => \Magento\Ui\Component\Form\Element\Input::NAME,
                        'dataScope' => 'custom_text',
                        'dataType' => \Magento\Ui\Component\Form\Element\DataType\Text::NAME,
                        'sortOrder' => $sortOrder,
                        'validation' => [
                            'required-entry' => false
                        ],
                    ],
                ],
            ],
        ],
        $options
        );
}

}

Update: Now I am able to view the custom field in bunlde options by modyfing Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\BundlePanel.php

adding just after requierd

'bundle_template' => [
               'arguments' => [
                'data' => [
                    'config' => [
                        'dataType' => Form\Element\DataType\Text::NAME,
                        'formElement' => Form\Element\Input::NAME,
                        'componentType' => Form\Field::NAME,
                        'description' => __('bundle template'),
                        'dataScope' => 'bundle_template',
                        'label' => 'bundle template',
                        'sortOrder' => 40,
                    ],
                ],
            ],
        ],

UPDATE I am able to save the value in data base but not see in admin

I just simply crated colum with exact name that my new custom field has in table catalog_product_bundle_option but it didn't work.

I would appreciate any help. Thanks

1
0

You need to create a plugin to grab the data you stored in database:

di.xml

<type name="Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\Composite">
    <plugin name="PLUGIN_NAME" type="VENDOR\MODULE\Plugin\Composite" sortOrder="1" />
</type>

Plugin\Composite.php

    class Composite
{
    /**
     * @var LocatorInterface
     */
    protected $locator;

    /**
     * @var \VENDOR\MODULE\Model\ResourceModel\LIST\Collection
     */
    protected $collectionFactory;

    /**
     * Composite constructor.
     * @param LocatorInterface $locator
     * @param CollectionFactory $collectionFactory
     */
    public function __construct(
        LocatorInterface $locator,
        CollectionFactory $collectionFactory
    ) {
        $this->locator = $locator;
        $this->collectionFactory = $collectionFactory->create();
    }

    /**
     * @param \Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\Composite $subject
     * @param $data
     * @return mixed
     * @throws LocalizedException
     */
    public function afterModifyData(\Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\Composite $subject, $data)
    {
        if ($this->locator->getProduct()->getTypeId() !== 'bundle') {
            return $data;
        }

        $productId = $this->locator->getProduct()->getId();

        try {
            if (isset($data[$productId]['bundle_options']['bundle_options']) && is_array($data[$productId]['bundle_options']['bundle_options'])) {

                // Select your data from collectionFactory->toArray()['items']
                foreach ($data[$productId]['bundle_options']['bundle_options'] as $key => $option) {

                    foreach ($selectedData as $opData) {

                        if ($opData['option_id'] == $option['option_id']) {

                            $data[$productId]['bundle_options']['bundle_options'][$key]['FIELD'] = $opData['DATA'];

                        }
                    }
                }
            }
        } catch (\Exception $exception) {
            throw new LocalizedException(__('Could not load collection data'));
        }
        return $data;
    }
}
0

I cannot comment, however I appreciate the answer from @Emadello. I see some placeholder code so this example is incomplete.

For example, I do not know what the contents of $option is, I don't know how to load or retrieve only the options values. The answer assumes that the person implementing this would have a deep understanding of this specific area of functionality, in which case they would likely not need this help.

It would be really helpful if you provided a complete answer - explain how to retrieve the values from the database. The primary goal of the plugin for modifyData is to inject data that is not available using the default method - so the answer proposed leaves out the most critical component, that is how to retrieve the information to inject into the response.

0

Add custom field like 'Default Quantity'

https://magento.stackexchange.com/a/220754/21432

This explains how you can add a new field beside 'Default Quantity' field under bundle options

Your Answer

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

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