WooCommerce管理员订单编辑保存帖子 [英] WooCommerce admin order edit save post

查看:105
本文介绍了WooCommerce管理员订单编辑保存帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,当我提交时,如何捕获在订单编辑管理页面中添加的自定义选择字段?

In WooCommerce, when I submit, how to catch a custom select field added in the order edit admin pages?

我已在文件中添加了此自定义选择字段 class-wc-meta-box-order-data.php 。我明白了:

I have added this custom select field in the file class-wc-meta-box-order-data.php. I get this:

但我不知道如何捕获或保存 $ _ POST ['vendor']

But I dont know how to catch or to save $_POST['vendor']

我尝试添加<$ c $ wp-admin / post.php 中的c> $ _ POST ['vendor'] ,但不起作用

I have tried to add $_POST['vendor'] in wp-admin/post.phpbut it doesn't work.

这是我添加的代码:

    <select class="wc-customer-search" id="customer_user" name="customer_user" data-placeholder="<?php esc_attr_e( 'Guest', 'woocommerce' ); ?>" data-allow_clear="true">
                                <option value="<?php echo esc_attr( $user_id ); ?>" selected="selected"><?php echo htmlspecialchars( $user_string ); ?></option>
                            </select>
                            <!--/email_off-->
                        </p>
                   <p> <label for="order_status">供應商: </label>
                    <select name="vendor">
  <?php 
           global $wpdb;
   $user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" );
    for($i=1;$i<=$user_count;$i++){
    $user_info = get_userdata($i);

    if (implode(', ', $user_info->roles)=='vendor')
echo "<option value=".$user_info->user_login.">$user_info->user_login</option>";
    }
                    ?>
</select></p> 

如何获取提交的值以及如何保存?

How can I get the submitted value and how can I save it?

推荐答案

开发人员禁止重写核心文件。 所以这不是正确的方法。

Overriding core files is something prohibited for developers. So this is not the correct way to do it.

做到这一点的方法是使用源代码中的可用钩子,而不是覆盖此核心文件,因为在更新插件时会丢失所有内容。

The way to do it is using the available hooks in the source code, instead of overriding this core files, as you will loose everything when the plugin will be updated.


  1. 替换所有原始核心文件

  2. 改为添加此代码(我做了一些小的必要更改)

  1. Replace all original core files
  2. Add this code instead (I have make some minor necessary changes).

此处是替换代码+用于将数据保存到订单元数据的钩子:

Here is the replacement code + a hook to save the data to the order meta data:

add_action( 'woocommerce_admin_order_data_after_order_details', 'custom_code_after_order_details', 10, 1 );
function custom_code_after_order_details ( $order ) {


    $value = get_post_meta( $order->get_id(), '_vendor', true );
    ?>
    <p> <label for="order_status">供應商: </label>
    <select name="vendor">
    <?php  global $wpdb;
        $user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" );
        echo '<option value="">Select a vendor</option>';
        for($i=1;$i<=$user_count;$i++){
            $user_info = get_userdata($i);
            if (implode(', ', $user_info->roles)=='customer'){
                $user_login = $user_info->user_login;
                $selected = $value == $user_login ? 'selected' : '';
                echo '<option '.$selected.' value="'.$user_login.'">'.$user_login.'</option>';
            }
        }
    ?>
    </select></p>
    <input type="hidden" name="custom_select_field_nonce" value="<?php echo wp_create_nonce(); ?>">
    <?php
}

add_action( 'save_post', 'save_custom_code_after_order_details', 10, 1 );
function save_custom_code_after_order_details( $post_id ) {

    // We need to verify this with the proper authorization (security stuff).

    // Check if our nonce is set.
    if ( ! isset( $_POST[ 'custom_select_field_nonce' ] ) ) {
        return $post_id;
    }
    $nonce = $_REQUEST[ 'custom_select_field_nonce' ];

    //Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $nonce ) ) {
        return $post_id;
    }

    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    // Check the user's permissions.
    if ( 'page' == $_POST[ 'post_type' ] ) {

        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return $post_id;
        }
    } else {

        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return $post_id;
        }
    }

    // Update the meta field in the database.
    update_post_meta( $post_id, '_vendor', $_POST[ 'vendor' ] );
}

代码包含在您活动的子主题的function.php文件中(

此代码已经过测试并且可以正常工作。

This code is tested and works.

这篇关于WooCommerce管理员订单编辑保存帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