【问题标题】:Save multiple select option editable fields in WooCommerce Admin Orders Page在 WooCommerce 管理订单页面中保存多个选择选项可编辑字段
【发布时间】:2019-04-03 05:45:06
【问题描述】:

在通过 Woocommerce 购买/结帐时,我的结帐代码包括 2 个隐藏字段,它们发送保存在 _dispatch_dispatch_driver 下的默认帖子元值。

这个帖子元数据提供了一个选择字段,其中包含订单项管理端的第一个值。

我有自定义帖子类型,其中包含应将选择选项字段作为其他选项提供的信息。

打开订单商品时,我的问题出在管理方面。

  1. 默认信息设置得很好,但我似乎可以在单击更新按钮时保存/更改第二个选择字段。
  2. 我无法将我已完成的 CPT 查询中的值提供给 woocommerce 选项字段。

部分代码来自:Custom editable field in Woocommerce admin edit order pages general section

add_action( 'woocommerce_checkout_update_order_meta', 'save_custom_checkout_field'), 10, 1 );
add_action('woocommerce_admin_order_data_after_order_details', 'editable_order_custom_field', 10, 1 );
add_action( 'woocommerce_process_shop_order_meta', 'save_order_custom_field_meta_data', 12, 2 );

//Then you will need to save this hidden field in the order, this way:

function save_custom_checkout_field( $order_id ) {

    if ( ! empty( $_POST['dispatch'] ) )
        update_post_meta( $order_id, '_dispatch', sanitize_text_field( $_POST['dispatch'] ) );

    if ( ! empty( $_POST['dispatch_driver'] ) )
        update_post_meta( $order_id, '_dispatch_driver', sanitize_text_field( $_POST['dispatch_driver'] ) );

}
// Output a custom editable field in backend edit order pages under general section
function editable_order_custom_field( $order ){
    // Loop through order items

    foreach( $order->get_items() as $item_id => $item ){
        // Get "customer reference" from order item meta data
        if( $item->get_meta('_dispatch') ){
            // The "customer reference"
            $item_value = $item->get_meta('_dispatch');

            // We output a hidden field with the Item ID (to be able to update this order item meta data later)
            echo '<input type="hidden" name="item_id" value="' . $item_id . '">';

            break; // We stop the loop
        }

        if( $item->get_meta('_dispatch_driver') ){
            // The "customer reference"
            $item_value_ref = $item->get_meta('_dispatch_driver');

            // We output a hidden field with the Item ID (to be able to update this order item meta data later)
            echo '<input type="hidden" name="item_id_ref" value="' . $item_value_ref . '">';

            break; // We stop the loop
        }
    }

    // Get "customer reference" from meta data (not item meta data)
    $dispatch_updated_value = $order->get_meta('_dispatch');
    $dispatch_driver_updated_value = $order->get_meta('_dispatch_driver');

    // Replace "dispatch reference" value by the meta data if it exist
    $dispatch_new_value = $dispatch_updated_value ? $dispatch_updated_value : ( isset($item_value) ? $item_value : '');
    $dispatch_driver_new_value = $dispatch_driver_updated_value ? $dispatch_driver_updated_value : ( isset($item_value_ref) ? $item_value_ref : '');

    $variable = $data_dispatches )[0];

    // Display the custom editable field
    woocommerce_wp_select( array(
        'id'            => 'dispatch',
        'label'         => __("Dispatch Reference:", "woocommerce"),
        'type'          => 'select',
        'options'       => array (
            //$variable
            $dispatch_new_value   => __( $dispatch_new_value, 'woocommerce' ),
            'Unassigned'  => __('Unassigned', 'woocommerce' ),
            'nagulu'    => __('Nagulu', 'woocommerce' ),
            'kamwokya'  => __('Kamwokya', 'woocommerce' ),
            'bukoto'    => __('Bukoto', 'woocommerce' )
            )
    ) );

    woocommerce_wp_select( array(
        'id'            => 'dispatch-driver',
        'label'         => __("Dispatch Driver:", "woocommerce"),
        'type'          => 'select',
        'options'       => array (  
            $dispatch_driver_new_value   => __( $dispatch_driver_new_value, 'woocommerce' ),
            'Unassigned'  => __('Unassigned', 'woocommerce' ),
            'kamwokya'  => __('Kamwokya', 'woocommerce' ),
            'bukoto'    => __('Bukoto', 'woocommerce' )
        ),
    ) );
}

