获取 Woocommerce 中 "billing company" 的所有客户数量
Get the count of all customers with a "billing company" in Woocommerce
我在 Wordpress 和 Woocommerce 中有一个网站,对于我的报告,我需要知道我有多少专业知识。
由于Woocommerce不提供这些信息,我需要自己去获取,但是我无法手动计算。
所以我决定使用 Woocommerce Rest Api 来获取信息。
我尝试获取所有填写 "billing company" 字段的客户。
我用 php curl 提出请求并开始我的代码:
//URL
$url = '<...>/wp-json/wc/v2/customers';
//REQUEST
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
curl_close($ch);
$json = json_decode($result, true);
print_r($json);
我遇到两个问题:
- 无法获得所有客户
- 无法获取 "billing company" 变量
有什么办法吗?
谢谢
为什么不在 WooCommerce > 报告 > 名称为 "Professionals" 的客户中添加自定义子选项卡。在此自定义选项卡中,您可以轻松获取拥有结算公司的客户数量。
密码是:
// Add a custom sub-tab to customer tab WooCommerce reports
add_filter( 'woocommerce_admin_reports', 'admin_report_customer_pro_section', 10, 1 );
function admin_report_customer_pro_section( $reports ){
$reports['customers']['reports']['customer_pro'] = array(
'title' => __( 'Professionals', 'woocommerce' ),
'description' => '',
'hide_title' => true,
'callback' => 'content_admin_report_customer_pro',
);
return $reports;
}
// The content of the customer custom sub-tab
function content_admin_report_customer_pro(){
// Get all Customers
$customers = get_users( array( 'role' => 'customer'));
$count = 0;
// Iterating through each customer in the array
foreach ($customers as $customer) {
// Count professional customers that have a billing company
if( ! empty( $customer->billing_company ) ) $count++;
}
// Output the count
echo '<div style="background-color:white; padding:8px 20px;"><p>'.__( 'Number of Professionals (with a company): ', 'woocommerce' ) . '<strong style="display:inline-block;background-color:#9c5d90; padding:0 10px; color:white;">' . $count . '</strong></p></div>';
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
经过测试并有效……您将得到:
我在 Wordpress 和 Woocommerce 中有一个网站,对于我的报告,我需要知道我有多少专业知识。
由于Woocommerce不提供这些信息,我需要自己去获取,但是我无法手动计算。
所以我决定使用 Woocommerce Rest Api 来获取信息。
我尝试获取所有填写 "billing company" 字段的客户。
我用 php curl 提出请求并开始我的代码:
//URL
$url = '<...>/wp-json/wc/v2/customers';
//REQUEST
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
curl_close($ch);
$json = json_decode($result, true);
print_r($json);
我遇到两个问题:
- 无法获得所有客户
- 无法获取 "billing company" 变量
有什么办法吗?
谢谢
为什么不在 WooCommerce > 报告 > 名称为 "Professionals" 的客户中添加自定义子选项卡。在此自定义选项卡中,您可以轻松获取拥有结算公司的客户数量。
密码是:
// Add a custom sub-tab to customer tab WooCommerce reports
add_filter( 'woocommerce_admin_reports', 'admin_report_customer_pro_section', 10, 1 );
function admin_report_customer_pro_section( $reports ){
$reports['customers']['reports']['customer_pro'] = array(
'title' => __( 'Professionals', 'woocommerce' ),
'description' => '',
'hide_title' => true,
'callback' => 'content_admin_report_customer_pro',
);
return $reports;
}
// The content of the customer custom sub-tab
function content_admin_report_customer_pro(){
// Get all Customers
$customers = get_users( array( 'role' => 'customer'));
$count = 0;
// Iterating through each customer in the array
foreach ($customers as $customer) {
// Count professional customers that have a billing company
if( ! empty( $customer->billing_company ) ) $count++;
}
// Output the count
echo '<div style="background-color:white; padding:8px 20px;"><p>'.__( 'Number of Professionals (with a company): ', 'woocommerce' ) . '<strong style="display:inline-block;background-color:#9c5d90; padding:0 10px; color:white;">' . $count . '</strong></p></div>';
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
经过测试并有效……您将得到: