gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:50:08 28 4
gpt4 key购买 nike

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

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

enter image description here

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

我试过在 wp-admin/post.php 中添加 $_POST['vendor']但是没有用.

这是我添加的代码:

    <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>

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

最佳答案

开发人员禁止覆盖核心文件。 所以这不是正确的做法。

这样做的方法是使用源代码中可用的钩子(Hook),而不是覆盖这个核心文件,因为当插件更新时你会丢失所有东西。

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

这里是替换代码 + 将数据保存到订单元数据的钩子(Hook):

add_action( 'woocommerce_admin_order_data_after_order_details', 'custom_code_after_order_details', 10, 1 );
function custom_code_after_order_details ( $order ) {
// Get custom field value from '_vendor' meta key
$value = $order->get_meta('_vendor');
?>
<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 ( in_array('vendor', $user_info->roles) ){
$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 文件中或任何插件文件中。

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

关于php - WooCommerce 管理员订单编辑保存帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45833977/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com