// Save the custom editable field value as order meta data and update order item meta data
function save_order_custom_field_meta_data( $post_id, $post ){

    if( isset( $_POST[ 'dispatch' ] ) ){
        // Save "dispatch reference" as order meta data
        update_post_meta( $post_id, '_dispatch', sanitize_text_field( $_POST[ 'dispatch' ] ) );

        // Update the existing "dispatch reference" item meta data
        if( isset( $_POST[ 'item_id' ] ) )
            wc_update_order_item_meta( $_POST[ 'item_id' ], 'Dispatch No.', $_POST[ 'dispatch' ] );
    }

    if( isset( $_POST[ 'dispatch_driver' ] ) ){
        // Save "dispatch_driver reference" as order meta data
        update_post_meta( $post_id, '_dispatch_driver', sanitize_text_field( $_POST[ 'dispatch_driver' ] ) );

        // Update the existing "dispatch_driver reference" item meta data
        if( isset( $_POST[ 'item_id_ref' ] ) )
            wc_update_order_item_meta( $_POST[ 'item_id_ref' ], 'Dispatch Driver', $_POST[ 'dispatch_driver' ] );
    }
}     
}

这是在结帐阶段保存初始详细信息的代码

function register() {
    add_action( 'woocommerce_after_order_notes', array( $this, 'my_custom_checkout_field' ), 10, 1 );
    add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'save_custom_checkout_field'), 10, 1 );
}

//Then you will need to save this hidden field in the order, this way:
function my_custom_checkout_field( $checkout ) {

    $dispatch = 'Unassigned Dispatch No';
    $dispatch_driver = 'Unassigned Driver';

    // Output the hidden link
    echo '
        <div id="dispatch_checkout_field">
                <input type="hidden" class="input-hidden" name="dispatch" id="dispatch" value="' . $dispatch . '">
        </div>
        <div id="dispatch_driver_checkout_field">
                <input type="hidden" class="input-hidden" name="dispatch_driver" id="dispatch_driver" value="' . $dispatch_driver . '">
        </div>
    ';
}

//Then you will need to save this hidden field in the order, this way:

function save_custom_checkout_field( $order_id ) {

    if ( ! empty( $_POST['dispatch'] ) )
        update_post_meta( $order_id, '_dispatch', sanitize_text_field( $_POST['dispatch'] ) );

    if ( ! empty( $_POST['dispatch_driver'] ) )
        update_post_meta( $order_id, '_dispatch_driver', sanitize_text_field( $_POST['dispatch_driver'] ) );

}

【问题讨论】:

  • 我已经简化为问题的核心
  • 我已经添加了上面的代码

标签: wordpress woocommerce


【解决方案1】:

由于您的结帐字段是具有相同值的隐藏字段,因此您不需要它们。您只需将它们直接保存为具有“未分配”值的订单元数据。现在有一个更好的钩子:

add_action( 'woocommerce_checkout_create_order', 'save_custom_checkout_field', 10, 2 );
function save_custom_checkout_field( $order, $data ) {
    $order->update_meta_data( '_dispatch', 'unassigned' );
    $order->update_meta_data( '_dispatch_driver', 'unassigned' );
}

现在您的第二个选择字段没有被保存,因为选择字段 slug 中存在错误……我还删除了所有不需要的代码并进行了一些更改:

// Output a custom editable field in backend edit order pages under general section
add_action('woocommerce_admin_order_data_after_order_details', 'editable_order_custom_field', 10, 1 );
function editable_order_custom_field( $order ){
    // Display the custom editable field
    woocommerce_wp_select( array(
        'id'            => '_dispatch',
        'label'         => __("Dispatch Reference:", "woocommerce"),
        'type'          => 'select',
        'options'       => array (
            'unassigned'    => __('Unassigned Dispatch No', 'woocommerce' ),
            'nagulu'        => __('Nagulu', 'woocommerce' ),
            'kamwokya'      => __('Kamwokya', 'woocommerce' ),
            'bukoto'        => __('Bukoto', 'woocommerce' )
        ),
    ) );

    woocommerce_wp_select( array(
        'id'            => '_dispatch_driver',
        'label'         => __("Dispatch Driver:", "woocommerce"),
        'type'          => 'select',
        'options'       => array (
            'unassigned'    => __('Unassigned Driver', 'woocommerce' ),
            'kamwokya'      => __('Kamwokya', 'woocommerce' ),
            'bukoto'        => __('Bukoto', 'woocommerce' )
        ),
    ) );
}

// Save the custom editable field value as order meta data and update order item meta data
add_action( 'woocommerce_process_shop_order_meta', 'save_order_custom_field_meta_data', 12, 2 );
function save_order_custom_field_meta_data( $post_id, $post ){
    if( isset( $_POST[ '_dispatch' ] ) ){
        update_post_meta( $post_id, '_dispatch', esc_attr( $_POST[ '_dispatch' ] ) );
    }
    if( isset( $_POST[ '_dispatch_driver' ] ) ){
        update_post_meta( $post_id, '_dispatch_driver', esc_attr( $_POST[ '_dispatch_driver' ] ) );
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

【讨论】:

  • 这看起来不错。循环遍历我的 CPT 查询以提供选择选项的部分怎么样?
猜你喜欢
  • 2019-06-21
  • 1970-01-01
  • 1970-01-01
  • 2019-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-01
  • 2018-07-02
相关资源
最近更新 更多