New Landing How can we help? Themeforest Theme Support Dante How to add a custom field in woocommerce checkout?

Viewing 8 posts - 1 through 8 (of 8 total)
  • Posted in: Dante
  • #109256
    adstream
    Member
    Post count: 85

    Hello,

    Im trying to add a custom woocommerce checkout field by adding this code to the dante-child theme functions.php. (Its taken from the woocommerce tutorial about the subject.)

    /**
    * Add the field to the checkout
    **/
    add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');
    
    function my_custom_checkout_field( $checkout ) {
    
    echo '<div id="my_custom_checkout_field"><h2>'.__('My Field').'</h2>';
    
    woocommerce_form_field( 'my_field_name', array(
    'type'          => 'text',
    'class'         => array('my-field-class form-row-wide'),
    'label'         => __('Fill in this field'),
    'placeholder'       => __('Enter something'),
    ), $checkout->get_value( 'my_field_name' ));
    
    echo '</div>';
    
    }
    
    /**
    * Process the checkout
    **/
    add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
    
    function my_custom_checkout_field_process() {
    global $woocommerce;
    
    // Check if set, if its not set add an error.
    if (!$_POST['my_field_name'])
    $woocommerce->add_error( __('Please enter something into this new shiny field.') );
    }
    
    /**
    * Update the order meta with field value
    **/
    add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
    
    function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));
    }

    But nothing happens when i refresh/clear the cache of the checkout page. How come? What am I doing wrong?

    #109374
    laranz – SUPPORT
    Member
    Post count: 3186

    HI,

    It should work, I put that in the functions.php and it works.. Can you try that in the parent theme functions.php http://take.ms/Ua6UO

    Let us know,

    Thanks,
    laranz.

    #109426
    adstream
    Member
    Post count: 85

    Hi laranz,

    If i put it in the parent theme functions.php everything gets blank which to me indicates there is something wrong in the php/code?

    This is how my entire child-theme functions look like:

    <?php
    	
    	/*
    	*
    	*	Dante Functions - Child Theme
    	*	------------------------------------------------
    	*	These functions will override the parent theme
    	*	functions. We have provided some examples below.
    	*
    	*
    	*/
    	
    
    /**
    * Add the field to the checkout
    **/
    add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');
    
    function my_custom_checkout_field( $checkout ) {
    
    echo '<div id="my_custom_checkout_field"><h2>'.__('My Field').'</h2>';
    
    woocommerce_form_field( 'my_field_name', array(
    'type'          => 'text',
    'class'         => array('my-field-class form-row-wide'),
    'label'         => __('Fill in this field'),
    'placeholder'       => __('Enter something'),
    ), $checkout->get_value( 'my_field_name' ));
    
    echo '</div>';
    
    }
    
    /**
    * Process the checkout
    **/
    add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
    
    function my_custom_checkout_field_process() {
    global $woocommerce;
    
    // Check if set, if its not set add an error.
    if (!$_POST['my_field_name'])
    $woocommerce->add_error( __('Please enter something into this new shiny field.') );
    }
    
    /**
    * Update the order meta with field value
    **/
    add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
    
    function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));
    }
    
    ?>
    #109475
    Mohammad – SUPPORT
    Moderator
    Post count: 27441
    This reply has been marked as private.
    #109571
    adstream
    Member
    Post count: 85
    This reply has been marked as private.
    #109985
    Mohammad – SUPPORT
    Moderator
    Post count: 27441

    Hi,
    You are using very old version 2.53 of theme so please upgrade to latest version 2.66 of theme. Hope that should resolve the issue easily.
    Thanks 🙂
    With Best Regards
    Mohammad

    #118900
    rixator
    Member
    Post count: 28

    Hello,
    Is it possible to have this field in the cart page?

    I need to have order notes on the cart page instead of on the checkout page.
    I tried to put in “add_action(‘woocommerce_after_cart_table'” but that didn’t work for me.

    #118914
    Rui Guerreiro – SUPPORT
    Keymaster
    Post count: 25779

    Hi,

    Adding checkout fields it’s a know request and several solutions in the web.
    Adding fields to the cart page it’s a different thing.

    You should contact Woocommerce support with this request.

    -Rui

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in and have valid license to reply to this topic.

License required for the following item
Login and Registration Log in · Register