在 WooCommerce 管理订单仪表板中更新客户密码

Kar*_*nak 7 php wordpress jquery orders woocommerce

我们有一个非常具体的情况,我想知道有人已经做了类似的事情。在 Woocommerce 订单上,我们使用订单邮件在 WP 中生成用户。然后我们的代理需要手动更改他们的密码,因为这个密码就像是他们内容的 PIN 码。我们通过传统邮件将这些 PIN 码装在信封中寄送。问题是代理需要手动从订单仪表板转到用户 - 找到正确的用户并更改他们的密码。有什么方法可以input field在订单仪表板上为链接到此订单的用户设置密码?

Loi*_*tec 5

这是一个完整的解决方案,它将在管理订单列表中添加一个带有按钮的自定义列(在每一行中),允许通过 Ajax 编辑用户密码。

在此处输入图片说明

然后当商店经理点击所需订单的按钮时,将出现一个输入文本字段,可以通过 Ajax 更新新密码。

在此处输入图片说明

将很快显示一条消息,显示此更新是否成功。

所以这里是完整的代码:

// Add a column to admin orders list
add_filter( 'manage_edit-shop_order_columns', 'add_edit_password_orders_column' );
function add_edit_password_orders_column( $columns ) {
    if( current_user_can( 'edit_shop_orders' ) ) {
        $sorted_columns = [];

        foreach ( $columns as $key => $value ) {
            if ( $key === 'order_total') {
                $sorted_columns[ 'edit_password' ] = __('Edit password', 'woocommerce');
            }
            $sorted_columns[$key] = $columns[$key];
        }

        return $sorted_columns;
    }
    return $columns;
}

// Column content
add_action( 'manage_shop_order_posts_custom_column', 'edit_password_orders_column_row_content' );
function edit_password_orders_column_row_content( $column ) {
    if ( 'edit_password' === $column ) {
        global $post, $the_order;

        $user = $the_order->get_user();

        if( is_a($user, 'WP_User') && $user->ID > 0 ) {
            echo '<div class="edit_password userid-' . $user->ID . '" data-user_id="' . $user->ID . '">
                <a class="button user-login" style="text-align:center; display:block;">' . __('Edit', 'woocommerce') . '</a>
                <div  class="hidden-fields" style="display:none;">
                    <input type="text" name="customer-password" class="customer-password" value="" style="width:auto; max-width:120px; margin-bottom: 6px;">
                    <input type="hidden" name="customer-id" class="customer-id" value="' . $user->ID . '">
                    <a class="button submit-password" style="text-align:center;">' . __('Ok', 'woocommerce') . '</a>
                </div>
                <div class="message-response" style="display:none;">Message</div>
            </div>';
        } else {
            echo __("Guest user", "woocommerce");
        }
    }
}

// Jquery Ajax
add_action( 'admin_footer', 'edit_password_orders_column_js' );
function edit_password_orders_column_js() {
    global $pagenow;

    if ( $pagenow === 'edit.php' && isset($_GET['post_type']) && 'shop_order' === $_GET['post_type'] ) :
    ?>
    <script type="text/javascript">
    jQuery( function($){
        $('div.edit_password > .button.user-login').on('click', function(e){
            e.preventDefault();

            $(this).hide('fast');
            $(this).parent().find('.hidden-fields').show('slow');
        });

        $(document.body).on('click focus focusin', 'div.edit_password input.customer-password', function(e){
            e.stopImmediatePropagation();
        });

        $('div.edit_password .button.submit-password').on('click', function(e){
            e.preventDefault();
            e.stopImmediatePropagation();

            var $this = $(this),
                $parent = $this.parent(),
                password = $parent.find('input.customer-password').val(),
                user_id  = $parent.find('input.customer-id').val(),
                text = '',
                color = 'red';

            $.ajax({
                type: 'POST',
                url: '<?php echo admin_url('/admin-ajax.php'); ?>',
                data: {
                    'action'           : 'updating_customer_password',
                    'customer-password': password,
                    'customer-id'      : user_id
                },
                success: function (response) {
                    if ( response === 'empty' ) {
                        text  = '<?php echo __('Empty input, retry…', 'woocommerce'); ?>';
                    } else if ( response === 'whitespace' ) {
                        text  = '<?php echo __('No white spaces…', 'woocommerce'); ?>';
                    } else {
                        text  = '<?php echo __('Updating password!', 'woocommerce'); ?>';
                        color = 'green';
                    }
                    $parent.find('input.customer-password').val('');
                    $parent.parent().find('.hidden-fields').hide('fast');
                    $parent.parent().find('div.message-response').css('color',color).html('<small>'+text+'<small>').show();
                    setTimeout(function(){
                        $parent.parent().find('div.message-response').css('color','black').html('').hide();
                        $parent.parent().find('a.user-login').show();
                    }, 2000);

                    console.log(response); // For testing (to be removed)
                },
                error: function (error) {
                    $this.parent().parent().find('.hidden-fields').hide('fast');
                    $this.parent().parent().find('div.message-response').html('Error!').css('color',color).show();
                    setTimeout(function(){
                        $parent.parent().find('div.message-response').css('color','black').html('').hide();
                        $parent.parent().find('a.user-login').show();
                    }, 2000);

                    console.log(error); // For testing (to be removed)
                }
            });
        });
    });
    </script>
    <?php
    endif;
}

