客户所花费的总订单和金额在Woocomerce中没有更新

时间:2019-12-18 07:44:42

标签: wordpress woocommerce hook-woocommerce

我正在使用以下代码将来宾注册为Woocomerce / Wordpress的客户。但是,我面临的问题是单个客户的总订单数和总花费没有更新:-

// Register guest customers as users in wordpress
function wc_register_guests( $order_id ) {
  // get all the order data
  $order = new WC_Order($order_id);

  //get the user email from the order
    $order_phone = $order->billing_phone;
    $first_name = $order->billing_first_name;
    $last_name = $order->billing_last_name;

  // check if there are any users with the billing phone as user
  $user = username_exists( $order_phone );

  // if the UID is null, then it's a guest checkout
  if( $user == false ){

    // random password with 12 chars
    $random_password = wp_generate_password();

    // create new user with email as username &  newly created pw
    $user_id = wp_create_user( $order_phone, $random_password, $order_phone );
    wp_update_user( 
        array( 
            'ID' => $user_id, 
            'first_name' => $first_name,
            'last_name' => $last_name,
            'role' => get_option('default_role')
        ) 
    );

    //WC guest customer identification
    update_user_meta( $user_id, 'guest', 'yes' );

    //user's billing data
    update_user_meta( $user_id, 'billing_address_1', $order->billing_address_1 );
    update_user_meta( $user_id, 'billing_address_2', $order->billing_address_2 );
    update_user_meta( $user_id, 'billing_city', $order->billing_city );
    update_user_meta( $user_id, 'billing_company', $order->billing_company );
    update_user_meta( $user_id, 'billing_country', $order->billing_country );
    update_user_meta( $user_id, 'billing_email', $order->billing_email );
    update_user_meta( $user_id, 'billing_first_name', $order->billing_first_name );
    update_user_meta( $user_id, 'billing_last_name', $order->billing_last_name );
    update_user_meta( $user_id, 'billing_phone', $order->billing_phone );
    update_user_meta( $user_id, 'billing_postcode', $order->billing_postcode );
    update_user_meta( $user_id, 'billing_state', $order->billing_state );

    // user's shipping data
    update_user_meta( $user_id, 'shipping_address_1', $order->shipping_address_1 );
    update_user_meta( $user_id, 'shipping_address_2', $order->shipping_address_2 );
    update_user_meta( $user_id, 'shipping_city', $order->shipping_city );
    update_user_meta( $user_id, 'shipping_company', $order->shipping_company );
    update_user_meta( $user_id, 'shipping_country', $order->shipping_country );
    update_user_meta( $user_id, 'shipping_first_name', $order->shipping_first_name );
    update_user_meta( $user_id, 'shipping_last_name', $order->shipping_last_name );
    update_user_meta( $user_id, 'shipping_method', $order->shipping_method );
    update_user_meta( $user_id, 'shipping_postcode', $order->shipping_postcode );
    update_user_meta( $user_id, 'shipping_state', $order->shipping_state );

    // link past orders to this newly created customer
    wc_update_new_customer_past_orders( $user_id );

    $count = wc_update_new_customer_past_orders( $user_id );
    update_user_meta( $user_id, '_wc_linked_order_count', $count );
  }
}

//add this newly created function to the thank you page
add_action( 'woocommerce_thankyou', 'wc_register_guests', 10, 1 );

这是我的功能,用于查看WordPress用户单独列中的总订单计数和已花费总额,这两个列中的用户名均显示为“ 0”:-

/ Add columns for Total Orders and Total Amount Spent on Users page
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {

    function add_user_details_columns($columns) {
        $columns['user_orders'] = 'Orders';
        $columns['user_total_spent'] = 'Total Spent';
        return $columns;
    }

    function show_user_details_column_content($value, $column_name, $user_id) {
        if ('user_orders' == $column_name)
            return wc_get_customer_order_count($user_id);
        else if ('user_total_spent' == $column_name)
            return wc_price(wc_get_customer_total_spent($user_id));
        return $value;
    }

    function add_order_details_to_user_list() {
        add_filter('manage_users_columns', 'add_user_details_columns');
        add_action('manage_users_custom_column', 'show_user_details_column_content', 10, 3);
    }

    add_action('admin_init', 'add_order_details_to_user_list');

    add_filter( 'manage_users_sortable_columns', 'my_sortable_cake_column' );
    function my_sortable_cake_column( $columns ) {
        $columns['user_orders'] = '_order_count';
        $columns['user_total_spent'] = '_money_spent';
        return $columns;
    }

    add_action('pre_user_query', 'status_column_orderby');

    function status_column_orderby($userquery){

        if('_order_count'==$userquery->query_vars['orderby']) {
            global $wpdb;
            $userquery->query_from .= " LEFT OUTER JOIN $wpdb->usermeta AS alias ON ($wpdb->users.ID = alias.user_id) "; //note use of alias
            $userquery->query_where .= " AND alias.meta_key = '_order_count' "; //which meta are we sorting with?
            $userquery->query_orderby = " ORDER BY alias.meta_value + 0 ".($userquery->query_vars["order"] == "ASC" ? "asc " : "desc ");//set sort order
        } else if('_money_spent'==$userquery->query_vars['orderby']) {
            global $wpdb;
            $userquery->query_from .= " LEFT OUTER JOIN $wpdb->usermeta AS alias ON ($wpdb->users.ID = alias.user_id) "; //note use of alias
            $userquery->query_where .= " AND alias.meta_key = '_money_spent' "; //which meta are we sorting with?
            $userquery->query_orderby = " ORDER BY alias.meta_value + 0 ".($userquery->query_vars["order"] == "ASC" ? "asc " : "desc ");//set sort order
        }
    }
}

0 个答案:

没有答案