Jul*_*den 7 php wordpress orders woocommerce advanced-custom-fields
我正在开发一种内部"插件"来显示我目前通过状态和客户经理进行拆分的订单数量.所以我想把它作为一个表格来呈现:
Account Manager | No of pending orders | No of canceled orders etc
Run Code Online (Sandbox Code Playgroud)
我有这样一段代码:
function display_woocommerce_order_count( $atts, $content = null ) {
$args = shortcode_atts( array(
'status' => 'completed',
), $atts );
$statuses = array_map( 'trim', explode( ',', $args['status'] ) );
$order_count = 0;
foreach ( $statuses as $status ) {
// if we didn't get a wc- prefix, add one
if ( 0 !== strpos( $status, 'wc-' ) ) {
$status = 'wc-' . $status;
}
$order_count += wp_count_posts( 'shop_order' )->$status;
}
ob_start();
echo number_format( $order_count );
return ob_get_clean();
}
add_shortcode( 'wc_order_count', 'display_woocommerce_order_count' );
Run Code Online (Sandbox Code Playgroud)
最重要的是,我看到了这段代码,但我不确定如何使用它:
public function get_customer_total_order() {
$customer_orders = get_posts( array(
'numberposts' => - 1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => array( 'shop_order' ),
'post_status' => array( 'wc-completed' )
) );
$total = 0;
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order );
$total += $order->get_total();
}
return $total;
}
Run Code Online (Sandbox Code Playgroud)
它会添加一个短代码以显示一般订单号,但不会向客户经理细分.通过ACF,我创建了一个自定义字段,"handlowiec"
并将其分配给订单屏幕.如何使它工作?
以下代码将按状态对与“ handlowiec” ACF字段相对应的当前客户经理ID的订单进行计数:
因此,您必须为每个订单分配一个客户经理ID:
具有非常简单的SQL查询的功能代码(基于wp_count_posts()
WordPress功能):
function display_woocommerce_order_count( $atts, $content = null ) {
$args = shortcode_atts( array(
'status' => 'completed, processing, on-hold, cancelled',
'handlowiec' => ''
), $atts );
// Formatting the order statuses for the SQL query
$statuses = $data = [];
$statuses_array = array_map( 'trim', explode( ',', $args['status'] ) );
foreach ( $statuses_array as $status ) {
if ( 0 !== strpos( $status, 'wc-' ) )
$statuses[] = 'wc-' . $status;
}
$statuses = implode("','", $statuses);
## -- 1. The SQL Query -- ##
global $wpdb;
$handlowiec_query = $join_postmeta = $output = '';
// Handling the Account Manager ID (optionally)
if( $args['handlowiec'] !== '' ){
$join_postmeta = "INNER JOIN {$wpdb->prefix}postmeta pm ON p.ID = pm.post_id";
$handlowiec_query = "AND meta_key like 'handlowiec' AND meta_value = ";
$handlowiec_query .= $args['handlowiec'];
}
$results = $wpdb->get_results("
SELECT post_status, COUNT( * ) AS num_posts
FROM {$wpdb->prefix}posts as p
$join_postmeta
WHERE post_type = 'shop_order'
AND post_status IN ('$statuses')
$handlowiec_query
GROUP BY post_status
");
## -- 2. Formatting the Output -- ##
// Loop through each order status count
foreach($results as $result){
$status = str_replace( 'wc-', '', $result->post_status );
$orders_count = (int) $result->num_posts;
if( $orders_count > 0 )
$data[] = ucfirst($status) . ' ' . _n( 'order', 'orders', $orders_count ) . ': ' . $orders_count;
}
if( $args['handlowiec'] !== '' )
$output = 'Account Manager ID ' . $args['handlowiec'] . ' | ';
else
$output = 'All Account Managers | ';
if( sizeof($data) > 0 )
$output .= implode( ' | ', $data );
else
$output .= 'No results';
return '<p>' . $output . '</p>';
}
add_shortcode( 'wc_order_count', 'display_woocommerce_order_count' );
Run Code Online (Sandbox Code Playgroud)
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。
用法:
1)没有“客户经理ID”(适用于所有经理):
echo do_shortcode("[wc_order_count]");
Run Code Online (Sandbox Code Playgroud)
您将得到类似:
Run Code Online (Sandbox Code Playgroud)All Account Managers | On-hold orders: 2 | Processing orders: 7 | …
2)使用特定的“客户经理ID”:
echo do_shortcode("[wc_order_count handlowiec='5']");
Run Code Online (Sandbox Code Playgroud)
您将得到类似:
Run Code Online (Sandbox Code Playgroud)Account Manager ID 5 | On-hold order: 1 | Processing orders: 3 | …
您还可以使用as before status
参数指定输出中将涉及哪些订单状态…