我正在做一个Electron 商务项目.当客户购买产品时,会根据他们的名字、姓氏和序列号为他们分配一个客户代码.它被会计软件使用.

以下是我的代码:

add_action( 'show_user_profile', 'ipx_user_profile_fields' );
add_action( 'edit_user_profile', 'ipx_user_profile_fields' );

function ipx_user_profile_fields( $user ) { ?>
    <h3><?php _e("IPX", "blank"); ?></h3>

    <table class="form-table">
    <tr>
        <th><label for="ipx_customer_code"><?php _e("Customer Code"); ?></label></th>
        <td>
            <input type="text" name="ipx_customer_code" id="ipx_customer_code" value="<?php echo esc_attr( get_the_author_meta( 'ipx_customer_code', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e("This field should contain only the IPX Customer Code."); ?></span>
        </td>
    </tr>
    </table>
<?php }

add_action( 'personal_options_update', 'save_ipx_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_ipx_user_profile_fields' );

function save_ipx_user_profile_fields( $user_id ) {
    if ( !current_user_can( 'edit_user', $user_id ) ) { 
        return false; 
    }
    update_user_meta( $user_id, 'ipx_customer_code', $_POST['ipx_customer_code'] );
}

/**
 * Generate customer code based on user's first name, last name and a sequential number
 */
function generate_customer_code($user_id) {
    $user = get_userdata($user_id);
    $first_name = $user->first_name;
    $last_name = $user->last_name;

    // Get the current sequential number for the customer code
    $sequential_number = get_user_meta($user_id, 'sequential_customer_code', true);
    if (!$sequential_number) {
        $sequential_number = 0;
    }

    // Loop until a unique customer code is generated
    do {
        $sequential_number++;
        // Generate the customer code
        $customer_code = strtoupper(substr($first_name, 0, 4) . substr($last_name, 0, 4) . sprintf('%02d', $sequential_number));
        // Check if the customer code already exists
        $user_query = new WP_User_Query(array(
            'meta_key' => 'ipx_customer_code',
            'meta_value' => $customer_code
        ));
    } while (!empty($user_query->results));

    // Save the customer code and sequential number as separate custom fields
    update_user_meta($user_id, 'ipx_customer_code', $customer_code);
    update_user_meta($user_id, 'sequential_customer_code', $sequential_number);
}
add_action('user_register', 'generate_customer_code');

它判断客户代码是否已被使用,如果已使用,则(按顺序)移动到下一个代码.

我一直在测试这一点,遇到了一个问题.只有当客户创建帐户时,它才起作用.我的客户坚持认为访客访问应该保持打开状态,这意味着永远不会生成客户代码.

有没有办法将此信息存储在wp_wc_Customer_Lookup表中?它记录了所有的客户,无论他们是否已经注册.我认为我的客户仍然希望在某个地方看到客户代码(在WooCommerce的Customers选项卡上?),但它需要针对每个客户,而不仅仅是注册客户.

任何 idea 都将不胜感激.对于任何不可靠的编码,我提前道歉-我的PHP很粗糙,但我宁愿try 也不愿不try .

推荐答案

这里的 idea 是,当Guest用户在您的store 上进行购买时注册他们.

  • 首先,我们向WordPress添加一个"Guest"用户角色(第一个函数).
  • 然后,一旦订单被处理(意味着订单被创建并保存到数据库中,就在付款之前),我们挂钩一个函数,在该函数中,我们使用WooCommerce也使用的WordPress函数wp_insert_user()将用户注册为"Guest".

现在,由于您的最后一个生成"客户代码"的函数被挂接在由wp_insert_user()精确触发的user_register WordPress钩子中,所以该函数将被执行,这就解决了您的问题.

代码(未经测试):

// Add "guest" user role
add_action( 'init', 'add_role_guest' );
function add_role_guest() {
    add_role(
        'guest',
        __( 'Guest', 'woocommerce' ),
        array( 'read' => true )
    );
}

// Registering unregistered Guest users
add_action( 'woocommerce_checkout_order_processed', 'process_checkout_guest', 10, 3 );
function process_checkout_guest( $order_id, $posted_data, $order ) {
    $user_id = $order->get_user_id(); // Get user Id (for guest the value is 0)
    $b_email = $order->get_billing_email(); // Get email from order

   // Only Guest
    if ( ! $user_id ) {
        // check with th email if WP_User exist
        $user = get_user_by( 'email', $b_email );

        // WP_User don't exist
        if ( $b_email && ! $user ) {
            // Generating the username
            $username = wc_create_new_customer_username( $b_email, array(
                'first_name' => $order->get_billing_first_name(),
                'last_name' => $order->get_billing_last_name(),
            ) );

            // Creating user
            $user_id = wp_insert_user( array(
                'user_login' => $username,
                'user_pass'  => wp_generate_password(),
                "first_name" => $order->get_billing_first_name(),
                'last_name'  => $order->get_billing_last_name(),
                'user_email' => sanitize_email($b_email),
                'role'       => 'guest', // Custom user role
            ) );
            // For testing
            if ( is_wp_error( $user_id ) ) {
                error_log( print_r($user_id, true) );
            }
        } 
    }
}

应该能行得通.

这需要测试,以确保没有与WooCommerce的不良互动.

我认为这是最好也是最简单的方法.

Php相关问答推荐

输入手写CSV时在PHP中进行日期判断

向在添加到购物车上动态创建的WooCommerce产品添加发货类别

Nginx重写扩展名为.php的动态URL

在AJAX请求后,Laravel重定向.(不允许使用方法)

根据未用于变体的产品属性隐藏WooCommerce发货方式

是否重新排序多维数组元素以将所有子数组中的子数组移动到元素列表的底部?

如何在汇总表构建器中操作LALAVEL细丝Sum::Make()值

使用ESI的Symfony Sulu清漆

从两个日期创建日期范围,然后为范围中的每个日期复制子数组

在WooCommercestore 页面上显示库存产品属性的值

将自定义字段添加到管理产品 在 WooCommerce 3.2+ 中批量编辑

Wordpress,配置一周第一天选项

PHP:数组的等边

由于 PHP 版本不受支持,如何终止 PHP 脚本?

在PHP WordPress插件中使用变量连接路径时的问题解决方法

如何用 php ML 解决这个问题?

在 WordPress 中使用 $wpdb 运行 MySQL 事务是否安全?

Docker: unixodbc.h 没有这样的文件或目录.已安装 unixodbc-dev 时出现pecl install sqlsrv错误

使用 splat 运算符时按引用传递 (...)

Google Drive - 仅使用 FileID 获取文件的 FileSize