在Woocommerce Admin Edit Orders页面中显示客户取消的订单数

时间:2017-12-02 11:01:28

标签: php wordpress woocommerce backend orders

我试图计算客户已取消的订单数量,并在管理订单屏幕中显示它们。

我的问题是我无法让它为远程客户工作,我可以让它为我自己工作(作为current_user)。

这是我的代码(取自其他谷歌搜索和一些小修改):

// print the number
function print_the_number() { 
 echo do_shortcode( '[wc_order_count]' );
}

// add the action 
add_action( 'woocommerce_admin_order_data_after_order_details', 'print_the_number', 10, 1 ); 

然后在admin

中显示该号码
//MARK: Function to animate Image View (it will animate to the middle of the View)
func animateImageView(statusImageView : UIImageView) {
    //Get access to a starting frame
    statusImageView.frame.origin.x = 0
    if let startingFrame = statusImageView.superview?.convert(statusImageView.frame, to: nil) {

        //Add the view from cell to the main view
        let zoomImageView = UIView()
        zoomImageView.backgroundColor = .red
        zoomImageView.frame = statusImageView.frame

        view.addSubview(zoomImageView)


        print("Starting frame is: \(startingFrame)")
        print("Image view frame is: \(statusImageView.frame)")

        UIView.animate(withDuration: 0.75, animations: { 
            let height = (self.view.frame.width / startingFrame.width) * startingFrame.height
            let y = self.view.frame.height / 2 - (height / 2)

            zoomImageView.frame = CGRect(x: 0, y: y, width: self.view.frame.width, height: height)

        })
    }
}

任何帮助都是非常苛刻的!

2 个答案:

答案 0 :(得分:2)

您需要从当前订单中定位客户ID。它可以以更简单的方式完成。

你应该试试这个:

add_action( 'woocommerce_admin_order_data_after_order_details', 'get_specific_customer_orders', 10, 1 );
function get_specific_customer_orders( $order ) {

    $customer_orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => $order->get_customer_id(),
        'post_type'   => 'shop_order',
        'post_status' => array('wc-cancelled'),
    ) );

    $orders_count = '<strong style="color:#ca4a1f">' . count($customer_orders) . '</strong>';

    echo'<br clear="all">
    <p>' . __( 'Cancelled orders count: ' ) . $orders_count . '</p>';
}

代码放在活动子主题(或活动主题)的function.php文件中或任何插件文件中。

经过测试和工作。

答案 1 :(得分:0)

我在我的项目中使用了它:
使用

<块引用>

get_current_user_id()

代替

<块引用>

$order->get_customer_id()

add_shortcode( 'geo-get-customer-orders', 'geo_get_customer_orders' );
function geo_get_customer_orders() {
    global $order;
    $customer_orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => 'shop_order',
        'post_status' => array('wc-completed'),
    ) );

    echo count($customer_orders);
}

最后使用这个简码:

<块引用>

[geo-get-customer-orders]

相关问题