WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Custom order fields changed in WooCommerce 2.1

[Resolved] Custom order fields changed in WooCommerce 2.1

Viewing 15 replies - 1 through 15 (of 22 total)
  • DanielHolm
    Member

    @danielholm

    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

    kaplunkiigirl
    Participant

    @kaplunkiigirl

    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.

    Plugin Author Mike Jolley
    Participant

    @mikejolley

    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.

    Bogdan
    Member

    @bogdanguenther

    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

    John
    Member

    @johnmcole

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

    harpo1984
    Participant

    @harpo1984

    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>’;
    }

    Peter Brock
    Member

    @peter-brock

    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.

    Gabriel Reguly
    Participant

    @gabriel-reguly

    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

    harpo1984
    Participant

    @harpo1984

    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.]

    Gabriel Reguly
    Participant

    @gabriel-reguly

    Hi harpo1984,

    Nice example, thanks for sharing.

    Regards,
    Gabriel

    Peter Brock
    Member

    @peter-brock

    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.

    harpo1984
    Participant

    @harpo1984

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

    Gabriel Reguly
    Participant

    @gabriel-reguly

    @peter, de nada 🙂

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

    😛

    simmonsstummer
    Member

    @simmonsstummer

    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”?

    ljmcwilliam
    Member

    @ljmcwilliam

    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 🙂

Viewing 15 replies - 1 through 15 (of 22 total)
  • The topic ‘[Resolved] Custom order fields changed in WooCommerce 2.1’ is closed to new replies.