WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
dropdown in the checkout (2 posts)

  1. infobuster
    Member
    Posted 8 months ago #

    Hi there hoping someone can help here as i've created a drop down box in the checkout all seems to work ok however i want it to be a required field i know i can use the
    required = true line but as it already has an option selected as it just uses the first option on the list i have tried using a blank field but no joy

    here is my code so far as i'm sure other would want to use this

    echo '<div id="my_custom_checkout_field"><h3>'.__('How you found us').'</h3>';
    
        woocommerce_form_field ( 'my_field_name', array(
    
    	    'type'          => 'select',
    		'class'         => array('my-field-class form-row-wide'),
            'label'         => __('Choose an option'),
            'placeholder'   => _x('Phone', 'placeholder', 'woocommerce'),
    
    		'options'     => array(
    	 	'option_a' => __( '','' ),
            'option_b' => __('Google', 'Google' ),
            'option_c' => __('Medical referral', 'Medical_referral' ),
    		 'option_d' => __('Snorelab mobile app', 'Snorelab_mobile_app' ),
            'option_e' => __('Bing', 'Bing' ),
    		'option_f' => __('Yahoo', 'Yahoo' ),
            'option_g' => __('Leaflet', 'Leaflet' ),
    		 'option_h' => __('Newsletter', 'Newsletter' ),
            'option_i' => __('Other', 'Other' ),
            ),
    		'required'    => true,
    
            ), $checkout->get_value( 'my_field_name' ));

    http://wordpress.org/plugins/woocommerce/

  2. Harish Chouhan
    Member
    Posted 3 months ago #

    I might be replying late, but I use something like

    'blank' => __('Click here to select', 'dot'),

    After that you need to check if the field is set and the first option is not selected, otherwise you can display an error.

    add_action('woocommerce_checkout_process', 'dot_refereed_by_checkout_field_process');
    
    	function dot_refereed_by_checkout_field_process() {
    		global $woocommerce;
    
    		// Check if set, if its not set add an error.
    			if (!$_POST['refereed_by'] || $_POST['refereed_by'] == "blank") {
    				$woocommerce->add_error( __('Refereed By is a required field. Please select a option for How did you hear about us?') );
    			}
    	}

Reply

You must log in to post.

About this Plugin

About this Topic