在结帐中添加自定义字段,并在WooCommerce管理订单页面中显示它

问题描述 投票:1回答:1

[在WooCommerce中,我正在尝试使用"Customizing checkout fields using actions and filters"官方文档添加自定义字段以进行结帐。因此,我可以看到该字段,并且效果很好,但是问题是当我尝试在管理面板中查看客户输入的信息时。

我正在使用simply show hooks查看触发了哪些钩子,依此类推。而且我看不到woocommerce_admin_order_data_after_shipping_address触发。我想念的是什么?!这是我在functions.php文件中的代码:

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
     unset($fields['billing']['billing_address_2']);
     unset($fields['shipping']['shipping_address_2']);
          $fields['billing']['shipping_time'] = array(
            'type' => 'select',
        'label'     => __('Laikas', 'woocommerce'),
    'placeholder'   => _x('Laikas', 'placeholder', 'woocommerce'),
    'required'  => true,
    'class'     => array('form-row-wide'),
    'clear'     => true,
    'options' => array(
        'option_1' => 'nesvarbu',
        'option_2' => '8-12',
        'option_3' => '12-16',
        'option_4' => '16-20'
        )
     );


     return $fields;
}

add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
    global $post_id;
    $order = new WC_Order( $post_id );
    echo '<p><strong>'.__('Laikas').':</strong> ' . get_post_meta($order->get_id(), '_shipping_ftime', true ) . '</p>';
}
php wordpress woocommerce hook-woocommerce orders
1个回答
0
投票

enter image description here

add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
    unset( $fields[ 'billing' ][ 'billing_address_2' ] );
    unset( $fields[ 'shipping' ][ 'shipping_address_2' ] );
    $fields[ 'billing' ][ 'shipping_time' ] = array(
        'type'           => 'select',
        'label'          => __( 'Laikas', 'woocommerce' ),
        'placeholder'    => _x( 'Laikas', 'placeholder', 'woocommerce' ),
        'required'       => true,
        'class'          => array( 'form-row-wide' ),
        'clear'          => true,
        'options'        => array(
            'option_1'   => 'nesvarbu',
            'option_2'   => '8-12',
            'option_3'   => '12-16',
            'option_4'   => '16-20'
        )
    );


    return $fields;
}

add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta( $order ) {
    global $post_id;
    $order = new WC_Order( $post_id );
    $options         = array(
            'option_1'   => 'nesvarbu',
            'option_2'   => '8-12',
            'option_3'   => '12-16',
            'option_4'   => '16-20'
        );
    echo '<p><strong>' . __( 'Laikas' ) . ':</strong> ' . $options[get_post_meta( $order->get_id(), '_shipping_time', true )] . '</p>';
}

请尝试此代码段。

© www.soinside.com 2019 - 2024. All rights reserved.