// PHP Ajax receiver
add_action('wp_ajax_updating_customer_password', 'action_ajax_updating_customer_password');
function action_ajax_updating_customer_password() {
    if ( isset($_POST['customer-password']) && isset($_POST['customer-id']) ) {
        $password = sanitize_text_field( $_POST['customer-password'] );
        $user_id  = intval( esc_attr( $_POST['customer-id'] ) );

        if ( ! $password ) {
            echo 'empty'; // empty input
        } elseif ( strpos($password, ' ') !== false ) {
            echo 'whitespace'; // empty input
        } else {

            wp_set_password( $password, $user_id ); // Set new password
        }
        wp_die();
    }
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或活动主题)的 functions.php 文件中。测试和工作。


Loi*_*tec 4

这是另一种类似的方式,但这一次管理单个编辑订单页面上,右侧有一个自定义 Metabox,使用 Ajax 来更新客户密码。

\n

在此输入图像描述

\n

很快就会显示一条消息,显示此密码更新是否成功,或者是否不允许提交空密码或密码中包含空格。

\n

这是代码:

\n
// Adding a custom Metabox on WooCommerce single orders (on right side)\nadd_action( \'add_meta_boxes\', \'add_custom_shop_order_metabox\' );\nfunction add_custom_shop_order_metabox(){\n    global $post;\n\n    $customer_id = get_post_meta( $post->ID, \'_customer_user\', true );\n\n    if ( $customer_id > 0 ) {\n        add_meta_box(\n            \'custom_shop_order_metabox\',\n            __(\'Change password\', \'woocommerce\'),\n            \'content_custom_shop_order_metabox\',\n            \'shop_order\',\n            \'side\',\n            \'core\'\n        );\n    }\n}\n\n// Custom Metabox content on WooCommerce single orders\nfunction content_custom_shop_order_metabox() {\n    global $post;\n\n    $customer_id = get_post_meta( $post->ID, \'_customer_user\', true );\n\n    echo \'<div class="options_group edit_password" style="min-height:4.5em;margin-top:12px;;">\n        <input type="text" name="customer-password" class="customer-password" value="">\n        <a class="button submit-password" style="text-align:center;">\' . __(\'Submit\', \'woocommerce\') . \'</a>\n        <input type="hidden" name="customer-id" class="customer-id" value="\' . $customer_id . \'">\n        <div class="message-response" style="margin-top:6px;"></div>\n    </div>\';\n}\n\n// Jquery Ajax\nadd_action( \'admin_footer\', \'edit_password_orders_column_js\' );\nfunction edit_password_orders_column_js() {\n    global $pagenow, $post_type;\n\n    if ( \'post.php\' === $pagenow && \'shop_order\' === $post_type ) :\n    ?>\n    <script type="text/javascript">\n    jQuery( function($){\n        $(\'div.edit_password > .message-response\').fadeOut(0);\n        $(\'div.edit_password > .button.submit-password\').on(\'click\', function(e){\n            e.preventDefault();\n            e.stopImmediatePropagation();\n\n            var $this = $(this),\n                $parent = $this.parent(),\n                password = $parent.find(\'.customer-password\').val(),\n                user_id  = $parent.find(\'.customer-id\').val(),\n                text = \'\',\n                color = \'red\';\n\n            $.ajax({\n                type: \'POST\',\n                url: \'<?php echo admin_url(\'/admin-ajax.php\'); ?>\',\n                data: {\n                    \'action\'           : \'updating_customer_password\',\n                    \'customer-password\': password,\n                    \'customer-id\'      : user_id\n                },\n                success: function (response) {\n                    if ( response === \'empty\' ) {\n                        text  = \'<?php echo __(\'Empty input, retry\xe2\x80\xa6\', \'woocommerce\'); ?>\';\n                    } else if ( response === \'whitespace\' ) {\n                        text  = \'<?php echo __(\'No white spaces\xe2\x80\xa6\', \'woocommerce\'); ?>\';\n                    } else {\n                        text  = \'<?php echo __(\'Password Updated!\', \'woocommerce\'); ?>\';\n                        color = \'green\';\n                    }\n                    $parent.find(\'.customer-password\').val(\'\');\n                    $parent.find(\'.message-response\').html(\'<small>\'+text+\'<small>\').css(\'color\',color).fadeIn();\n                    setTimeout(function(){\n                        $parent.find(\'.message-response\').fadeOut( function(){ $(this).css(\'color\',\'black\').html(\'\'); });\n                    }, 2000)\n                }\n            });\n        });\n    });\n    </script>\n    <?php\n    endif;\n}\n\n// PHP Ajax receiver\nadd_action(\'wp_ajax_updating_customer_password\', \'action_ajax_updating_customer_password\');\nfunction action_ajax_updating_customer_password() {\n    if ( isset($_POST[\'customer-password\']) && isset($_POST[\'customer-id\']) ) {\n        $password = sanitize_text_field( $_POST[\'customer-password\'] );\n        $user_id  = intval( esc_attr( $_POST[\'customer-id\'] ) );\n\n        if ( ! $password ) {\n            echo \'empty\'; // empty input\n        } elseif ( strpos($password, \' \') !== false ) {\n            echo \'whitespace\'; // empty input\n        } else {\n            wp_set_password( $password, $user_id ); // Set new password\n        }\n        wp_die();\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。

\n