• Resolved halehaleomiyage

    (@halehaleomiyage)


    Please tell me how to solve my problem!!

    I try to customize checkout field of shipping address, and found following document.

    https://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/

    As the instruction says, I added two functions to my function.php under my child theme.
    First one is for override a placeholder (If I put this, the webpage gets blank…), and second one is to add a field.

    I updated woo commerce to 2.6.0 recently. Is that because it doesn’t work? Or my code is something wrong?

    // Hook in
    add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
    
    // Our hooked in function - $fields is passed via the filter!
    function custom_override_checkout_fields( $fields ) {
         $fields['shipping']['shipping_address_1']['placeholder'] = 'test';
         return $fields;
    }
    
    // Hook in
    add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
    
    // Our hooked in function - $fields is passed via the filter!
    function custom_override_checkout_fields( $fields ) {
         $fields['shipping']['shipping_hotel'] = array(
            'label'     => __('Hotel', 'woocommerce'),
        'placeholder'   => _x('Hotel name', 'placeholder', 'woocommerce'),
        'required'  => false,
        'class'     => array('form-row-wide'),
        'clear'     => true
         );
    
         return $fields;
    }

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been damaged by the forum’s parser.]

    https://wordpress.org/plugins/woocommerce/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    The snippets are fine. When adding custom code you should enable debugging to spot your mistakes. https://wordpress.org/support/topic/how-to-debug-issues-after-updates?replies=1

    Thread Starter halehaleomiyage

    (@halehaleomiyage)

    Thank you for your support.
    I enabled debugging and found why the screen was white. I was writing the function twice
    that’s why it became white.
    So I rewrote the code like this..

    // Hook in
    add_filter( ‘woocommerce_checkout_fields’ , ‘custom_override_checkout_fields’ );

    // Our hooked in function – $fields is passed via the filter!
    function custom_override_checkout_fields( $fields ) {
    $fields[‘shipping’][‘shipping_address_1’][‘placeholder’] = ‘test’;
    $fields[‘shipping’][‘shipping_hotel’] = array(
    ‘label’ => __(‘Hotel’, ‘woocommerce’),
    ‘placeholder’ => _x(‘Hotel name’, ‘placeholder’, ‘woocommerce’),
    ‘required’ => false,
    ‘class’ => array(‘form-row-wide’),
    ‘clear’ => true
    );

    return $fields;
    }

    It’s not white anymore but still doesn’t change any field which I override..
    Do you have any idea?

    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Hey,

    I reduced your snippet down to this:

    // Hook in
    add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
    
    // Our hooked in function - $fields is passed via the filter!
    function custom_override_checkout_fields( $fields ) {
    	$fields['shipping']['shipping_address_1']['placeholder'] = 'test';
    	return $fields;
    }

    And it works for me: http://cld.wthms.co/1hOYq/1mtWOGG3

    I’m thinking you have code in other places maybe overriding this, or maybe the theme / another plugin is?

    Try to get just the placeholder text to change (be sure to check the shipping fields, not billing). Narrow things down by disabling all plugins and switching to a default theme and add the code to the correct functions.php file. Once you have things working in your “sandbox”, you can go from there to find out why things aren’t working.

    Thread Starter halehaleomiyage

    (@halehaleomiyage)

    Hi Caleb, thank you for your advice. I followed your advice and tried everything like disabling all plugins and switching to a default theme…. Those didn’t change the situation but today I could customize the field finally somehow…. I don’t know why though.

    Anyways, now I could customize the field except ‘required’ for ‘shipping_address_1’.
    I want to change it to optional, but it is not allowed to do it?

    My snippet is like this.

    // Hook in Customize checkout fields
    add_filter( ‘woocommerce_checkout_fields’ , ‘custom_override_checkout_fields’ );

    // Our hooked in function – $fields is passed via the filter!
    function custom_override_checkout_fields( $fields ) {
    $fields[‘shipping’][‘shipping_address_1’][‘placeholder’] = ‘Hotel address’;
    $fields[‘shipping’][‘shipping_address_1’][‘label’] = ‘Address (Please put the address if there is no name on the list.)’;
    $fields[‘shipping’][‘shipping_address_1’][‘required’] = false;
    $fields[‘shipping’][‘shipping_address_2’][‘placeholder’] = ‘Room No.’;
    unset($fields[‘shipping’][‘shipping_company’]);
    unset($fields[‘billing’][‘billing_company’]);
    return $fields;
    }

    Thread Starter halehaleomiyage

    (@halehaleomiyage)

    Never mind… I could change to optional. Ha, I still don’t know why sometimes it doesn’t work and sometimes works….
    Trying my best.

    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Sounds like you were able to get this working correctly?

    Thread Starter halehaleomiyage

    (@halehaleomiyage)

    Yes. I could get it to work correctly. Thank you for your help!!!

    Hello,

    I have the save issue.
    I am using woocommerce version 2.6.6

    After debuggin I found out this hook fires after form dom rendered.

    Details:
    I var dumped the $fields variable in custom_override_checkout_fields (woocommerce_checkout_fields hook listner).
    It shows after the html of form field is rendered.

    Thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to customize checkout field?’ is closed to new replies.