首页 > 解决方案 > WooCommerce:: id 被错误地称为自定义结帐字段的错误

问题描述

我正在尝试显示我在 functions.php 中为 WooCommerce 创建的自定义结帐字段,但出现此错误(以及实际的自定义字段文本,如下所示:

Notice: id was called incorrectly. Order properties should not be accessed directly. Backtrace: include('wp-admin/edit-form-advanced.php'), do_meta_boxes, WC_Meta_Box_Order_Data::output, do_action('woocommerce_admin_order_data_after_billing_address'), WP_Hook->do_action, WP_Hook->apply_filters, cdm_custom_checkout_field_display_admin_order_meta, WC_Abstract_Legacy_Order->__get, wc_doing_it_wrong Please see Debugging in WordPress for more information. (This message was added in version 3.0.) in /www/webroot/cafed/wordpress/wp-includes/functions.php on line 4778

这是生成此错误的代码。

add_action( 'woocommerce_admin_order_data_after_billing_address',     'so_custom_checkout_field_display_admin_order_meta', 10, 1 );

function so_custom_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('Gift Message').':</strong> ' . get_post_meta( $order->id, 'Gift Message', true ) . '</p>';
}

add_filter('woocommerce_email_order_meta_keys', 'my_custom_order_meta_keys');

function my_custom_order_meta_keys( $keys ) {
     $keys[] = 'Gift Message';
     return $keys;
}

我相信这与 Woo 代码的更新有关,但我不确定如何更改它。

标签: woocommerce

解决方案


我以前做过的类似的事情正在为我工​​作

/**
* Add the field to the checkout
*/
add_action('woocommerce_before_order_notes', 'my_custom_checkout_field');

function my_custom_checkout_field( $checkout ) {

echo '<div id="my_custom_checkout_field"><h5>'.__('Time Slot: ').'</h5>';
echo '<h6>'.__('Every Day 09:00AM - 05:00PM').'</h6>';
woocommerce_form_field( 'gv_time_slot', array(
'type'          => 'hidden',
'class'         => array('my-field-class form-row-wide woocommerce-validated'),
'label'         => __('Every Day 04:00Pm - 09:00PM'),
 'required'      => false,
'placeholder'       => __('Check this'),
), $checkout->get_value( 'gv_time_slot' ));

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['gv_time_slot'])
$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['gv_time_slot']) update_post_meta( $order_id, 'Time Slot', esc_attr($_POST['gv_time_slot']));
}


// display the extra data on order recieved page and my-account order review
function sri_display_order_data( $order_id ){  ?>
    <h2><?php _e( 'Time Slot' ); ?></h2>
    <table class="shop_table shop_table_responsive additional_info">
        <tbody>
            <tr>
                <th><?php _e( 'Your Order will be delivered between' ); ?></th>
                <td><?php echo 'Every Day 09:00AM - 05:00PM'; ?></td>
            </tr>
            <tr>
                <th><?php _e( '*Please Note:' ); ?></th>
                <td><?php echo 'All orders placed before 3pm in the website/WhatsApp will be delivered on the same day. Orders after 3 pm will be moved to next day.'; ?></td>
            </tr>
        </tbody>
    </table>
<?php }
add_action( 'woocommerce_thankyou', 'sri_display_order_data', 20 );
add_action( 'woocommerce_view_order', 'sri_display_order_data', 20 );

推荐阅读