WordPress.org

Forums

WooCommerce - excelling eCommerce
[resolved] Custom order fields changed in WooCommerce 2.1 (23 posts)

  1. John
    Member
    Posted 1 year ago #

    In case it helps anyone else, it looks like WooCommerce 2.1 has changed the way it handles custom meta fields on orders. The one that I had set up to display on the checkout page is no longer displayed and the values cannot be retrieved as before.

    I spoke to WooThemes support about it and they said that the custom order fields are now stored as regular meta data, but could not offer any examples or documentation at this point as to how it works with orders.

    Does anyone know how this works in 2.1 now?

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

  2. DanielHolm
    Member
    Posted 1 year ago #

    This totaly broke my site so I'd like some help with this as well. Reported it as a bug[1]. They can't just change that without documenting how to change it.

    1: https://github.com/woothemes/woocommerce/issues/4746

  3. kaplunkiigirl
    Member
    Posted 1 year ago #

    You can use

    $my_order_meta = get_post_custom( $order_id );

    to return an array of all of the meta data for an order.

    (Make sure wherever you're using it also has the correct $order_id defined.)

    This is what it returns:

    Array
    (
        [_billing_first_name] => Array
            (
                [0] => Jane
            )
        [_billing_last_name] => Array
            (
                [0] => Doe
            )
        [_billing_company] => Array
            (
                [0] => Some Business
            )
        [_billing_my_custom_field] => Array
            (
                [0] => Custom field value
            )
    etc.

    Which you can then manipulate.

    e.g.

    $my_custom_field_value = $my_order_meta[_billing_my_custom_field][0];

    I'm not sure if it's the best way, but it will do for now.

  4. Mike Jolley
    Member
    Plugin Author

    Posted 1 year ago #

    Loading all the meta data as we did previously is totally unnecessary. Just because you made use of that data, doesn't make this a bug.

    You can use @kaplunkiigirl's solution above, a get_post_meta call, or even access it directly using $order->meta_key_name which WordPress supports as well.

  5. Bogdan
    Member
    Posted 1 year ago #

    Hi Mike,

    thanks for $order->meta_key_name, this works great.

    Still it would have been nice to give a hint somewhere for alle the developers that where using $order->order_custom_fields before.

    Bogdan

  6. John
    Member
    Posted 1 year ago #

    Thanks guys. I've re-worked my code and all is well now.

  7. harpo1984
    Member
    Posted 1 year ago #

    Hi guys,

    I am really struggling with this as well. My custom field still displays in the checkout page, but not on single order view in Orders of the WooCommerce 2.1 Dashboard.

    When updating to WooCommerce 2.1 and viewing an order I received this error code in the order:

    Fatal error: Cannot use string offset as an array in /var/sites/t/test.bearingbasement.com/public_html/wp-content/themes/canvas-child/functions.php on line 153

    The original custom function used to load the data into the order page was:

    /**
    * Display field value on the order edition page
    **/
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

    function my_custom_checkout_field_display_admin_order_meta($order){
    echo '<p>'.__('Trade Account No.').': ' . $order->order_custom_fields['My Field'][0] . '</p>';
    }

    Line 153 began at "echo".

    I then tried to follow the advice on this page and changed line 153 to:

    echo '<p>'.__('Trade Account No').': ' . $order->meta_key_name['My Field'][0] . '</p>';

    However, I still get the same error message.

    I discovered by deleting [0] from line 153 the order page would now load correctly and show the custom field. However, when placing a test order and completing the custom field at the checkout, the custom data will not show in the order, only the fields custom title shows.

    Please help.

    (The full original code I used for adding the field to the checkout page and the order page is below)

    // Hook in
    add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );

    // Our hooked in function - $address_fields is passed via the filter!
    function custom_override_default_address_fields( $address_fields ) {
    $address_fields['postcode']['required'] = false;

    return $address_fields;
    }

    /**
    * 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"><h3>'.__('Trade Accounts').'</h3>';

    woocommerce_form_field( 'my_field_name', array(
    'type' => 'text',
    'class' => array('my-field-class form-row-wide'),
    'placeholder' => __('Enter your Trade Account Number'),
    ), $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;
    }

    /**
    * 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']));
    }

    /**
    * Display field value on the order edition page
    **/
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

    function my_custom_checkout_field_display_admin_order_meta($order){
    echo '<p>'.__('Trade Account No.').': ' . $order->order_custom_fields['My Field'][0] . '</p>';
    }

  8. Peter Brock
    Member
    Posted 1 year ago #

    I realize this topic was resolved, but not for me or harpo1984.

    I have edited my code to to replace $order->order_custom_fields with $order->meta_key_name but I still don't see my custom fields on the order page or in the admin email.

    The fields look fine in checkout.

  9. Gabriel Reguly
    Member
    Posted 1 year ago #

    Hi folks,

    Another solution is using get_post_meta as in

    get_post_meta( $order->id, 'My Field', true )

    to replace

    $order->order_custom_fields['My Field'][0]

    Cheers,
    Gabriel

  10. harpo1984
    Member
    Posted 1 year ago #

    Hi Peter,

    I got things solved today. Here is the code you need.

    The meta key does need changed, but more needs changed as well, including references to POST and the call to the admin. Changing the meta key reference got things working in the admin, but the field doesn't update. I finally got things working today after a week with Woo support.

    I've pasted the code below that will get things working. Simply customise the name of your field or change the field type for your needs:

    /**
    * 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"><h3>'.__('Trade Accounts').'</h3>';
    
    woocommerce_form_field( 'my_field_name', array(
    'type' => 'text',
    'class' => array('my-field-class form-row-wide'),
    'placeholder' => __('Enter your Trade Account Number'),
    ), $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;
    }
    
    /**
    * Update the order meta with field value
    **/
    add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 2);
    function my_custom_checkout_field_update_order_meta( $order_id, $posted ) {
        if ( $_POST['my_field_name'] ) {
            update_post_meta( $order_id, '_MyField', esc_attr($_POST['my_field_name']));
        }
    }
    
    /**
    * Display field value on the order edition page
    **/
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 ); 
    
    function my_custom_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('Trade Account No').':</strong> ' . $order->MyField. '</p>';
    }

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

  11. Gabriel Reguly
    Member
    Posted 1 year ago #

    Hi harpo1984,

    Nice example, thanks for sharing.

    Regards,
    Gabriel

  12. Peter Brock
    Member
    Posted 1 year ago #

    Thank you harpo1984. You saved the day. My order details pane came back to life with your new code.

    However, I still had trouble with the last function. The Member Number label in the order details pane was there, but the actual number was missing. To fix that, I replaced this piece of code:

    function my_custom_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('Trade Account No').':</strong> ' . $order->MyField. '</p>';
    }

    with this:

    function my_custom_checkout_field_display_admin_order_meta($order){
        echo '<p><strong>'.__('Member Number').':</strong> ' . get_post_meta( $order->id, 'Member Number', true ). '</p>';
    }

    Obrigado to Gabriel Reguly for the solution.

  13. harpo1984
    Member
    Posted 1 year ago #

    Glad you got sorted Peter...cheers for the thumbs up Gabriel.

  14. Gabriel Reguly
    Member
    Posted 1 year ago #

    @Peter, de nada :-)

    Same code as yours, https://gist.github.com/woogist/6267983, in hindsight I should have placed it here too

    :-P

  15. simmonsstummer
    Member
    Posted 1 year ago #

    now i can't get the custom fields.
    It works for the past one but not for new customers...
    is there anything else changed in the "Update the order meta with field value"?

  16. ljmcwilliam
    Member
    Posted 1 year ago #

    I'm struggling to get this right too!

    I have 5 custom text fields I'm adding.

    What do you do for multiple custom fields?

    Thanks :)

  17. Rafael Segat
    Member
    Posted 1 year ago #

    Thanks harpo1984!

  18. Rafael Segat
    Member
    Posted 1 year ago #

    Thanks harpo1984!

  19. D-Fuzion
    Member
    Posted 1 year ago #

    I'm trying to add custom fields to the checkout page that will also show up on order emails. Does anyone have the required code to do this?

    Kind regards,

    Robert

  20. WebEndev
    Member
    Posted 1 year ago #

  21. dezahyner
    Member
    Posted 9 months ago #

    I am trying to add a select field to billing page.. I have successfully added, but it is not displaying the placeholder text and the first option is selected. Need it to show the placeholder so the user must make a selection. Am I missing something here?

    <?php
    /**
    * Add the field to the checkout
    **/
    add_action('woocommerce_before_order_notes', 'my_club_checkout_field');
    
    function my_club_checkout_field( $checkout ) {
    
    echo '<div id="my_local_club"><h3>'.__('Local Club').'</h3>';
    
    woocommerce_form_field( 'local_club', array(
    'type' => 'select',
    'class' => array('my-club-class form-row-wide'),
    'label'       			=> __('<br><br>Please Select your Local Club', 'woocommerce'),
    'required'    			=> true,
    'placeholder' => _x('Please Select An Option...', 'placeholder','woocommerce'),
        'options'			=> array(
            'option_a' 	=> __('Lees Summit', 'woocommerce' ),
            'option_b' 	=> __('Kansas City', 'woocommerce' )
         )
    ), $checkout->get_value( 'local_club' ));
    
    echo '</div>';
    
    }
    
    /**
    * Process the checkout
    **/
    add_action('woocommerce_checkout_process', 'my_club_checkout_field_process');
    
    function my_club_checkout_field_process() {
    global $woocommerce;
    }
    
    /**
    * Update the order meta with field value
    **/
    add_action('woocommerce_checkout_update_order_meta', 'my_club_checkout_field_update_order_meta');
    
    function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['local_club']) update_post_meta( $order_id, 'My Club', esc_attr($_POST['local_club']));
    }
    
    /**
    * Display field value on the order edition page
    **/
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_club_checkout_field_display_admin_order_meta', 10, 1 );
    
    function my_club_checkout_field_display_admin_order_meta($order){
    echo '<p>'.__('local_club').': ' . $order->order_custom_fields['My Club'][0] . '</p>';
    }
  22. vinsprinter
    Member
    Posted 8 months ago #

    Can I ask a question here? I am trying to source some coding/function in order to select the meta-data (i.e. name, address, product, qty, etc) from the order page excluding the prices and I want automate this page so that it automatically emails to a third party for shipping when the order is placed. Anybody got any tips or hints on how to do this. Thanks

  23. gasto
    Member
    Posted 8 months ago #

    Is there a way to make the "Additional Information" field mandatory, and also change its label and placeholder in Woocommerce 2.1?

    I used to that by simply modifying the line in class-wc-checkout.php file like this:

    $this->checkout_fields['order']	= array(
    			'order_comments' => array(
    				'type' => 'textarea',
    				'class' => array('notes'),
    				'required'          => true,
    				'label' => __( 'My label', 'woocommerce' ),
    				'placeholder' => _x('My placeholder', 'placeholder', 'woocommerce')
    				)
    			);

    Thanks in advance.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce - excelling eCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic