显示登录用户购买的单品数量

问题描述 投票:0回答:0

这里需要一点帮助,我想找到登录用户购买的单个产品的总数。例如,如果今天他/她购买了 2 份“橘子”,一周后又购买了 5 份相同的产品。所以我想总结所有购买并将其存储到一个变量并打印出来。我不擅长 PHP,所以我找到了这段代码并根据我的需要做了一些修改,但它仍然没有输出任何东西,你能帮我解决我的错误吗? 谢谢

注意:产品编号将手动输入。

function checking_product_bought( $_product_id ){

    global $woocommerce, $posts;

    // Get the current customer info (as an object)
    $customer      = wp_get_current_user();
    $customer_id   = $customer->ID; // customer ID
    $customer_email = $customer->email; // customer email

    // Get all orders for this customer_id
    $customer_orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => $customer_id,
        'post_type'   => wc_get_order_types(),
        'post_status' => array_keys( wc_get_order_statuses() ),
    ) );

    if ( $customer_orders ){

        foreach ( $customer_orders as $customer_order ) {
            $order        = wc_get_order();
            $order_id     = $order->id; // get the order ID )or may be "order->ID")
            // getting all products items for each order
            $items = $order->get_items();

            foreach ($items as $item) 
            {
                $product_id = 10012; // product id
                $product_qty = $item['qty']; // product quantity

                if(wc_customer_bought_product( $customer_email, $customer_id, $_product_id)) 
                {
                    echo '<div>for product number: ' . $product_id . ' ,there is ' . $product_qty . ' for this product.';
                }

            }

        }

    }

}
php wordpress woocommerce hook-woocommerce woocommerce-rest-api
© www.soinside.com 2019 - 2024. All rights reserved.