0

I want to add product chooser field like this enter image description here

I am using this code but its not working anyone please help me.

helper = $helper;
        $this->_allProducts = $allProducts;
        $this->_systemStore = $systemStore;
        $this->_productloader = $_productloader;
        $this->timezone = $timezone;
        parent::__construct($context, $registry, $formFactory, $data);
    }


    /**
     * @return Main
     * @throws \Magento\Framework\Exception\LocalizedException
     * @throws Exception
     */
    protected function _prepareForm()
    {
        $model = $this->_coreRegistry->registry('lofauction_product');
        $dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);

        $form = $this->_formFactory->create();
        //$form->setUseContainer(true);

        $fieldset = $form->addFieldset(
            'options_fieldset',
            ['legend' => __('Product Information'), 'class' => 'fieldset-wide fieldset-widget-options']
        );
        if ($model->getId()) {
            $fieldset->addField('auction_entity_id', 'hidden', ['name' => 'auction_entity_id']);
        }
        if (!$model->getProductId()) {
            $chooserField = $fieldset->addField(
                'options_fieldset_entity_id',
                'label',
                [
                    'name' => 'product_id',
                    'label' => __('Product'),
                    'required' => true,
                    'class' => 'widget-option',
                    'note' => __("Select a product"),
                ]
            );
            /*Add chooser helper for the field*/
            $helperData = [
                'data' => [
                    'button' => ['open' => __("Select Product...")]
                ]
            ];
            // $chooserField->setValue('product/' . $model->getProductId());
            // Fixes Require product.
            $chooserField->setValue($model->getProductId());
            $helperBlock = $this->getLayout()->createBlock(
                'Magento\Catalog\Block\Adminhtml\Product\Widget\Chooser',
                '',
                $helperData
            );

            $helperBlock->setConfig($helperData)
                ->setFieldsetId($fieldset->getId())
                ->prepareElementHtml($chooserField)
                ->setValue($model->getId());
        } else {
            $fieldset->addField(
                'product_id',
                'note',
                [
                    'name' => 'product_id',
                    'label' => __('Product'),
                    'title' => __('Product'),
                    'text' => $this->helper->getProductbyId($model->getProductId())->getName()
                ]
            );
        }
        

Please help me.how can we add one product choose field..?

1 Answer 1

0

In your widget.xml file for the product chooser field set the following class:

Magento\Catalog\Block\Adminhtml\Product\Widget\Chooser

That should do the trick. Example:

        <parameter name="product" xsi:type="block" visible="true" required="true" sort_order="10">
            <label translate="true">Product</label>
            <block class="Magento\Catalog\Block\Adminhtml\Product\Widget\Chooser">
                <data>
                    <item name="button" xsi:type="array">
                        <item name="open" xsi:type="string">Choose Product...</item>
                    </item>
                </data>
            </block>
        </parameter>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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