WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. John
    Member
    Posted 5 months 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 5 months 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 5 months 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. mikejolley
    Member
    Plugin Author

    Posted 5 months 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 5 months 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 5 months ago #

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

  7. harpo1984
    Member
    Posted 5 months 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 5 months 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 5 months 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 5 months 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 5 months ago #

    Hi harpo1984,

    Nice example, thanks for sharing.

    Regards,
    Gabriel

  12. Peter Brock
    Member
    Posted 5 months 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 5 months ago #

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

  14. Gabriel Reguly
    Member
    Posted 5 months 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 5 months 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 4 months 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 4 months ago #

    Thanks harpo1984!

  18. Rafael Segat
    Member
    Posted 4 months ago #

    Thanks harpo1984!

  19. D-Fuzion
    Member
    Posted 4 months 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 3 months ago #

Reply

You must log in to post.

About this Plugin

About this Topic