在 Woocommerce 中获取所有客户手机 phone 号码

Get alll customer mobile phone numbers in Woocommerce

我想将我用户的所有 phone 号码循环输入一个输入字段。到目前为止,我在以下方面没有任何运气:

<?php for get_user_meta('phone_number',true); { echo $phone; "," } ?>

我试图将它们列为 07812345678, 07812345678, 07812345678, 等...直到最后一个没有“,”的数字。有人能帮我吗?如果有任何不同,这将在管理仪表板的主页上执行...

如果您的输入数据已经在一个数组中,那么您可以只使用 implode()。这将使用指定的分隔符连接数组的所有项目。

echo implode(', ', get_user_meta('phone_number',true));

您可以使用此自定义简码,它使用非常简单的 sql 查询来获取 所有 "customer" 用户角色 "billing phone" numbers 作为逗号分隔的数字字符串 phones

如果您使用的是自定义 phone 数字自定义字段,您只需在函数 billing_phone 中替换为您的自定义字段 slug (如 phone_number例如)

add_shortcode( 'customers_phones', 'get_customers_phone' );
function get_customers_phone(){
    global $wpdb;

    $results = $wpdb->get_col( "
        SELECT DISTINCT um.meta_value FROM {$wpdb->prefix}users as u
        INNER JOIN {$wpdb->prefix}usermeta as um ON um.user_id = u.ID
        INNER JOIN {$wpdb->prefix}usermeta as um2 ON um2.user_id = u.ID
        WHERE um.meta_key LIKE 'billing_phone' AND um.meta_value != ''
        AND um2.meta_key LIKE 'wp_capabilities' AND um2.meta_value NOT LIKE '%administrator%'
    " );

    // return a coma separated string of "customer" billing phones numbers
    return implode( ', ', $results );
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

USAGE - 显示数字 phones 逗号分隔的字符串:

  1. 在任何 Wordpress post 或接受短代码的页面文本编辑器中:

    [customers_phones]
    
  2. 里面php代码

    echo do_shortcode("[customers_phones]");
    
  3. php 文件中的 html 代码

    <?php echo do_shortcode("[customers_phones]"); ?>
    

下面是一个没有针对 "customer" 用户角色的替换版本:

add_shortcode( 'customers_phones', 'get_customers_phone' );
function get_customers_phone(){
    global $wpdb;

    $results = $wpdb->get_col( "
        SELECT DISTINCT um.meta_value FROM {$wpdb->prefix}users as u
        INNER JOIN {$wpdb->prefix}usermeta as um ON um.user_id = u.ID
        WHERE um.meta_key LIKE 'billing_phone' AND um.meta_value != ''
    " );

    // return a coma separated string of "customer" billing phones numbers
    return implode( ', ', $results ); 
}

我知道很久以前有人问过这个问题,但我会 post 对我有用的。

要从 Woocommerce 的结帐表单中将所有 phone 号码保存为账单 phone,您可以使用这样的功能:

/**
 * Get billing phone numbers, join them by a comma and return them as a string.
 */
function customer_loyalty_with_twilio_get_customers_phone(){
    global $wpdb;

    // use $wpdb to make a query directly to the database
    $results = $wpdb->get_col("
        SELECT DISTINCT `meta_value` FROM `{$wpdb->prefix}usermeta` WHERE `meta_key` = 'billing_phone' and `meta_value` != '';
    ");

    // return a coma separated string of "customer" billing phones numbers
    return implode( ',', $results );
}

其工作方式是使用数据库访问抽象 class、$wpdb。 SQL 查询 selecting 来自 table {$wpdb->prefix}postmeta 的所有 DISTINCT meta_value(如果你的 table 以“wp_”为前缀,那么 table 将被称为 wp_postmeta)。查询筛选 meta_key 列等于“_billing_phone”的结果。我添加了最后一个条件 and `meta_value` != '' 以排除空记录。

希望这对某人有用。