How can i make custom field value required ( compulsory ) in woocommerce product page when adding product

Vidhi picture Vidhi · May 23, 2014 · Viewed 7.2k times · Source

I have added custom text box field in my add product page in woocommerce. Now i want to make it required (compulsory). I tried it by passing argument "required"=>true. but it is not working. Please see below code

woocommerce_wp_text_input(
  array(
    'id' => 'special_price',
    'label' => __( 'Wholesaler Price *', 'woocommerce' ),
    'placeholder' => '',
    'desc_tip' => 'true',
    'required' => 'true',
    'description' => __( 'Enter wholesaler price here.', 'woocommerce' )
  )
);

but it is not making textbox compulsory. Please can anyone tell how can i do this?

Answer

Danijel picture Danijel · May 23, 2014

For the required HTML5 attribute and other custom attributes woocommerce_wp_text_input() function has custom_attributes option.

woocommerce_wp_text_input(
  array(
    'id' => 'special_price',
    'label' => __( 'Wholesaler Price *', 'woocommerce' ),
    'placeholder' => '',
    'desc_tip' => 'true',
    'custom_attributes' => array( 'required' => 'required' ),
    'description' => __( 'Enter wholesaler price here.', 'woocommerce' )
  )
);