To create custom field in Bundle custom options , you need to complete the following steps:
Step 1: Create the etc/adminhtml/di.xml file
Step 2: Create app/code/Tridhyatech/CustomOptions/Plugin/Catalog/Ui/DataProvider/Product/Form/Modifier/BundlePanel.php
step :3 app/code/Tridhyatech/CustomOptions/Ui/DataProvider/Product/Form/Modifier/Composite.php
Step 4: Create InstallSchema file
Step 5: Run command
1.) create di.xml file in Tridhyatech/CustomOptions/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\Bundle\Ui\DataProvider\Product\Form\Modifier\BundlePanel"> <plugin name="bundle_addtional_options" type="Tridhyatech\CustomOption\Plugin\Catalog\Ui\DataProvider\Product\Form\Modifier\BundlePanel" sortOrder="1000"/> </type> <preference for="Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\Composite" type="Tridhyatech\CustomOption\Ui\DataProvider\Product\Form\Modifier\Composite"/> </config>
2.) create plugin Tridhyatech/CustomOptions/Plugin/Catalog/Ui/DataProvider/Product/Form/Modifier/BundlePanel.php
namespace Tridhyatech\CustomOption\Plugin\Catalog\Ui\DataProvider\Product\Form\Modifier; class BundlePanel { public function afterModifyMeta( \Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\BundlePanel $subject, $meta ){ $fieldSet = [ 'option_info' => [ 'dataType' => \Magento\Ui\Component\Form\Element\DataType\Text::NAME, 'formElement' => \Magento\Ui\Component\Form\Element\Input::NAME, 'label' => 'Option Message', 'dataScope' => 'option_info', 'sortOrder' => 11 ] ]; foreach ($fieldSet as $filed => $fieldOptions) { $meta['bundle-items']['children']['bundle_options']['children'] ['record']['children']['product_bundle_container']['children']['option_info']['children'][$filed] = $this->getSelectionCustomText($fieldOptions); } return $meta; } protected function getSelectionCustomText($fieldOptions) { return [ 'arguments' => [ 'data' => [ 'config' => [ 'componentType' => \Magento\Ui\Component\Form\Field::NAME, 'dataType' => $fieldOptions['dataType'], 'formElement' => $fieldOptions['formElement'], 'label' => __($fieldOptions['label']), 'dataScope' => $fieldOptions['dataScope'], 'sortOrder' => $fieldOptions['sortOrder'], 'options' => array_key_exists('options', $fieldOptions) ? $fieldOptions['options']: "", ] ] ] ]; } protected function getSkuFieldConfig($sortOrder, array $options = []){ return array_replace_recursive( [ 'arguments' => [ 'data' => [ 'config' => [ 'label' => __('Option Message'), 'componentType' => \Magento\Ui\Component\Form\Field::NAME, 'formElement' => \Magento\Ui\Component\Form\Element\Input::NAME, 'dataScope' => 'option_info', 'dataType' => \Magento\Ui\Component\Form\Element\DataType\Text::NAME, 'sortOrder' => $sortOrder, 'validation' => [ 'required-entry' => true, 'validate-alphanum-with-spaces' => true ] ] ] ] ], $options ); } }
3.)
create preference
Tridhyatech\CustomOption\Ui\DataProvider\Product\Form\Modifier\Composite.php
namespace Tridhyatech\CustomOption\Ui\DataProvider\Product\Form\Modifier; use Magento\Bundle\Model\Product\Type; use Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\BundlePanel; class Composite extends \Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\Composite { public function modifyData(array $data) { /** @var \Magento\Catalog\Api\Data\ProductInterface $product */ $product = $this->locator->getProduct(); $modelId = $product->getId(); $isBundleProduct = $product->getTypeId() === Type::TYPE_CODE; if ($isBundleProduct && $modelId) { $data[$modelId][BundlePanel::CODE_BUNDLE_OPTIONS] [BundlePanel::CODE_BUNDLE_OPTIONS] = []; $extra_options = $this->getAdditionalAttributes($product); /** @var \Magento\Bundle\Api\Data\OptionInterface $option */ foreach ($this->optionsRepository->getList($product->getSku()) as $option) { $selections = []; foreach ($option->getProductLinks() as $productLink) { $linkedProduct = $this->productRepository->get($productLink->getSku()); $integerQty = 1; if ($linkedProduct->getExtensionAttributes()->getStockItem()) { if ($linkedProduct->getExtensionAttributes()->getStockItem()->getIsQtyDecimal()) { $integerQty = 0; } } $selections[] = [ 'selection_id' => $productLink->getId(), 'option_id' => $productLink->getOptionId(), 'product_id' => $linkedProduct->getId(), 'name' => $linkedProduct->getName(), 'sku' => $linkedProduct->getSku(), 'is_default' => ($productLink->getIsDefault()) ? '1' : '0', 'selection_price_value' => $productLink->getPrice(), 'selection_price_type' => $productLink->getPriceType(), 'selection_qty' => (bool)$integerQty ? (int)$productLink->getQty() : $productLink->getQty(), 'selection_can_change_qty' => $productLink->getCanChangeQuantity(), 'selection_qty_is_integer' => (bool)$integerQty, 'position' => $productLink->getPosition(), 'delete' => '', ]; } $data[$modelId][BundlePanel::CODE_BUNDLE_OPTIONS][BundlePanel::CODE_BUNDLE_OPTIONS][] = [ 'position' => $option->getPosition(), 'option_id' => $option->getOptionId(), 'title' => $option->getTitle(), 'option_info' => $extra_options[$option->getOptionId()]['option_info'], 'default_title' => $option->getDefaultTitle(), 'type' => $option->getType(), 'required' => ($option->getRequired()) ? '1' : '0', 'bundle_selections' => $selections, ]; } } return $data; } public function getAdditionalAttributes($product) { $optionArray = array(); $_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $_objectManager->get('\Magento\Catalog\Model\Product')->load($product->getId()); $optionsCollection = $product->getTypeInstance(true) ->getOptionsCollection($product); foreach ($optionsCollection as $options) { $optionArray[$options->getOptionId()]['option_info'] = $options->getOptionInfo(); } return $optionArray; } }
4.) Create InstallSchema.php File
namespace Tridhyatech\CustomOption\Setup; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; use Magento\Framework\DB\Ddl\Table; class InstallSchema implements InstallSchemaInterface { public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); $setup->getConnection()->addColumn( $setup->getTable('catalog_product_bundle_option'), 'option_info', [ 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 'size' => 255, 'comment' => 'Option Info', ] ); $setup->endSetup(); } }
5.) Run below command :
php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento c:f