WooCommerce 管理员订单编辑保存帖子

Xie*_*hao 5 php wordpress custom-fields orders woocommerce

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

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

在此处输入图片说明

但我不知道如何捕捉或拯救 $_POST['vendor']

我尝试添加$_POST['vendor']wp-admin/post.php但它不起作用

这是我添加的代码:

    <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> 
Run Code Online (Sandbox Code Playgroud)

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

Loi*_*tec 4

覆盖核心文件对于开发人员来说是被禁止的。所以这不是正确的方法。

\n

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

\n
    \n
  1. 替换所有原来的核心文件
  2. \n
  3. 添加此代码(我做了一些必要的小更改)
  4. \n
\n

这是替换代码+一个将数据保存到订单元数据的钩子:

\n
add_action( 'woocommerce_admin_order_data_after_order_details', 'custom_code_after_order_details', 10, 1 );\nfunction custom_code_after_order_details ( $order ) {\n    // Get custom field value from '_vendor' meta key\n    $value = $order->get_meta('_vendor');\n    ?>\n    <p> <label for="order_status">\xe4\xbe\x9b\xe6\x87\x89\xe5\x95\x86\xef\xbc\x9a </label>\n    <select name="vendor">\n    <?php  global $wpdb;\n        $user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" );\n        echo '<option value="">Select a vendor</option>';\n        for ( $i=1; $i<=$user_count; $i++ ) {\n            $user_info = get_userdata($i);\n            if ( in_array('vendor', $user_info->roles) ){\n                $user_login = $user_info->user_login;\n                $selected = $value == $user_login ? 'selected' : '';\n                echo '<option '.$selected.' value="'.$user_login.'">'.$user_login.'</option>';\n            }\n        }\n    ?>\n    </select></p>\n    <input type="hidden" name="custom_select_field_nonce" value="<?php echo wp_create_nonce(); ?>">\n    <?php\n}\n\nadd_action( 'save_post', 'save_custom_code_after_order_details', 10, 1 );\nfunction save_custom_code_after_order_details( $post_id ) {\n\n    // We need to verify this with the proper authorization (security stuff).\n\n    // Check if our nonce is set.\n    if ( ! isset( $_POST[ 'custom_select_field_nonce' ] ) ) {\n        return $post_id;\n    }\n    $nonce = $_REQUEST[ 'custom_select_field_nonce' ];\n\n    //Verify that the nonce is valid.\n    if ( ! wp_verify_nonce( $nonce ) ) {\n        return $post_id;\n    }\n\n    // If this is an autosave, our form has not been submitted, so we don't want to do anything.\n    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {\n        return $post_id;\n    }\n\n    // Check the user's permissions.\n    if ( 'page' == $_POST[ 'post_type' ] ) {\n\n        if ( ! current_user_can( 'edit_page', $post_id ) ) {\n            return $post_id;\n        }\n    } else {\n\n        if ( ! current_user_can( 'edit_post', $post_id ) ) {\n            return $post_id;\n        }\n    }\n\n    // Update the meta field in the database.\n    update_post_meta( $post_id, '_vendor', $_POST[ 'vendor' ] );\n}\n
Run Code Online (Sandbox Code Playgroud)\n

代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。

\n

这段代码已经过测试并且可以工作。

\n