创建一个返回int计算woocommerce订单状态和woocommerce订单备注的函数时,我无法使其工作,我不知道我做错了什么。
我知道wc_orders_count($ status)返回特定订单状态的订单数量,如果我使用了错误的功能,必须使用吗?如果我使用它错了,必须如何使用?
这是函数
add_action( 'init' 'custom_counter' );
function custom_counter() {
$customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types( 'view-orders' ),
'post_status' => array_keys( wc_get_order_statuses() )
) ) );
if ( $customer_orders ) {
$status = wc_get_order( $customer_orders )->get_status();
$orderscounter = wc_orders_count( $status );
?>
<p class="counter">
<?php echo $orderscounter; ?>
</p>
<?php
}
修改
实际上,我正在寻找一个在下新订单或订单状态更新时返回警报的功能,所以我不知道我是否需要一个计数器。
编辑2
我只是希望功能正常工作。假设客户有新订单,我们(管理员)将此订单的状态从“暂停”更新为“已完成”,我们需要客户知道页面前端的状态更改作为警报(但无论警报是什么,这是工作的简单部分,你可以忘记这一点,我可以做到),所以......必须退回管理员的任何订单状态更改或注释订单,因此我不需要柜台。
答案 0 :(得分:0)
您可以根据需要修改此功能数据,希望这有用。
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' );
编辑问题后尝试以下答案:
正如Xcid的回答所示,您需要注册该电子邮件。
add_action( 'woocommerce_order_status_wc-order-confirmed', array( WC(), 'send_transactional_email' ), 10, 10 );
我已经为WooCommerce 2.3添加了一个过滤器,所以当出现这种情况时,自定义电子邮件将能够添加到WooCommerce注册的电子邮件操作列表中:
// As of WooCommerce 2.3
function so_27112461_woocommerce_email_actions( $actions ){
$actions[] = 'woocommerce_order_status_wc-order-confirmed';
return $actions;
}
add_filter( 'woocommerce_email_actions', 'so_27112461_woocommerce_email_actions' );