cancel
Showing results for 
Search instead for 
Did you mean: 

Set defautl value in Price Field in backend

Set defautl value in Price Field in backend

Hello,

I'am trying to set a default value (in example 50$) in the product price field in backend when I'am creating a new product.

I have added the file \app\code\MyVendor\MyModule\view\adminhtml\ui_component\product_form.xml

with the following code :

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="product-details">
        <field name="price">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="value" xsi:type="string">50</item>
                    <item name="disabled" xsi:type="boolean">true</item>
                </item>
            </argument>
        </field>
    </fieldset>
 </form>

The default value 50 is set but another field is created :

probleme_magento.png

Is someone has an idea about this field duplication ?
I'am using magento 2.3.4

 

Regards,

 

5 REPLIES 5

Re: Set defautl value in Price Field in backend

Hi @Yohannento 

Try the following code once:

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="product-details">
        <field name="price">
            <argument name="data" xsi:type="array">
                    <item name="value" xsi:type="string">50</item>
            </argument>
        </field>
    </fieldset>
 </form>


for more info :

https://devdocs.magento.com/guides/v2.3/howdoi/customize-form-configuration.html

I hope it will help you!

Re: Set defautl value in Price Field in backend

Hi Vimal,

 

Thansk for your reply, I have already see the post https://devdocs.magento.com/guides/v2.3/howdoi/customize-form-configuration.html.
I have tried your code but now I have an exception :

1 exception(s):
Exception #0 (Magento\Framework\Exception\LocalizedException): The "formElement" configuration parameter is required for the "price" field.

Exception #0 (Magento\Framework\Exception\LocalizedException): The "formElement" configuration parameter is required for the "price" field.
<pre>#1 Magento\Framework\View\Layout\Generator\UiComponent->prepareComponent() called at [vendor/magento/framework/View/Layout/Generator/UiComponent.php:161]
#2 Magento\Framework\View\Layout\Generator\UiComponent->prepareComponent() called at [vendor/magento/framework/View/Layout/Generator/UiComponent.php:161]
#3 Magento\Framework\View\Layout\Generator\UiComponent->prepareComponent() called at [vendor/magento/framework/View/Layout/Generator/UiComponent.php:142]
#4 Magento\Framework\View\Layout\Generator\UiComponent->generateComponent() called at [vendor/magento/framework/View/Layout/Generator/UiComponent.php:103]
#5 Magento\Framework\View\Layout\Generator\UiComponent->process() called at [vendor/magento/framework/View/Layout/G

Have you please another idea ?

Re: Set defautl value in Price Field in backend

Hi @Yohannento 

Try the below workaround for now.

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="product-details">
        <field name="price">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="value" xsi:type="string">50</item>
                    <item name="visible" xsi:type="boolean">false</item>
                </item>
            </argument>
        </field>
    </fieldset> 
</form>

I hope it will help you!

Re: Set defautl value in Price Field in backend

Thanks, the workaround works !
I'am not sure to use it in production. I'am afraid about hiding an extra field in product creation. I don't know if it can cause problem by creating another price attribute. I will continue to search the solution without workaround... But many thanks for your help

 

Re: Set defautl value in Price Field in backend

Hi @Yohannento 

 

Great!

 

Btw it will not create any issue. The additional field we have used only for price attribute code for this group only on add new product page form only.