harpo1984
Forum Replies Created
-
Forum: Plugins
In reply to: [WooCommerce] Custom order fields changed in WooCommerce 2.1Hi 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>’;
}